Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Adding Large Integers: In C++, the largest int value is 2147483627. So, an integ

ID: 3622242 • Letter: A

Question

Adding Large Integers: In C++, the largest int value is 2147483627. 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 2147483627, 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 a string and store the digits of the number in the reverse order.]
Your program must also check the improper input -- such as negative integer, integer with more than 20 digits -- then issue an error message, provide users chances to correct it.

PLEASE HELP!!!

Explanation / Answer

please rate - thanks

#include<iostream>
using namespace std;
int getnum(string mess,int[]);
int add(int [], int [],int[] ,int,int);
void print(int a[],int);
bool checkinput(string);
int main()
{int a[21];
int b[21];
int c[51];
int i, lena,lenb,lenc;
lena=getnum("first number: ",a);
cout<<endl;
lenb=getnum("second number: ",b);
lenc=add(a,b,c,lena,lenb);
cout<<"    ";
print(a,lena);
cout<<"+ ";
print(b,lenb);
for(i=0;i<lenc+5;i++)
    cout<<"-";
cout<<"     ";
print(c,lenc);
system("pause");
return 0;
}
bool checkinput(string in)
{int i;
if(in.length()>20)
      return false;
for(i=0;i<in.length();i++)
     if(in[i]<'0'||in[i]>'9')
          {cout<<"invalid input ";
          return false;
          }
return true;
}
int getnum(string mess,int a[])
{int i;
for(i=0;i<21;i++)
    a[i]=0;
string input;
cout<<"Enter "<<mess;
cin>>input;
while(!checkinput(input))
    {cout<<"Enter "<<mess;
     cin>>input;
     }
for(i=0;i<input.length();i++)
     a[input.length()-1-i]=input[i]-'0';
return input.length();
}
int add(int a[], int b[],int c[],int lena,int lenb)
{int i,n,carry=0,d;
cout<<endl;
for(i=0;i<21;i++)
    c[i]=0;
if(lena>lenb)
    n=lena;
else
    n=lenb;
   
for(i=0;i<n;i++)
    {d=a[i]+b[i]+carry;
      c[i]=d%10;
      carry=d/10;
      }
if(carry==0)
   return n;
else
   c[i]=carry;
    
return n+1;
}
void print(int a[],int len)
{int i;
for(i=len-1;i>=0;i--)
       cout<<a[i];
cout<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote