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

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.
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...
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.
In Java, we typically iterate over a sequence of integers as follows: for (int i=0; i<10;...
In Java, we typically iterate over a sequence of integers as follows: for (int i=0; i<10; i = i + 2) { ... } Suppose we use a version of Java that only supports for-each loops, i.e. it only allows you to iterate through the elements of an Iterable In order to still be able to iterate over a sequence of numbers, you need to create such an Iterable, which we will call Range. Modify the class Range.java so that...
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   ...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT