Question

function add(A, B): while B is greater than 0: U = A XOR B V =...


function add(A, B):
    while B is greater than 0:
        U = A XOR B
        V = A AND B
        A = U
        B = V * 2
    return A 

The above is an algorithm to add two non-negative integers A and B.

A and B are binary numbers. They have been inputted as strings.

Given the initial values of A and B write an efficient C++ program to find out the number of times the while loop of the algorithm is repeated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>

using namespace std;

int cnt;

string add(string a, string b)//the function to add the two binary numbers

{

bool ok=false;//this will store the condition for the while loop

for(int i=0;i<b.size();i++)//ok will be true if string b contains atleast one zero

if(b[i]=='1')

{

ok=true;

break;

}

if(a.size()>b.size())//if the two strings are of different size then making them of same size by adding zeroes before them

{

int diff=a.size()-b.size();

for(int i=0;i<diff;i++)b='0'+b;

}

else if(a.size()<b.size())

{

int diff=b.size()-a.size();

for(int i=0;i<diff;i++)a='0'+a;

}

while(ok)

{

cnt++;

//in this while loop we are doing what we are asked to do

string u="";

string v="";

int na=a.size();

int nb=b.size();

int offset=0;

if(nb>na)//if size of b has become greater because of carry

{

u+=b[0];

offset=1;

}

for(int i=0;i<na;i++)

{

if(a[i]=='1'&&b[i+offset]=='1')//for and, xor operations

{

v+='1';//and

u+='0';//xor

}

else if(a[i]=='1'||b[i+offset]=='1')

{

u+='1';//xor

v+='0';//and

}

else

{

u+='0';//xor

v+='0';//and

}

}

a=u;

b=v+'0';//to multiply v by two we need to add a zero to the string

ok=false;

for(int i=0;i<b.size();i++)if(b[i]=='1')//checking if there is a one in string b

{

ok=true;

break;

}

}

return a;//returning the answer

}

int main()

{

string a, b;

cin>>a>>b;

cnt=0;

//printing the answers

cout<<"Sum is : "<<add(a, b)<<endl;

cout<<"No. of times loop ran is "<<cnt<<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
function add(A, B): while B is greater than 0: U = A XOR B V =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a function that takes a list and an integer v, and returns True if v...

    Create a function that takes a list and an integer v, and returns True if v is in the list (False otherwise). The function should be efficient and stop searching as soon as possible. •The main program must generate a very large list with random elements, call the function to search a value and display the result. Add in the function a variable Nsteps to count the number of steps used by the algorithm (number of times the loop is...

  • Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value...

    Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value stored in variable v at the end of the execution of the loop could best be described as the sum of all positive numbers scanned from the terminal the number of times the loop executed the sum of all non-positive numbers scanned from the terminal the number of positive integers scanned from the terminal the number of negative integers scanned from the terminal

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • The least common multiple (lcm) of two positive integers u and v is the smallest positive...

    The least common multiple (lcm) of two positive integers u and v is the smallest positive integer that is evenly divisible by both u and v. Thus, the lcm of 15 and 10, written lcm (15,10), is 30 because 30 is the smallest integer divisible by both 15 and 10. Write a function lcm() that takes two integer arguments and returns their lcm. The lcm() functon should calculate the least common multiple by calling the gcd() function from program 7.6...

  • Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

    Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • urgent Help needed in python program ! Thanx # This is a function definition. You can...

    urgent Help needed in python program ! Thanx # This is a function definition. You can ignore this for now. def parse_integer_list_from_string(string): """ Returns a list of integers from a string containing integers separated by spaces. """ # Split the line into a list of strings, each containing a number. number_string_list = string.split() # Create an empty list to store the numbers as integers. numbers = [] # Convert each string to an integer and add it to the list...

  • Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its...

    Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its decimal number representation. Write a main program to test the function. Your function should have two arguments - the roman number as a string, and an error processing function. Write a helper function that will return the numeric value of each of the letters used in roman numbers. Then convert the string argument as follows look at the first two characters. If the first...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Hi, I need help with this practice problem! Chapter 2 Algorithm Discovery and Design 64C FIGURE...

    Hi, I need help with this practice problem! Chapter 2 Algorithm Discovery and Design 64C FIGURE 2.10 Get values for a and b If (either a 0orb 0) then Set the value of product to o se Set the value of count to 0 Set the value of product to 0 While (count <b) do Set the value of product to (product+ a) Set the value of count to (count + 1) End of loop Print the value of product...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT