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

using array data structure for a binary number write a C++ program that will app

ID: 3543627 • Letter: U

Question

using array data structure for a binary number write a C++ program that will apply the shift add algorithm in order to mutiply two 8 bit binary numbers. Your program will apply functions in order to perform the following operations:

A.function1:
i.allow a user to input two decimal integers between 0 and 255

ii.convert them to binary numbers and store them in two different arrays

iii.output the product of the decimal numbers using the '*' operator

B.Function 2:

i.convert a number from decimal to binary

C.Function 3:

i.given two binary numbers(each represented in a 1-d array) as the input

ii.implement the shift and add algorithm.Given the constraints of the data structure, the shift operation may involve an array copy function.

iii.convert the binary result to a decimal number.

iv.return the binary result of the multiplication

v.retun the equivalent decimal number

D.Function 4:(called by function 3)

i.convert a number from binary to decimal

E.Function 5:(called by function 3)

i.perform the binary addition of two numbers represented in 1-d arrays

F.main

i.output the binary result retuned from fiunction 3

ii.output the decimal result returned from function 3

iii.compare decimal result from function 3 with the decimal result from function 1 and indicate whether or not they are equal

you must demonstrate your program works by showing output for the product of the following pairs of numbers (0, 100), (255,255), and (100,11).

Explanation / Answer

// the full source code....

#include<iostream>

using namespace std;


/* prints the binary number*/

void print_bin(int *b) {

int i;

for(i=0;i<17;i++)

if(b[i]!=-1)

cout << b[i];

cout <<endl;

return;

}


/* converts the decimal a(<=255) to an 8 bit binary and returns it*/

int *dec_to_bin(int a) { //func 2

if(a>255) return NULL;

int k = 128, i;

int* bin = new int[17];

for(i=0;i<17;i++) {

if(k==0)

bin[i] = -1;

else {

bin[i] = a/k;

a = a%k;

k /= 2;

}

}

return bin;

}


/*converts the binary to a decimal and returns that decimal*/

int bin_to_dec(int *b) { //func 4

int i, sum = 0;

for(i=0;i<17;i++)

if(b[i]!=-1) {

sum *= 2;

sum += b[i];

}

return sum;

}


/*takes two numbers and adds them considering that their 0th bit is the most significant bit and also shifts the multiplication to include the carry*/

int *add_bin(int *b1, int *b2) { //func 5, if b1 and b2 are n1, n2(n1>=n2) bit, then the output is n1+1 bit, ie this funct adds and shifts the number

int i,j;

for(i=0;i<17;i++)

if(b1[i]==-1)

break;


for(j=0;j<17;j++)

if(b2[j]==-1)

break;

if(i<j) return NULL;

for(;j<i;j++)

b2[j] = 0;


  

int carry = 0, sum;

int *bout = new int[17];

for(i=0;i<17;i++) bout[i] = -1;

  

for(;j>0;j--) {

sum = b1[j-1] + b2[j-1] + carry;

bout[j] = sum%2;

carry = sum/2;

}

bout[0] = carry;

delete[] b1;

return bout;

}



/*the main algorithm is here

* it takes two binary numbers as input

* it returns their product as output in binary form

*/

int* shift_add_algo(int* n, int* x) { // func 3, assumes 8 bit numbers and returns their binary multiplication n*x in 16 bit number

int i;

int n_dec = bin_to_dec(n);

int *ans = new int[17];

ans = dec_to_bin(0);

int *temp = new int[17];

for(i=7;i>=0;i--) {

if(x[i]==0) {

delete[] temp;

temp = dec_to_bin(0);

ans = add_bin(ans, temp);

}

else if(x[i]==1) {

delete[] temp;

temp = dec_to_bin(n_dec);

ans = add_bin(ans, temp);

}

else

break;

}

return ans;

}


/*

* the function 1 which takes 2 int as input, provokes the function 3, and returns the answer from func 3 in decimal form

*/

int multiply_two_numbers() { // function 1

int a,b, c;

cout << "Input two numbers 0-255: " ;

cin >> a >> b;

int* bin_a = dec_to_bin(a);

int* bin_b = dec_to_bin(b);


// the shift add algorithm called here

int *bin_c = shift_add_algo(bin_a,bin_b);


// print output from * operator

cout << a << "*" << b << "=" << a*b << endl;

// print the binary representation of the output

cout << "Binary representation of number obtained from funtion 3 is: ";

print_bin(bin_c);

  

// convert the binary to decimal

c = bin_to_dec(bin_c);


// free the memory

delete[] bin_a;

delete[] bin_b;

delete[] bin_c;


// return product

return c;

}



int main() {

cout << "By the function 3, result is: " << multiply_two_numbers() << endl;

cout << "By the function 3, result is: " << multiply_two_numbers() << endl;

cout << "By the function 3, result is: " << multiply_two_numbers() << endl;


return 0;

}