# Addition array

Printable View

• 04-08-2007
quiet_forever
Addition array
In this program that I have written so far I am attempting to add two very large integers together using arrays:

Code:

```#include <iostream> using namespace std; const int Size = 20; int main () {         char FirstNumber [Size];         char SecondNumber [Size];         int Length;         int Length2;         int Num1 [Size];         int Num2 [Size];         int Answer [Size];                 //getting numbers into character array         cout<<"Enter the First Number : ";         cin>>FirstNumber;         cout<<"Enter the Second Number : ";         cin>>SecondNumber;                 //finding length of the arrays         Length = strlen (FirstNumber);         Length2 = strlen (SecondNumber);                 //bringing numbers in reverse so that they can be added         for (int a = Length - 1; a >= 0; a--)         {                 Num1 [a] = FirstNumber [a] - int ('0');//changing char to int                         for (int a = Length2 -1; a >= 0; a--)                         {                                 Num2 [a] = SecondNumber [a] - int ('0');                         }                                                 Answer [a] = Num1 [a] + Num2 [a];//getting sum of two numbers in the arrays                         if (Answer [a] > 10)//if answer of the two numbers is greter than ten, minus 10 and add ten to the next number                         {                                 Answer [a] = Answer [a] - 10;                                 Answer [a--] = Answer [a--] + 1;                         }                                                                         cout<< Answer [a];         }                         return 0; }```
Everything works right untill the numbers add up to be bigger then 10 and then I am trying to figure out how to carry the one to the next number. Any suggestions?
• 04-08-2007
Salem
Why do you reverse num2 every time ?

Create two functions to
- reverse a number string (which you call twice)
- add two number strings, and store the result in a third
• 04-08-2007
quiet_forever
I am suppose to add each number in the array together one by one. If the two numbers add up to greater then ten I know I have to minus ten for that particular index place but how do I add one on to the next number?
• 04-08-2007
Salem
How do you add two numbers on paper?
Use a 'carry', and add that on the next iteration.
• 04-08-2007
quiet_forever
I know that, but how do I do that in my program and get it to work? I tried to in my program above but that doesn't work.
• 04-08-2007
KONI
Without looking at the horrible, and probably wrong structure of your code (why are you even using an INNER for loop, with the SAME variable than the outer loop), the line that produces the biggest mistake is the following:

Code:

`Answer [a--] = Answer [a--] + 1;`
That lines decrements a two times. Additionally, the number you want to increment is stored at Answer[a+1] and not a-1.

The code is pretty hard to code in reverse, since the length of the two input strings is variable, so do as Salem suggested, reverse the two strings and add them from the start to the end and finally reverse the result once again.
• 04-08-2007
quiet_forever
You know I am a beginner and I'm sorry I'm not as superior as some people here think they are but even if I wern't there is no reason to go insulting peoples work, whether it's wrong or not. If you are not going to offer help without being derogatory then you don't need to post at all.