Question

In: Computer Science

1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two...

1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two positive binary number in string and perform the addition with Lab05.2 function enhance in a way can accept binary string in addition to decimal string (use input parameter to control the base2 or base10 addition) and output the as binary string. (Optional: Demonstrate 8 bits and 16 bits in length as inputs.) // the example function prototype for addition function below where accepting two numbers, m, and base as input; output the addition result as string type as return value; base should be 2 or 10 as required. string addFunction(string numberA, string numberB, int m, int base); Requirement: C++ programing; program an addition algorithm function that can both accept binary and decimal addition; convert each other if necessary. ................................................................................................................................................................................................................

Requirement:

1. Write a string addFunction that both can accept two positive binary numbers and decimal numbers in string.

2. The example function prototype for addition function below where accepting two numbers,m, and base as input; output the addition result as string type as return value; base should be 2 or 10 as required.

3. Output needs as binary string.

4. Example for String addFuntion(string numberA, string number B, int m, int base).

Program language: C++

Solutions

Expert Solution

SOLUTION-

I have given the c++ code below with commands for easy understanding and given screenshot also :)

CODE-

// main program
#include<iostream>
using namespace std;

string addFunction(string numberA, string numberB, int m, int base) //function to get the binary number in string
{
   string sol="";   //print the result
   int carry=0;

   int indexA=numberA.length()-1; //Store last index of numberA in indexA
   int indexB=numberB.length()-1; //Store last index of numberB in indexB

   while(indexA>=0 || indexB>=0) //Loop untill both the strings finished
   {
       int a = (indexA>=0) ? numberA[indexA]-'0' : 0; //If theres is a bit in numberA then take it, 0 other
     
       int b = (indexB>=0) ? numberB[indexB]-'0' : 0; //If theres is a bit in numberB then take it, 0 other
    
       int sum = (a^b^carry);
       //Find carry
       carry = ((a & b) | (a & carry) | (b & carry));
       //Store the sum into result string
       sol=char(sum+48)+sol;
       //Decrement both the indiecs
       indexA--;
       indexB--;
   }
   //If there is a carry then add append 1 to the front
   sol=(carry==1)?"1"+sol:sol;

   return sol;
}

int main()
{
   //Function calling
   cout<<"Addition:"<<addFunction("1001","100001101",4,2)<<endl;

   cout<<"Addition:"<<addFunction("1111","11111",5,2)<<endl;
   cout<<"Addition:"<<addFunction("1000","0100",4,2)<<endl;
   return 0;
}

SCREENSHOT-

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
Write a program in python language, which accepts 2 numbers and a + sign (for addition)...
Write a program in python language, which accepts 2 numbers and a + sign (for addition) A sign - (for subtraction) A sign * (for multiplication), / (for division) Then calculate and to display the result of the operation he chose with the two numbers. Displaying the appropriate message
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Write a function that accepts two arguments (say a and b) by value and one argument...
Write a function that accepts two arguments (say a and b) by value and one argument (say c) by reference. Then it calculates the multiplication of all the numbers between a and b (inclusive) and saves the results in c. The function does not return any value.
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT