The largest magnitude of integers is limited, so we are not able to add 18,274,3
ID: 3711302 • Letter: T
Question
The largest magnitude of integers is limited, so we are not able to add 18,274,364,583,929,273,748,459,595,684,373 and 8,129,498,165,026,026,350,236(1)(notation),since integer variables cannot hold such large values, let alone their sum. The program can be solved if we treat these numbers as sequences of numerals, store the numbers corresponding to these numerals on two stacks, and then perform addition by popping numbers from the stacks. The pseudocode for this algorithm is:
addingLargeNumebrs():
read the numerals of the first number and store them on one stack;
read the numerals of the second numbers and store them on another stack;?result = 0;
while at least one stack is not empty;pop a number from each non-empty stack and add them to result;
store carry in result;push carry on the result stack if it is not zero;
pop numbers from the result stack and display them
Write a modular C++ program to perform the four basic arithmetic operations, +,-, X, and / on very large integers; the result of division should also be an integer. Use the std::stack template class in design.
Apply these operations to compute the following:
1) 123(45) ( to the 45th)
2) The hundredth number in the sequence(1*2(to the 1st) + 3),(2*3(to the 2nd)+4),(3*4(to the 3rd) + 5),......
Explanation / Answer
ANS:-
PROGRAM:-
/*
* largeNumbers.cpp
*
* Created on: 19-Mar-2017
* Author: kasturi
*/
#include <iostream>
#include <vector>
using namespace std;
void addLargeNumbers(const char *pNum1, const char *pNum2);
int main()
{
addLargeNumbers("134567892", "7634956");
addLargeNumbers("235682760926", "1824986235");
addLargeNumbers("99999999999923", "888888888888843");
return 0;
}
void addLargeNumbers(const char *pNum1, const char *pNum2)
{
vector<int> v1, v2, resultStack;
int result = 0, counter=0;
if(strlen(pNum1) > strlen(pNum2))
{
counter = strlen(pNum1);
for(int i=strlen(pNum2); i<counter; i++)
v2.push_back(0);
}
else if(strlen(pNum2) > strlen(pNum1))
{
counter = strlen(pNum2);
for(int i=strlen(pNum1); i<counter; i++)
v1.push_back(0);
}
else
counter = strlen(pNum1);
for(int i=0; i<strlen(pNum1); i++)
{
v1.push_back(pNum1[i] - '0');
}
for(int i=0; i<strlen(pNum2); i++)
{
v2.push_back(pNum2[i] - '0');
}
while(!v1.empty() && !v2.empty())
{
//cout<<v1[counter-1]<<" "<<v2[counter-1]<<endl;
result += v1[counter-1]+ v2[counter-1];
resultStack.push_back(result%10);
result = result/10;
v1.pop_back();
v2.pop_back();
counter--;
}
if(result > 0)
resultStack.push_back(result);
cout<<"Adding "<<pNum1<< " and "<<pNum2<<endl;
cout<<"Result: ";
for(int i=resultStack.size()-1; i>=0; i--)
{
cout<<resultStack[i];
}
cout<<endl<<endl;
}
OUTPUT:
Adding 134567892 and 7634956
Result: 142202848
Adding 235682760926 and 1824986235
Result: 237507747161
Adding 99999999999923 and 888888888888843
Result: 988888888888766
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.