Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C++ ( Visual Studio 2017 ) Conversion_between_Binary_and_Decimal

ID: 3872671 • Letter: P

Question

Programming in C++ ( Visual Studio 2017 )

Conversion_between_Binary_and_Decimal

I have to make two functions, int BinToDec(char* s) and char* DecToBin(int n).

The purpose is to make you understand the number system concept. So you have to manually implement the ideas without making use of some library function, like itoa(), atoi(), pow(), etc. that make the assignment meaningless. Only function allowed to use is strlen().

1. For BinToDec(),

- Multiplication complexity of O(n) is required

- Using Horner's method is suggested

2. For DecToBin(),

- Correctly using memory to return char* is required

- The method of dividing by 2 and taking remainders reversely is suggested

Then you call two functions in main() like this:

Also, don't use OOP based class/object feature, either a standard string or your created one. You must use C-string. Notice

1. The difference between a C-string and a char array. C-string is a zero-terminated char array

2. For a C-string, you can directly use it in cin and cout. Don’t set a loop to input/output for each char

3. If not understood well, search online such as

- C strings and C++ strings at www.prismnet.com/~mcmahon/Notes/strings.html

- C-style strings in C++ at www.cplusplus.com/forum/beginner/73114/

4. Avoid using mixed cin>>someVar and get()/getline() in this assignment.

Conversion_between_Binary_and_Decimal (Chapter 1) Write a C++ program converting an 8-bit unsigned binary to its decimal and converting a positive decimal to its 8-bit binaries. You can create functions declared as :1 : // Description: This function converts an 8-bit binary integer to its decimal :// Parameter: s [IN]a C-string with 8-bit binary digits received :// Return: / Function: BinToDec A decimal integer value calculated froms int BinToDec (char* s); : // Function: DecToBin : // Deseription: This function converts a decimal integer to its binary bits :/ Parameter: n [IN]a decimal integer received, less than 256 // Return : A C-string with 8-bit binary digits calculated from n char* DecToBin(int n)

Explanation / Answer


C++ source code: The below coverts decimal to binary as well as binary to decimal. Please refer to the output for clear understanding
// Header files
#include <iostream>
#include <cmath>
using namespace std;
//method definition
int BinToDec(long long);
long long DecToBin(int);
//main starts
int main()
{

int ch;
while(true)
{
cout<< "Please select the conversion type:"<<endl;
cout<<"1. Binary to Decimal"<<endl;
cout<<"2. Decimal to Binary"<<endl;
cout<<"3. Exit"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
//switch case
switch(ch)
{
case 1:
long long n1;
int decNumber;
cout << "Enter number in binary format: ";
cin >> n1;
decNumber = BinToDec(n1);
cout <<"Decimal number is:"<<decNumber<<endl;
break;
case 2:
int n2, binNumber;
cout << "Enter a number in decimal format: ";
cin >> n2;
binNumber = DecToBin(n2);
cout << "Binary number is:"<<binNumber << endl ;
break;
case 3: exit(0);
break;
default:
cout<<"Enter valid input"<<endl;
break;

}
}
return 0;
}
// method which converts binary to decimal
int BinToDec(long long n1)
{
int decNumber = 0, i = 0, rem;
while (n1!=0)
{
rem = n1%10;
n1 /= 10;
decNumber += rem*pow(2,i);
++i;
}
return decNumber;
}
//method which converts decimal to binary
long long DecToBin(int n2)
{
long long binNumber = 0;
int rem, i = 1;
while (n2!=0)
{
rem = n2%2;

n2 /= 2;
binNumber += rem*i;
i *= 10;
}
return binNumber;
}


Output screenshot:
Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
Enter your choice:
1
Enter number in binary format: 100
Decimal number is:4
Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
Enter your choice:
2
Enter a number in decimal format: 3
Binary number is:11
Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
Enter your choice:
3

Exit code: 0 (normal program termination)