Question: app to convert decimal to binary in C++ The application should perform as follows: The program is called and runs with no prompt for input.

app to convert decimal to binary in C++

The application should perform as follows: The program is called and runs with no prompt for input. The first value provided to the standard input stream shall be an integer and be interpreted as the number of bits of binary output. This value should be something in the range [1, 31]. Do be careful, sometimes I get careless and enter values outside this range. If that happens, the app should quietly exit with a value of 1. The second value provided is the decimal integer to be converted. Though I can guarantee that you will see values in the range [0, 2 31 1], I am sometimes careless about the number of bits required to represent a decimal integer. If I make a mistake by entering a value too large for the number of bits, that is on me and you should still only print the number of bits requested. If I enter a negative value, do not print a twos complement; again, quietly exit, this time with a value of 2. An algorithm to convert decimal number, n, to binary (without arrays): 1. Use loop to calculate the largest power of 2, call it i, in n, print a 1 and subtract 2 i from n. 1 2. For each exponent, j, from i 1 down to 0, (a) if that 2 j n, print a 1 and subtract 2 j from n (b) otherwise if that n < 2 j , print a 0. There are other algorithms which are easier as well as improvements to this algorithm. You should not solve this with an array (or string).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!