Question: Hello, I need help with this C++ code and Im using VS IDE. I need to convert a decimal number to binary and a binary
Hello, I need help with this C++ code and Im using VS IDE.
I need to convert a decimal number to binary and a binary number to decimal. I should do this by coding specific algorithms for these processes, as described below. My current code on Decimal to Binary is good but Binary to Decimal menu is no good. My menu should accept lower case and I should display the binary conversion process steps. I should validate the binary input as numeric and non-negative and only 1's and 0's. Binary input of 121 should be rejected as non-binary.
My current code is below these instructions:
A sample run would look something like this:
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!
My instructions are:
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.
Binary to Decimal
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.
#include "stdafx.h"
#include
#include
#include
using namespace std;
using namespace System;
void ConvertByRecursion(long long n);
long long getDecimal();
long long getBinary();
long long BinaryToDecimal(long long);
int main()
{
long long n, result;
char choice;
bool quit;
cout << "Decimal-Binary Conversions By Vaniese Pickett" << endl;
cout << " Welcome to the Decimal to Binary Converter." << endl;
while (choice != 0)
{
cout << " Dec2Bin or Bin2Dec or Quit? (D/B/Q): ";
cin >> choice;
quit = false;
while (!quit)
{
if (choice == 'D' || 'd')
{
n = getDecimal();
quit = true;
while (n != 0)
{
ConvertByRecursion(n);
cout << endl << endl;
n = getDecimal();
}
}
else if (choice == 'B' || 'b')
{
n = getBinary();
result = BinaryToDecimal(n);
cout << " Therfore the binary value is : " << result << endl;
cout << " Thanks for using the Decimal-Binary converter!" << endl;
}
if (choice == 'Q' || 'q')
{
cout << " Thanks for using the decimal converter!" << endl;
system("Pause");
return 0;
}
}//end of while (!quit)
cout << " Thanks for using the decimal converter!" << endl;
system("Pause");
}//end of while for choice
return 0;
}
long long getDecimal()
{
long long n;
bool baddata = false;
do
{
baddata = false;
cout << "Please enter your decimal value to be converted (0 to quit): ";
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, by recursion, the binary value is: ";
}
cout << r;
}
long long getBinary()
{
long long n;
bool baddata = false;
do
{
baddata = false;
cout << "Please enter your binary value to be converted (0 to quit). ";
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;
}
long long BinaryToDecimal(long long n)
{
long long value = 0, i = 0, binarydigit, position;
while (n != 0)
{
binarydigit = n % 10;
position = pow(2, i);
value = value + binarydigit * position;
n = n / 10;
++i;
}
return value;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
