Question: I am using C++ to write a program that converts Decimal to Binary and Vice Versa. The program is to follow these guidelines, pay attention
I am using C++ to write a program that converts Decimal to Binary and Vice Versa. The program is to follow these guidelines, pay attention to the sample output below.
IS256: C++ Programming
Binary/Decimal conversions (100 points) 2017
For this assignment you will write a program which can convert a decimal number to binary, or a binary number to decimal. You are to accomplish this by coding specific algorithms for these processes, as described below.
A sample run would look something like this:
Decimal-Binary Conversions By xxxxxxxxxxxxxxxxxxxxx
Dec2Bin or Bin2Dec or Quit? (D/B/Q): D
Please enter your decimal value to be converted (0 to quit): 320
Decimal 320 divided by 2 equals 160 with a remainder of: 0
Decimal 160 divided by 2 equals 80 with a remainder of: 0
Decimal 80 divided by 2 equals 40 with a remainder of: 0
Decimal 40 divided by 2 equals 20 with a remainder of: 0
Decimal 20 divided by 2 equals 10 with a remainder of: 0
Decimal 10 divided by 2 equals 5 with a remainder of: 0
Decimal 5 divided by 2 equals 2 with a remainder of: 1
Decimal 2 divided by 2 equals 1 with a remainder of: 0
Decimal 1 divided by 2 equals 0 with a remainder of: 1
Therefore, by recursion, the binary value of 320 is: 101000000
Please enter your decimal value to be converted (0 to quit): 0
Dec2Bin or Bin2Dec or Quit? (D/B/Q): B
Please enter your binary value to be converted (0 to quit): 1010
There is a 2 in the value (2^1)
There is a 8 in the value (2^3)
Therefore the decimal value is: 10
Please enter your binary value to be converted (0 to quit): 0
Dec2Bin or Bin2Dec or Quit? (D/B/Q): Q
Thanks for using the Decimal-Binary converter!
The xxxxxxxxxxxxxxxxxx in the sample above should be your name (of course).
Part A (Decimal to Binary 50 pts):
An algorithm for converting a decimal value into its binary equivalent involves repeated (integer) division by 2 (down to a result of 0), collection of the remainders, and reading those remainders backwards. (see sample above)
You are to code this algorithm using Recursion:
Recursion is a looping technique wherein a function calls itself (usually sending the recursive call a new set of parameter values). The recursive process (here the division by 2 step of each iteration) may proceed any number of times before a control condition is reached that stops the final routine from making another recursive call; this initiates a series of step out actions wherein the recursively called routines step backwards to the beginning of the process (this method will be explained in a posted lecture).
Note that when you build the programs recursive algorithm, you must make each call by sending only the number to be divided by 2 for the current iteration (i.e., only one parameter in the recursive function no additional control values).
As noted, additional materials and a recorded lecture will be posted regarding the recursive technique.
Part B (Binary to Decimal 50 pts):
A standard algorithm for a binary to decimal conversion evaluates the positional values of the binary digits. For example, the binary value of: 101 is 5 because you have the following:
Binary powers of 2: 2^2 2^1 2^0
Positional value: 4 2 1
Binary digits: 1 0 1
So the value = (1*4) + (0*2) + (1*1) = 5
Likewise, the binary value of 11111101 = 253 because you have the following:
Powers of 2: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Positional values: 128 64 32 16 8 4 2 1
Binary digits: 1 1 1 1 1 1 0 1
So the value = 128 + 64 + 32 + 16 + 8 + 4 + 1 = 253
Code this part of the program using the above algorithm. Make sure input values are validated as having only ones and zeroes, and use separate function calls for data validation and conversion.
I have completed part A of the assignment! It is part B I am having trouble with! Make sure you follow the steps above and that the program works as intended as per the sample output above! Here is what I have so far. Modify the code below so that it works as intended as above.
#include "stdafx.h"
#include
#include
using namespace System;
using namespace std;
void ConvertByRecursion(long long n);
long long getDecimal();
int main()
{
long long n;
cout << "Welcome to the Decimal to Binary Converter!" << endl;
n = getDecimal();
while (n !=0)
{
ConvertByRecursion(n);
cout << endl << endl;
n = getDecimal();
}
cout << " Thanks for using the decimal converter!" << endl;
system("Pause");
return 0;
}
long long getDecimal()
{
long long n;
bool baddata = false;
do
{
baddata = false;
cout << "Please enter your decimal value to convert: ";
cin >> n;
if(!cin.good())
{
cin.clear();
cin.ignore(numeric_limits
cout << "I could not decipher your input as a decimal value." << endl;
baddata = true;
}
else if (n < 0)
{
cout << "Please enter a non-negative value." << endl;
baddata = true;
}
} while (baddata);
return n;
}
void ConvertByRecursion(long long n)
{
long long r;
long long newval;
r = n % 2;
newval = n / 2;
Console::WriteLine("Decimal {0,12:D} divided by 2 = {1, 12:D} w/Remainder of: {2,3:D} ", n, newval, r);
if (newval > 0)
{
ConvertByRecursion(newval);
}
else
{
cout << " Therefore the binary value is: ";
}
cout << r;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
