1 Decimal Binary and Octal Write a series of functions that

1.) Decimal Binary and Octal : Write a series of functions that convert a decimal number into binary and octal.

2.) Binary and Octal Decimal: Write a series of functions that convert a binary or octal number back into decimal. For Problems 1 and 2, check your functions with the following: 10Decimal = 1010Binary = 12Octal 20Decimal = 10100Binary = 24Octal

3.) Binary Addition: Write a function that adds two binary numbers that are entered by the user. You may represent each binary number as a string of 0’s and 1’s. Remember that the calculations need to start at the end of the strings. For binary numbers of different length, you may pad the shorter number with 0’s in the front. Also, make sure that the carry bit is implemented properly. For example, the solution of 1 + 1 = 10.

Problems In this lab, we will use Matlab (or C++) to implement a variety of problems. Note for all problems listed, the use of built-in functions or libraries is allowed and should be well commented. 1. Decimal Binary and Octal (30 points Write a series of functions that convert a decimal number into binary and octal 2. Binary and Octal Decimal (30 points) Write a series of functions that convert a binary or octal number back into decimal. For Problems 1 and 2, check your functions with the following: 10 Decimal 1010 Binary 12 Octal 20 Decimal 10100 Binary 24 Octal 3. Binary Addition (40 points) Write a function that adds two binary numbers that are entered by the user. You may represent each binary number as a string of 0\'s and 1\'s. Remember that the calculations need to start at the end of the strings. For binary numbers of different length, you may pad the shorter number with 0\'s in the front. Also, make sure that the carry bit is implemented properly. For example, the solution of 1 1 10

Solution

Q1

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
using namespace std;

int convertBinarytoOctal(long number1);
int convertDecimaltoBinary(int number);
int main()
{
string number;
cout<<\"Enter decimal no\";
cin>>number;
int b = atoi(number.c_str());
  
long val = convertDecimaltoBinary(b);
cout<<\"Binary equivalent is \"<<val<<endl;
cout<<endl;
cout<<\"Octal equivalent is \"<<convertBinarytoOctal(val)<<endl;
  
}

int convertDecimaltoBinary(int number){
long dec,rem,i=1,sum=0;
dec = number;
do
{
rem=dec%2;
sum=sum + (i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
return sum;   
}

int convertBinarytoOctal(long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;

while(binaryNumber != 0)
{
decimalNumber += (binaryNumber%10) * pow(2,i);
++i;
binaryNumber/=10;
}

i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}

Q2

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cmath>

using namespace std;

long convertOctalToBinary(int number1);
int binaryToDecimal(long number);

int main()
{
int octalNumber;
cout << \"Enter an octal number: \";
cin >> octalNumber;
long binary = convertOctalToBinary(octalNumber);
cout << \"decimal equivalent is \"<<binaryToDecimal(binary)<<endl;

}


long convertOctalToBinary(int octalNumber)
{
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;

while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}

i = 1;

while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}

return binaryNumber;
}

int binaryToDecimal(long number){
   long bin, dec = 0, rem, base = 1;

bin = number;
while (number > 0)
{
rem = number % 10;
dec = dec + rem * base;
base = base * 2;
number = number / 10;
}
cout << \"The decimal equivalent of \" << bin << \" : \" << dec << endl;
}

Q3

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cmath>

using namespace std;
string addBitStrings( string first, string second );
int makeEqualLength(string &str1, string &str2);

int main()
{
string number1, number2;
cout<<\"Enter first binary number\";
cin>>number1;
cout<<\"Enter second binary number\";
cin>>number2;
string finalans = addBitStrings(number1,number2);
cout<<\"Binary addition of both number is \"<<finalans<<endl<<endl;
}

string addBitStrings( string first, string second )
{
string result;
int length = makeEqualLength(first, second);

int carry = 0;
for (int i = length-1 ; i >= 0 ; i--)
{
int firstBit = first.at(i) - \'0\';
int secondBit = second.at(i) - \'0\';

int sum = (firstBit ^ secondBit ^ carry)+\'0\';

result = (char)sum + result;

carry = (firstBit & secondBit) | (secondBit & carry) | (firstBit & carry);
}

if (carry)
result = \'1\' + result;

return result;
}

int makeEqualLength(string &str1, string &str2)
{
int len1 = str1.size();
int len2 = str2.size();
if (len1 < len2)
{
for (int i = 0 ; i < len2 - len1 ; i++)
str1 = \'0\' + str1;
return len2;
}
else if (len1 > len2)
{
for (int i = 0 ; i < len1 - len2 ; i++)
str2 = \'0\' + str2;
}
return len1;
}

1.) Decimal Binary and Octal : Write a series of functions that convert a decimal number into binary and octal. 2.) Binary and Octal Decimal: Write a series of
1.) Decimal Binary and Octal : Write a series of functions that convert a decimal number into binary and octal. 2.) Binary and Octal Decimal: Write a series of
1.) Decimal Binary and Octal : Write a series of functions that convert a decimal number into binary and octal. 2.) Binary and Octal Decimal: Write a series of
1.) Decimal Binary and Octal : Write a series of functions that convert a decimal number into binary and octal. 2.) Binary and Octal Decimal: Write a series of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site