Question

In: Computer Science

CS 211 Q.Adding BIG Integers In C++ an int is in the range 0 to 65535....

CS 211

Q.Adding BIG Integers In C++ an int is in the range 0 to 65535. But what if we need to add larger integers? Say we want to compute the sum 2345566777844567+ 9999988777765768009998. Your task in this assignment is to make this happen. Write a function string add(string a, string b)

Solutions

Expert Solution

Hi, Please find the solution, Explanations are inline.

//

// main.cpp

// HugeNumbersAdd

//

#include <iostream>

#include "string"

#include "algorithm"

using namespace std;

string add(string a, string b){

if(a.length()>b.length()){

string temp = a;

a=b;

b=temp;

}

string res = "";//empty for result

int len1 = a.length();

int len2 = b.length();

reverse(a.begin(), a.end());

reverse(b.begin(),b.end());

  

int carry = 0;

  

for (int i=0; i<len1; i++) {//adding until the size of the lower number

int sum = ((a[i]-'0')+(b[i]-'0')+carry);//-'0' is to convert char to number

res.push_back(sum%10+'0');

//carry for next step

carry = sum/10;

}

for(int i=len1;i<len2;i++){//this loop is after we are done with addition and if carry remains

int sum = ((b[i]-'0')+carry);

res.push_back(sum%10+'0');

carry = sum/10;

}

if(carry){//finally if carry remains

res.push_back(carry+'0');

}

reverse(res.begin(), res.end());//finally reversing

return res;

  

}

int main(int argc, const char * argv[]) {

string str1="122325",str2="45468";

cout<<"Enter big number 1:";

cin>>str1;

cout<<endl<<"Enter big number 2:";

cin>>str2;

cout<<add(str1, str2)<<endl;

}


Related Solutions

Write a program IN C that reads all integers that are in the range of 0...
Write a program IN C that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a number...
write a c++ program in which for loop iterates over all integers in the range 0...
write a c++ program in which for loop iterates over all integers in the range 0 to 99 inclusive and prints out all of those numbers dividible by both 2 and 5.
Plase, write program in c++. IP address consists of four integers of range [0, 255] separated...
Plase, write program in c++. IP address consists of four integers of range [0, 255] separated by dots. The next three rows show three correct IP-address: 130.0.0.0 193.197.0.01 255.00.255.255 Write a program that determines whether a given string is a valid IP-address. outpot should be 1 if given IP address is valid, or 0 otherwise.
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13 count: 5 pid: 13342...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT