Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 1 - File manipulation, binary numbers, exception handling (50 pts) (Each question is worth 10 points. Make sure your code is clear and concise.)
Problem 1 - File manipulation, binary numbers, exception handling (50 pts) (Each question is worth 10 points. Make sure your code is clear and concise.) The aim here is to convert a binary number to a decimal number. Recall that any number can be written as the sum of the products of its digits and its base to power j ( goes from 0 to some constant k). For example, 219 in decimal can be written as 2x102 + 1x101 + 9x 10 a) Write a function that takes as input an integer (in binary), then prints its digits in reverse order. For example, on input 10011, the function prints 1, 1, 0, 0, 1. Hint: Use the operators modulus and division to extract the digits of an integer n. b) Write a function that converts a binary number to a decimal number. For example, the binary number 1001 is equal to 1x23 + 1*2 = 9 (in base 10). You may use here the function implemented in (a) to compute the decimal number from the least significant bit to the most significant bit (i.e. right to left). c) Write a function that asks the user to enter an integer k, then writes to a file (e.g. binary.txt) all the possible values of the binary numbers of length k, together with their decimal notations. For example, if n = 2, then the content of the file binary.txt is given below (the order is not important): 00,0 01, 1 10,2 11,3 Hint: you may use the function itoa () in C++ d) Write a function that prints the content of the file you created (e.g. binary.txt). Make sure to add appropriate instructions in case the file is not accessible. e) Test your program in a main function. You may generate all the binary numbers of length 4 for example, store them in a file then print its content on the screen
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started