I need to write a program that takes in two big integers from the user and puts them into a dynamic array so that we can add them together. It says to use "paper-and-pencil" mehtod meaning to pretty much reverse the dynamic arrays and carry the ones over as you go. Thats where I am stuck, how can I carry over the ones? Here is what I have so far:
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
int* Allocate(int& numDigits);
void Init(int* array, int& numDigits);
void inputLargeInt(int* array, int numDigits, int& sizeOfNum);
void Reverse(int entry[], int* array, int numDigits, int& sizeOfNum);
void Add(int* Num1, int& sizeOfA, int* Num2, int& sizeOfB, int* sum, int& sizeSum, int digits);
void Output(int* array, int numDigits);
int main()
{
int size;
char ans;
int *Num1, *Num2, *sum;
int sizeOfNum1, sizeOfNum2, sizeSum;
do
{
cout << "How many integers would you like per entry? ";
cin >> size;
Num1 = Allocate(size);
Num2 = Allocate(size);
Init(Num1, size);
Init(Num2, size);
inputLargeInt(Num1, size, sizeOfNum1);
inputLargeInt(Num2, size, sizeOfNum2);
Add(Num1, sizeOfNum1, Num2, sizeOfNum2, sum, sizeSum, size);
cout << " ";
Output(Num1, sizeOfNum1);
cout << "+ ";
Output(Num2, sizeOfNum2);
for (int i = 0; i < size + 2; i++)
{
cout << "*";
}
cout << endl;
Output(sum, size);
cout << endl;
cout << "\n\nWould you like to add two more numbers? (y or n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
delete [] Num1;
delete [] Num2;
delete [] sum;
system("PAUSE");
return 0;
}
int* Allocate(int& numDigits)
{
int* hold;
hold = new int[numDigits];
return hold;
}
void Init(int* array, int& numDigits)
{
for (int i = 0; i < numDigits; i++)
{
*array = 0;
array++;
}
}
void inputLargeInt(int* array, int numDigits, int& sizeOfNum)
{
char entry[numDigits];
char change;
int i = 0;
cout << "Please enter a positive integer with no more than " << numDigits
<< " numDigits: ";
cin.get(change);
if (change == '\n')
cin.get(change);
while (isdigit(change) && i < numDigits)
{
entry[i] = change;
i++;
cin.get(change);
}
/*if (i < numDigits)
{
for (int k = i; k < i + (numDigits - i); k--)
{
entry[k + (numDigits - 1)] = entry[k];
}
}*/
sizeOfNum = i;
int j = 0;
while (i > 0)
{
i--; //Reverses the order so that it
*array = entry[i] - '0'; //add paper-and-pencil method.
array++;
}
}
void Add(int* Num1, int& sizeOfA, int* Num2, int& sizeOfB, int* sum, int& sizeSum, int digits)
{
for(int i = 0; i < digits - 2; i++)
{
*sum += (*Num1 + *Num2);
sum++;
*sum += (*Num1 + *Num2) % 10;
cout << *sum << endl;
Num1++;
Num2++;
}
}
void Output(int* array, int numDigits)
{
for (int i = 0; i < numDigits-1; i++)
{
array++;
}
for (int j = 0; j < numDigits; j++)
{
cout << *array;
array--;
}
cout << endl;
}