# Thread: Hey guys, back again with a question

1. ## Hey guys, back again with a question

All right, so this time I have an assignment to add two positive integers up to 20 digits long and output the two entries along with the sum. To this point I've been able to get the program to compile but the output is out of whack. Here's what I've got, and an example of the output:

Code:
#include <iostream>
using namespace std;
int main()
{
char augend[20] = {0}, addend[20] = {0}, sum[21], temp[20];
int aug = 0, add = 0, sumc = 20, tempc = 0;
int carry = 1;

cout << "This program adds two positive integers up to 20 digits long."
<< "\n\n";
cout << "Enter the augend: ";
cin >> noskipws >> temp[0];
while(temp[tempc]!='\n')
{
tempc++;
cin >> temp[tempc];
}
cout << "Enter the addend: ";
{
}
for(aug=tempc; aug>=0; aug--, tempc--)
{
augend[aug] = temp[tempc];
}
{
{
sum[sumc - 1] = carry;
}
else
{
}
}
temp[tempc]='\0';
cout << "\n\n" << temp << "+" << addend << "=" << sum << ".\n";
return 0;
}
And a sample output:

Code:
This program adds two positive integers up to 20 digits long.

Enter the augend: 2021

2021
+395
= R@.
All right, what's screwy here? I have a feeling part of it is due to the addition of the two arrays (augend and addend) - I'm not confident I've done that right. Advice/assistance?

2. Could you say step by step what you are trying to do?

I really don't understand what your trying to do.

3. Sure... at this point in my class (well, for this homework assignment, really), it's an emphasis on arrays. What the assignment is asking is for us to do the following...

1) Read in up to a 20-digit positive integer into the "temp" (first number) array.
2) Read in up to a 20-digit positive integer into the "addend" (second number) array.
3) Shift the "temp" array into an "augend" array which allows for easier addition. If it's a three digit number, then it's akin to taking the 0, 1, 2 places of the "temp" array and putting them into the 17, 18, 19 places (respectively) of the "augend" array.
4) Add the individual digits of the "augend" and "addend" arrays place by place (i.e., augend[19]+addend[19]) and putting them into a "sum" array.
5) Display the augend and addend arrays in the output with the sum array.

Example:

temp[0] = 2, temp[1] = 3, temp[2] = 4, temp[3] = 5. Once the shift is done, it would be: augend[16] = 2, augend[17] = 3, augend[18] = 4, augend[19] = 5.

augend[16] = sum[17]

Of course another wrinkle is the use of "carry" to aid in carrying over the 1 that comes from things like 8+5 and 7+4.

4. And what is the error your getting?

5. Well, look at the sample output above. I put in 2021 and 395 ... and the result is R@?

6. 1) You have wrong initialization. This just makes the first char to 0. You want everything in the array to 0. Actually you want everything in the array to '0' not 0!
2) Shouldn't it be augend[aug] + addend[add] >= '10' //'10' not 10 ?
3) Shouldn't it be sum[sumc] += augend[aug] + addend[add] - 10; // += not = since you might have a carry ?
4) Shouldn't you initialize sum to '0'?

7. Well I've made a few changes and still am getting an error upon output. Here's the current code along with a sample output.

Code:
#include <iostream>
using namespace std;
int main()
{
char augend[20] = {'0'}, addend[20] = {'0'}, sum[21] = {'0'}, temp[20];
int aug = 0, add = 0, sumc = 20, tempc = 0;
int carry = 1;

cout << "This program adds two positive integers up to 20 digits long."
<< "\n\n";
cout << "Enter the augend: ";
cin >> noskipws >> temp[0];
while(temp[tempc]!='\n')
{
tempc++;
cin >> temp[tempc];
}
cout << "Enter the addend: ";
{
}
for(aug=19; tempc>=0; aug--, tempc--)
{
augend[aug] = temp[tempc];
}
{
{
sum[sumc - 1] = carry;
}
else
{
}
}
cout << "\n\n" << temp << " + " << addend << " = " << sum << ".\n";
return 0;
}
Code:
This program adds two positive integers up to 20 digits long.

Enter the augend: 1234

1234
+ 3456
= 0.

8. Does anybody else have advice on this? I would like to do the assignment with arrays to really get the concept of it down but at this point I'm considering just scrapping it and using strings (which we haven't done yet) since the assignment is due tonight at midnight. The output is still the part that's making me scratch my head -- for one, the returns are still in the character arrays and, of course, the sum is being given out as 0 no matter what numbers I input.