Write a C++ program which converts numbers from decimal to binary and from binar
ID: 673646 • Letter: W
Question
Write a C++ program which converts numbers from decimal to binary and from binary to decimal. The program must contain all of the following functions (10 points):
A function named binToDec, which converts an eight-bit unsigned binary number to a positive decimal integer and then returns that integer value. Use an array named binNum[] to store the eight-bit binary number as integer values (0’s and 1’s). The most significant (i.e. leftmost) bit should be stored in binNum[0] and the least significant bit should be stored in binNum[7]. Ask the user to input the binary number with each bit separated by at least one space. (2 points)
A void function named decToBin, which converts a positive decimal integer value to an eight-bit unsigned binary number. Use an array named binNum[] to store the eight-bit binary number as integer values (0’s and 1’s). The most significant (i.e. leftmost) bit should be stored in binNum[0] and the least significant bit should be stored in binNum[7]. (2 points)
A function named tcBinToDec, which converts an eight-bit two’s complement binary number into a decimal integer and then returns that integer value. Use an array named binNum[] to store the eightbit binary number as integer values (0’s and 1’s). The most significant (i.e. leftmost) bit should be stored in binNum[0] and the least significant bit should be stored in binNum[7]. (2 points)
A void function named decToTcBin, which converts a decimal integer to an eight-bit two’s complement binary number. Use an array named binNum[] to store the eight-bit binary number as integer values (0’s and 1’s). The most significant (i.e. leftmost) bit should be stored in binNum[0] and the least significant bit should be stored in binNum[7]. Ask the user to input the binary number with each bit separated by at least one space. (2 points)
A main function which presents with the user with a menu. Each menu choice should correspond to one of the functions listed above. The menu should also give the user a choice to exit the program when he or she is done using it. Print the menu, capture the user’s menu choice, and activate one of the above methods from within a sentinel loop which ends only when the user has chosen the “exit” menu selection. (2 points) Here is what the program’s output might look like:
Welcome to the Number Conversion Program! -----------------------------------------
(1) Convert unsigned binary to unsigned decimal
(2) Convert unsigned decimal to unsigned binary
(3) Convert two's complement binary to decimal
(4) Convert decimal to two's complement binary
(5) Exit the program Your choice?> 1
Enter an eight-bit binary number with a space between bits: 0 0 0 0 1 1 1 1 The decimal value of 00001111(bin) is 15(dec).
Welcome to the Number Conversion Program! -----------------------------------------
(1) Convert unsigned binary to unsigned decimal
(2) Convert unsigned decimal to unsigned binary
(3) Convert two's complement binary to decimal
(4) Convert decimal to two's complement binary
(5) Exit the program Your choice?> 2
Enter a decimal number: 33 The binary value of 33(dec) is 00100001(bin).
Welcome to the Number Conversion Program! -----------------------------------------
(1) Convert unsigned binary to unsigned decimal
(2) Convert unsigned decimal to unsigned binary
(3) Convert two's complement binary to decimal
(4) Convert decimal to two's complement binary
(5) Exit the program Your choice?> 3
Enter an eight-bit two's complement binary number with a space between bits: 1 1 0 1 1 0 0 0 The decimal value of 11011000(bin) is -40(dec).
Welcome to the Number Conversion Program! -----------------------------------------
(1) Convert unsigned binary to unsigned decimal
(2) Convert unsigned decimal to unsigned binary
(3) Convert two's complement binary to decimal
(4) Convert decimal to two's complement binary
(5) Exit the program Your choice?> 4
Enter a decimal number: -67 The binary value of -67(dec) is 10111101(bin). Welcome to the Number Conversion Program! -----------------------------------------
(1) Convert unsigned binary to unsigned decimal
(2) Convert unsigned decimal to unsigned binary
(3) Convert two's complement binary to decimal
(4) Convert decimal to two's complement binary
(5) Exit the program Your choice?> 5
Thank you for using the Number Conversion Program!
Explanation / Answer
Conversion.cpp
/* C++ programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */
#include <iostream>
#include <cmath>
#include <cassert>
#include <iomanip>
#include <cstring>
using std::setw;
using std::cout;
using std::endl;
using namespace std;
int binary_decimal(int n);
int decimal_binary(int n);
int convert(char binary[]);
int decimal_binary_twoscomplement(int n) ;
int twoscomplement_decimal(int n);
int decimal_binary(int n) /* Function to convert decimal to binary.*/
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
int twoscomplement_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
decimal=-decimal;
return decimal;
}
int decimal_binary_twoscomplement(int n) /* Function to convert decimal to binary.*/
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
binary=~binary;
binary=binary+1;
return binary;
}
int main()
{
int n;
int choice;
cout << "Welcome to number conversion program "<<endl;
cout << "1.Convert unsigned binary to unsigned decimal"<<endl;
cout << "2.Convert unsigned decimal to unsigned binary"<<endl;
cout << "3.Convert two's complement binary to decimal"<<endl;
cout << "4.Convert decimal to two's complement binary"<<endl;
cout << "Exit the program"<<endl;
cout << "Your Choice:";
cin >> choice;
switch(choice)
{
case 1 :
cout << "Enter a binary number: ";
cin >> n;
cout << ":Converted unsigned binary to unsigned decimal = " << binary_decimal(n);
break;
case 2 :
cout << "Enter a decimal number: ";
cin >> n;
cout << ":Converted unsigned decimal to unsigned binary" << decimal_binary(n) <<endl;
break;
case 3 :cout << "Enter a twoscomplement binary number: ";
cin >> n;
cout << ":Converted two's complement binary to decimal= " <<twoscomplement_decimal(n)<<endl;
break;
case 4 :
cout << "Enter a decimal number: ";
cin >> n;
cout << " :Converted decimal to two's complement binary= " <<decimal_binary_twoscomplement(n) <<endl;
break;
case 5 :
exit(0);
break;
default :
cout << "Invalid Choice" << endl;
}
return 0;
}
output:
Welcome to number conversion program
1.Convert unsigned binary to unsigned decimal
2.Convert unsigned decimal to unsigned binary
3.Convert two's complement binary to decimal
4.Convert decimal to two's complement binary
5. Exit the program
Your Choice:1
Enter a binary number: 111
Convert unsigned binary to unsigned decimal = 7
Your Choice:2
Enter a decimal number: 11
:Converted unsigned decimal to unsigned binary1011
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.