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; 
}

Recommended Answers

All 7 Replies

You need a carry variable to store the addition of *Num1 + *Num2. then you need to add carry % 10 to sum. After that you need to dived carry by 10 to get what needs to be carried over to the next addition. You shout get something like this.

int carry = 0;
// in loop
carry = carry + *Num1 + *Num2;
*sum = carry % 10;
carry /= 10;
// in loop

hello Nathan, what does line 5 mean?

Line 5 means

carry = carry / 10;

What NathanOliver means is you need to add the numbers and then keep the modulo 10 of it in answer and carry the divided by 10 value.

Suppose 67 + 85

7 + 5 = 12

Keep 12 % 10 = 2 in answer

Carry 12 / 10 = 1 ( as 12 / 10 = 1.2 and since we are dealing in integer it rounds of to 1 )

6 + 8 = 14

14 + carry = 15

Ans : 152

It's shorthand for:

carry = carry / 10;

You can use it for many operators, see the Compound assignment section of this article.

Real quick can some explain to me why this *array = entry[i] - '0'; converts charaters to integers. Does it have to do with ASCII?

Yes. You are subtracting ascii value of char with ascii value of '0'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.