Question

Transcribed Image Text:(DecBin2) Write a program that will accept a number in decimal and display the number in binary.
In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary form
BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought (i.e.
four). In this programming assignment we seek to remedy this limitation!
(DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non-
negative base 10 integer has value between 0 and 15 inclusive.
As a guiding example, let's convert (13), to binary (base 2).
Using long division, we successively divide by the base to which we are converting, 2 in this case.
6
3
0
2)13 26 23
-12
6
2
0
1
0
1
1
The resulting binary number is (1101), (writing the remainders as read from right to left).
Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In
Java, we can express the quotient and remainder in long division as follows.
-12
1
Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as
(b3 b2 b1 b0), =(1 101), (respectively).
Then
13/2
integer division
yields the quotient
b0n & 2;
b1 = (n/2) & 2;
b2= ((n / 2)/2) & 2;
b3 (((n / 2)/2)/2 ) & 2;
and
13%2
modulus operator.
yields the remainder
This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to
change n to the most recent quotients as we go,
b0n & 2;
nn / 2;
bln 2;
nn / 2;
b2 n 2;
n = n / 2;
b3 = n2;
n = n / 2;
We're done once the quotient becomes 0 which will be on the last step.
//b0 will be 1
// new n will be 6.
// bl will be 0 (since 6 & 2 = 0)
// new n will be 3.
// b2 will be 1 (since 3 & 2 = 1)
// new n will be 1
// b3 will be 1 (since 1 & 2 = 1)
// new n will be 0 (since 1 / 2 = 0)
Sample Output:
Enter a positive integer: 37
The number 37 in binary is 100101.
Notice how we were essentially repeating divisions by 2 and updating the integer, n, containing the remaining bits in the
number. The situation now is that we don't know ahead of time how many times we need to repeat this sequence of
divisions, but we know were have finished when there are no more remaining bits to extract, i.e. when n becomes 0.

Transcribed Image Text:Question to ponder: The bits are produced in the reverse order. How can we encode this information into a single
decimal number for display? In the above example, we must create the number 1101 (one thousand one hundred and one)
to display the base 2 result.
Hints for constructing numbers or Strings that look like numbers
IMPORTANT: These samples appear in the PA 4 Unit. Use the debugger to trace them by setting
at breakpoint a as shown below and single stepping through the code paying attention to how the I
and total variables of Example 1 and the digitAs String and total variables in Example2
are changing with each step.
Example 1: (DecBin2_Example_1) A Numeric Approach
int total = 0;
for(int i=3; i < 10; i+=2)
{
The String
}
total total 10;
total += 1;
System.out.println( "Output 1: " + total);
Example 2: (DecBin2_Example_2) A String Approach
String total = "";
for(int i=3; i <10; i+=2)
{
}
String digitAsString = "" + 1;
total = total + digitAsString;
System.out.println( "Output 2: " + total);
Note that the statement digitAsString = "" + i does not perform numerical addition by rather joins
(concatenates) the String "" with the number contained in I and produces a String result. For example,
if i=5, then "" + 5"" + "5" (note how Java converted 5 to "5") → "5"
"" is called the empty string; it is a string that contains no characters.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images

Knowledge Booster
Similar questions
- Take a look at the following assembly code. Which of the following describes what is going on in the code? MOVQ $0 , %raxmystery: INCQ %rax CMPQ $5 , %rax JL mystery Group of answer choices A. This is a function call to the function 'mystery' where we increment the value stored in rax. B. This is essentially a while loop decreasing a counter by 1. C. First 0 is stored in a register--rax. Then we increment rax by 1, and compare to see if the value is 5. If it is less than 5, then we jump back to the label mystery. This is essentially a while loop incrementing a counter by 1.arrow_forwardI need some help redoing this code, I want it to function a little differently than this. The code basically needs the following things: cannot use the power function, cannot use the sin function, must use header files (user defined functions), a loop must be used for factorial functions. Basically using these rules it should autmatically change the input of degrees to radians. The overall code should compute sin(x) using a taylor series expansion(using loops instead of using the factorial function) This is what I have, I know it works but I would like to change it.arrow_forwardPlease solve and show steps, work, and information related to this problem. Thank you. The following C++ code performs a set of various arithmetic operations using both pointers and references: Fill in a memory table that shows the final values for both function a and function b. Indicate the final values of each parameter, passed in variable(s), and the return value of the function. Provide a separate copy of this memory table for each function.arrow_forward
arrow_back_ios
arrow_forward_ios