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 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.
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b)...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b) { a = a - b; b = a + b; a = b - a; } However, it fails in one important case, namely when calling tricky_swap(x, x). Explain what should happen and what actually happens.
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
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   ...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else {...
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else { System.out.println("B"); } } else if ((b<0)||(a<0)) { System.out.println("C"); } else { System.out.println("D"); }
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the...
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the index of wanted letter in string. For example, strfind("hello world", 'o') would return 4. If the character is not found, the funtion returns -1. Please code in c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT