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

My Problem: I am adding, subtracting, and multiplying two big integers together

ID: 3818089 • Letter: M

Question

My Problem:
I am adding, subtracting, and multiplying two big integers together in C++.
I get it to subtract and multiply two integers, but when I do, I get either a wrong number in subtraction or I am missing a number in multiplication.

Also, when I try to get the number to be larger than 20, it ends up skipping the input for the sign and the second expression. I have tried to use long and have tried double, but I end up getting the same issue.

The last issue that I have is when I enter an expression with a letter, it ends up adding the numbers up to the letter and then asking for a sign an expression to add the number after the letter entered.
Output:

Here is my code:
#include <iostream>
#include <cctype>
using namespace std;

const int Max_Digits = 20;

void Input_Big_Integer(double a[], double& Size_A, double b[], double& Size_B);
void Output_Big_Integer(double a[], double Size_A);
void Output_Big_Integer2(double b[], double Size_B);
void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum);
void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff);
void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod);

void Input_Big_Integer(double a[], double& Size_A)
{
   char digit[Max_Digits];
   char change;
   int i=0;
   cout<<"Enter an expression: "<<endl;
   cin.get(change);
   if(change ==' ')
   {
       cin.get(change);
   }
   while (isdigit(change) && i<Max_Digits)
   {
       digit[i]=change;
       i++;
       cin.get(change);
   }
   Size_A = i;
   int j=0;
   while (i>0)
   {
       i--;
       a[j]=digit[i] - '0';
       j++;
   }
}

void Output_Big_Integer(double a[], double Size_A)
{
   for(int i=0; i<Size_A; i++)
   {
       cout<<a[(int)Size_A - i - 1];
   }
}
void Output_Big_Integer2(double b[], double Size_B)
{
   for(int i=0; i<Size_B; i++)
   {
       cout<<b[(int)Size_B - i - 1];
   }
}

void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum)
{
   double carry=0;
   double maxsize=(Size_A>Size_B)?Size_A:Size_B;
   for(int i=0; i<maxsize; i++)
   {
       sum[i] = (a[i] + b[i] + carry);
       sum[i] = (long)sum[i] %10;
       carry = (a[i] + b[i] + carry) / 10;
   }
   if (maxsize==20 && carry>0)
   {
       cout<<"Integer overflow."<<endl;
   }
   else if (maxsize>20)
   {
       cout<<"Invalid operand (too large)."<<endl;
   }
   else if (isdigit(Size_A) && isdigit(Size_B))
   {
       cout<<"Invalid operand (bad digit)."<<endl;
   }
   else if (carry>0)
   {
       Size_Sum = (Size_A>Size_B)?Size_A:Size_B +1;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " + ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(sum, Size_Sum);
       }
   }
   else
   {
       Size_Sum = (Size_A>Size_B)?Size_A:Size_B;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " + ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(sum, Size_Sum);
       }
   }
}
void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff)
{
   double borrow=0;
   double maxsize=(Size_A>Size_B)?Size_A:Size_B;
   for(int i=maxsize;i>=0; i--)
   {
       if(borrow==0)
       {
           if (a[i]>=b[i])
           {
               diff[i] = a[i] - b[i];
           }
           else
           {
               borrow = 1;
               diff[i] = (a[i]+10) - b[i];
           }
       }
       else
       {
           borrow = 0;
           if((a[i]-1)>=b[i])
           {
               diff[i] = (a[i]-1) - b[i];
           }
       }
   }
  
   if (maxsize==20 && borrow>0)
   {
       cout<<"Integer overflow."<<endl;
   }
   else if (maxsize>20 && borrow>0)
   {
       cout<<"Invalid operand (too large)."<<endl;
   }
   else if (isdigit(Size_A) && isdigit(Size_B))
   {
       cout<<"Invalid operand (bad digit)."<<endl;
   }
   else if (borrow>0)
   {
       Size_Diff = (Size_A>Size_B)?Size_A:Size_B +1;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " - ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(diff, Size_Diff);
       }
   }
   else
   {
       Size_Diff = (Size_A>Size_B)?Size_A:Size_B ;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " - ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(diff, Size_Diff);
       }
   }
}

void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod)
{
   long tmp;
   long maxsize=(Size_A>Size_B)?Size_A:Size_B;
  
    for(int i=0;i < maxsize;i++)
    {
        for(int j=0;j < maxsize;j++)
        {
            prod[i+j] += b[i]*a[j];
        }
    }
    for(int i=0;i < maxsize;i++)
    {
        int tmp = prod[i]/10;
        prod[i] = (int)prod[i]%10;
        prod[i+1] = prod[i+1] + tmp;
    }
    for(int i=maxsize; i>= 0;i--)
    {
        if(prod[i] > 0)
            break;
    }
   if (maxsize==20 && tmp>0)
   {
       cout<<"Integer overflow."<<endl;
   }
   else if (maxsize>20)
   {
       cout<<"Invalid operand (too large)."<<endl;
   }
   else if (isdigit(Size_A) && isdigit(Size_B))
   {
       cout<<"Invalid operand (bad digit)."<<endl;
   }
   else if (tmp>0)
   {
       Size_Prod = (Size_A>Size_B)?Size_A:Size_B +1;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " * ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(prod, Size_Prod);
       }
   }
   else
   {
       Size_Prod = (Size_A>Size_B)?Size_A:Size_B;
       {
           Output_Big_Integer(a, Size_A);
            cout<< " * ";
            Output_Big_Integer2(b, Size_B);
            cout<<" = ";
            Output_Big_Integer(prod, Size_Prod);
       }
   }
}
int main()
{
   double a[Max_Digits], b[Max_Digits], sum[Max_Digits], diff[Max_Digits], prod[Max_Digits];
   double Size_A, Size_B, Size_Sum, Size_Diff, Size_Prod;
   char sign;
  
   for (int i=0; i<Max_Digits; i++)
   {
       a[i] = 0;
       b[i] = 0;
       sum[i] = 0;
       prod[i] = 0;
   }
   for (int i=Max_Digits;i>=0; i--)
   {
       a[i] = 0;
       b[i] = 0;
       diff[i] = 0;
   }
  
   Input_Big_Integer(a, Size_A);
   cout<<"Enter a sign"<<endl;
   cin>>sign;
   Input_Big_Integer (b, Size_B);
   while (Size_A!=0 && sign!='%' && Size_B!=0)
   {
       if (sign == '+' )
        {
           add(a, Size_A, b, Size_B, sum, Size_Sum);
       }
       if (sign == '-')
       {
           subtract(a, Size_A, b, Size_B, diff, Size_Diff);
       }
       if (sign == '*')
       {
           multiply(a, Size_A, b, Size_B, prod, Size_Prod);
       }
       cout<<endl;
       Input_Big_Integer(a, Size_A);
        cout<<"Enter a sign"<<endl;
        cin>>sign;
        Input_Big_Integer (b, Size_B);
   }
   cout<<"Thanks for using my program. Good bye!"<<endl;
   return 0;
}

Explanation / Answer

#include <iostream>
#include <cctype>
using namespace std;
const int Max_Digits = 20;
void Input_Big_Integer(double a[], double& Size_A, double b[], double& Size_B);
void Output_Big_Integer(double a[], double Size_A);
void Output_Big_Integer2(double b[], double Size_B);
void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum);
void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff);
void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod);
void Input_Big_Integer(double a[], double& Size_A)
{
char digit[Max_Digits];
char change;
int i=0;
cout<<"Enter an expression: "<<endl;
cin.get(change);
if(change ==' ')
{
cin.get(change);
}
  
Size_A = i;
int j=0;
while (i>0)
{
i--;
a[j]=digit[i] - '0';
j++;
}
}
void Output_Big_Integer(double a[], double Size_A)
{
for(int i=0; i<Size_A; i++)
{
cout<<a[(int)Size_A - i - 1];
}
}
void Output_Big_Integer2(double b[], double Size_B)
{
for(int i=0; i<Size_B; i++)
{
cout<<b[(int)Size_B - i - 1];
}
}
void add(double a[], double& Size_A, double b[], double& Size_B, double sum[], double& Size_Sum)
{
double carry=0;
double maxsize=(Size_A>Size_B)?Size_A:Size_B;
for(int i=0; i<maxsize; i++)
{
sum[i] = (a[i] + b[i] + carry);
sum[i] = (long)sum[i] %10;
carry = (a[i] + b[i] + carry) / 10;
}
if (maxsize==20 && carry>0)
{
cout<<"Integer overflow."<<endl;
}
else if (maxsize>20)
{
cout<<"Invalid operand (too large)."<<endl;
}
else if (isdigit(Size_A) && isdigit(Size_B))
{
cout<<"Invalid operand (bad digit)."<<endl;
}
else if (carry>0)
{
Size_Sum = (Size_A>Size_B)?Size_A:Size_B +1;
{
Output_Big_Integer(a, Size_A);
cout<< " + ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(sum, Size_Sum);
}
}
else
{
Size_Sum = (Size_A>Size_B)?Size_A:Size_B;
{
Output_Big_Integer(a, Size_A);
cout<< " + ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(sum, Size_Sum);
}
}
}
void subtract(double a[], double& Size_A, double b[], double& Size_B, double diff[], double& Size_Diff)
{
double borrow=0;
double maxsize=(Size_A>Size_B)?Size_A:Size_B;
for(int i=maxsize;i>=0; i--)
{
if(borrow==0)
{
if (a[i]>=b[i])
{
diff[i] = a[i] - b[i];
}
else
{
borrow = 1;
diff[i] = (a[i]+10) - b[i];
}
}
else
{
borrow = 0;
if((a[i]-1)>=b[i])
{
diff[i] = (a[i]-1) - b[i];
}
}
}
  
if (maxsize==20 && borrow>0)
{
cout<<"Integer overflow."<<endl;
}
else if (maxsize>20 && borrow>0)
{
cout<<"Invalid operand (too large)."<<endl;
}
else if (isdigit(Size_A) && isdigit(Size_B))
{
cout<<"Invalid operand (bad digit)."<<endl;
}
else if (borrow>0)
{
Size_Diff = (Size_A>Size_B)?Size_A:Size_B +1;
{
Output_Big_Integer(a, Size_A);
cout<< " - ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(diff, Size_Diff);
}
}
else
{
Size_Diff = (Size_A>Size_B)?Size_A:Size_B ;
{
Output_Big_Integer(a, Size_A);
cout<< " - ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(diff, Size_Diff);
}
}
}
void multiply(double a[], double& Size_A, double b[], double& Size_B, double prod[], double& Size_Prod)
{
long tmp;
long maxsize=(Size_A>Size_B)?Size_A:Size_B;
  
for(int i=0;i < maxsize;i++)
{
for(int j=0;j < maxsize;j++)
{
prod[i+j] += b[i]*a[j];
}
}
for(int i=0;i < maxsize;i++)
{
int tmp = prod[i]/10;
prod[i] = (int)prod[i]%10;
prod[i+1] = prod[i+1] + tmp;
}
for(int i=maxsize; i>= 0;i--)
{
if(prod[i] > 0)
break;
}
if (maxsize==20 && tmp>0)
{
cout<<"Integer overflow."<<endl;
}
else if (maxsize>20)
{
cout<<"Invalid operand (too large)."<<endl;
}
else if (isdigit(Size_A) && isdigit(Size_B))
{
cout<<"Invalid operand (bad digit)."<<endl;
}
else if (tmp>0)
{
Size_Prod = (Size_A>Size_B)?Size_A:Size_B +1;
{
Output_Big_Integer(a, Size_A);
cout<< " * ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(prod, Size_Prod);
}
}
else
{
Size_Prod = (Size_A>Size_B)?Size_A:Size_B;
{
Output_Big_Integer(a, Size_A);
cout<< " * ";
Output_Big_Integer2(b, Size_B);
cout<<" = ";
Output_Big_Integer(prod, Size_Prod);
}
}
}
int main()
{
cout<<"HI"<<endl;
double a[Max_Digits], b[Max_Digits], sum[Max_Digits], diff[Max_Digits], prod[Max_Digits];
double Size_A, Size_B, Size_Sum, Size_Diff, Size_Prod;
char sign;
  
for (int i=0; i<Max_Digits; i++)
{
a[i] = 0;
b[i] = 0;
sum[i] = 0;
prod[i] = 0;
}
for (int i=Max_Digits;i>=0; i--)
{
a[i] = 0;
b[i] = 0;
diff[i] = 0;
}
  
Input_Big_Integer(a, Size_A);
cout<<"Enter a sign"<<endl;
cin>>sign;
Input_Big_Integer (b, Size_B);
while (Size_A!=0 && sign!='%' && Size_B!=0)
{
if (sign == '+' )
{
add(a, Size_A, b, Size_B, sum, Size_Sum);
}
if (sign == '-')
{
subtract(a, Size_A, b, Size_B, diff, Size_Diff);
}
if (sign == '*')
{
multiply(a, Size_A, b, Size_B, prod, Size_Prod);
}
cout<<endl;
Input_Big_Integer(a, Size_A);
cout<<"Enter a sign"<<endl;
cin>>sign;
Input_Big_Integer (b, Size_B);
}
cout<<"Thanks for using my program. Good bye!"<<endl;
return 0;
}

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