In C++, the largest int value is 2147483647. So, an integer larger than this can
ID: 3554307 • Letter: I
Question
In C++, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Write a program that inputs two positive integers of, at most, 20 digits and outputs the sum of the numbers. If the sum of the numbers has more than 20 digits, output the sum with an appropriate message. Your program must, at least, contain a function to read and store a number into an array and another function to output the sum of the numbers. (Hint: Read numbers as strings and store the digits of the number in the reverse order.)
I need help please! I am using Microsoft Visual 2010, C++.
Thank you!
Explanation / Answer
//Header file for I/O operations.
#include <iostream>
//Header for string operations.
#include <string>
using namespace std;
//function prototypes.
int readNumbers(int[]);
int addNumbers(int[], int[], int[], int, int);
void display(int[], int);
//Function to read a string and convert it into array of digits.
int readNumbers(int num[20])
{
string number;
//reading string of numbers from the user.
getline(cin, number);
//loop to iterate through each character of the string.
//and converts it into numbers and stores it in array.
for (int i=0;i<(int)number.length();i++)
num[i]=(int)number[number.length()-1-i]-48;
//return the number of digits.
return number.length();
}
//function to add two large integers stored in two arrays.
int addNumbers(int a[20], int b[20], int sum[20], int la, int lb)
{
int carry=0,length, temp,i;
//find the biggest length among two integers.
if (la>lb)
length = la;
else
length = lb;
//loop to iterate and compute sum of individual digits.
for(i=0;i<length;i++)
{
temp=a[i]+b[i]+carry;
//compute the digit and store.
sum[i]=temp%10;
//compute carry and store.
carry=temp/10;
}
//return the length of the large integer obtained after addition.
if(carry==0)
return length;
else
sum[i]=carry;
return length+1;
}
//Function to display the large integer.
void display(int a[20], int n)
{
//Loop to display the array of integers.
for(int i=n-1;i>-1;i--)
cout<<a[i];
}
//main function.
int main()
{
//Varible declarations and initialization.
int numOne[20]={0}, numTwo[20]={0}, sum[21]={0};
int lenNum1, lenNum2, sumLen;
//prompt the user to enter the first large integer.
cout<<"Enter first positive integer (at most 20 digits) : ";
//The function readNumbers() is called twice to read two large integers.
//The function reads the string of digits and stores each digit in an
//integer array.
//calling function to read large integer.
lenNum1 = readNumbers(numOne);
//prompt the user to enter the second large integer.
cout<<"Enter second positive integer (atmost 20 digits): ";
//calling function to read large integer.
lenNum2 = readNumbers(numTwo);
cout<<endl<<endl;
cout<<"First large integer : ";
display(numOne, lenNum1);
cout<<endl<<"Second large integer : ";
display(numTwo, lenNum2);
//The function addNumbers() is called to add the two numbers and
//store it in the new array.
sumLen = addNumbers(numOne, numTwo, sum, lenNum1, lenNum2);
//Based on the length of the result, display it with
//message. Display the sum of the two large integers.
if(sumLen<=20)
{
cout<<endl<<endl<<"Sum of the two large integers : ";
display(sum, sumLen);
}
else
{
cout<<endl<<endl<<"Result of sum of the two large "
<<"integers has 21 digits..."<<endl;
cout<<"Sum of the two large integers : ";
display(sum, sumLen);
}
cout<<endl<<endl;
//to halt the system.
system("Pause");
}
Sample Output:
Run 1:
Enter first positive integer (at most 20 digits): 12345678987654321
Enter second positive integer (at most 20 digits): 2345678912345
First large integer: 12345678987654321
Second large integer: 2345678912345
Sum of the two large integers: 12348024666566666
Press any key to continue . . .
Run 2:
Enter first positive integer (at most 20 digits) : 6754738672834567829764
Enter second positive integer (at most 20 digits): 2765846738475643893457685
First large integer: 6754738672834567829276
Second large integer: 2765846738475643893457685
Result of sum of the two large integers has 21 digits...