# CSCE 240 Homework 1 solved

\$35.00

Category:

## Description

Everyone in computing should have a basic understanding of binary integers. To that end, you will write an
app to convert decimal to binary.
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 two’s 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 2i
from n.
1
2. For each exponent, j, from i − 1 down to 0,
(a) if that 2j ≤ n, print a 1 and subtract 2j
from n