1. ## Trouble with an addition function

Hello

I am trying to make a program that can do addition and multiplication on super high numbers. What I have been doing is making an array and putting a digit in each element of the array. But I am haveing trouble when the digits equal more than ten.

this is my code so far.

Code:
```#include <stdio.h>

int main()
{
int a;/* digit number */
int b;/* number manipulation interger */
int d = 0;/* number manipulation interger */
int c = 0;/* initialized first/second number */

for ( b = 0; b < 3; b++ ) {
for ( a = 0; a < 90; a++ ) {
adarray[a][b] = 0; /* Set each element to zero */
}
}
for ( b = 2; !b == 0 ; b-- ) {
for ( a = 89; a > 70; a-- ) {
c = c + 1;
if ( c == 10 ) {
c = 0;
}
adarray[a][b] = c; /* Set the first 20 of first and second numbers */
}
}
for ( a = 89; a > 0 ; a-- ) {
while ( adarray[a][0] >= 10 ) {
d = a - 1;
}
}
for ( a = 0; a < 90; a++ ) {
}
printf( "\n" );
for ( a = 0; a < 90; a++ ) {
}
printf( "\n" );
for ( a = 0; a < 90; a++ ) {
}

}```
I have three prints at the end for debugging purposes.
The first 2 loops work OK and the first part of the third loop. What happens is all the elements get set to zero, then on the digits on the far right of the first 2 element groups get set to something. So far it all works, then it adds the first 2 element groups together in the third element group. Then the program checks to see if the elements in the third group are are over ten. If they are it takes off ten, then it is supposed to add one to the next element along.
For some reason it takes off the ten but does not add the one to the next element.

Can anyone tell me why, it is driving me crazy.

2. It seems unlikely that you really mean this:
Code:
`!b == 0`
That logically negates b (changes 0 to 1 and non-zero to 0) then compares the result to 0.

Surely you want
Code:
`b != 0`
You could write it like this
Code:
`!(b == 0)`
but it's not idiomatic.

3. Does your array really put the most significant digit first? What do you do when you need to add one more digit? (Nevermind; I know you need to copy the entire number first. You would not, if you had the least significant digit first.)

In any case, if you have two digits, low and high, using radix RADIX (you're obviously using radix 10), then you can do the digit adjustment using
Code:
```    if (low >= RADIX) {
}```
You should also note that even if high was previously verified to be okay, it might just have grown too large. So, you should always process the digits in increasing importance. Which I think you are, although I'm having difficulty following your code due to the array order and traversal loops.

4. Code:
```for ( a = 89; a > 0 ; a-- ) {
while ( adarray[a][0] >= 10 ) {
d = a - 1;
}
}```
You add 1 to the row above (d = a - 1), then your loop starts the next iteration, and "a" is now "d". Thus, your first line in the loop overwrites the value you have just calculated in the iteration before.

I have three prints at the end for debugging purposes.
Using a debugger it was easy to step through the loop and see how the elements changed their values.

Bye, Andreas

5. Just a quick observation...
Code:
```int adarray[90][3];

...

for ( b = 0; b < 3; b++ ) {
for ( a = 0; a < 90; a++ ) {
adarray[a][b] = 0; /* Set each element to zero */
}
}```
You can skip the above double for-loop by zero-initializing the elements of the array where you declare the array. The following should be sufficient:
Code:
`int adarray[90][3] = {0};`
If you absolutely need to have the double loop present, know that your method of initialization (outer-dimension looped over first followed by inner-dimension) is usually less efficient than a more traditional loop that goes over the inner dimension first followed by the outer dimension:
Code:
```for ( a = 0; a < 90; a++ ) {
for ( b = 0; b < 3; b++ ) {
adarray[a][b] = 0; /* Set each element to zero */
}
}```
There is a difference between the two in regards to how memory is accessed.

6. Oogabooga, thank you I had trouble with that.

Nominal Animal, being a newbie your post went right over my head.

AndiPersti, getting a debugger is at the top of my list and I see what you mean saying the loop is overwriting itself. That explains why I get the output I do.

hk_mp5kpdw, I did not know that it was possible to set them all to zero at the beginnig. With the loops being outer then inner dimension, I had guessed but that part was an almost exact copy-n-pasted from the site's tutorial.

7. Originally Posted by Gu7g34r
Nominal Animal, being a newbie your post went right over my head.
Sorry.

I meant that instead of doing a loop like this, in pseudocode:
Code:
```while this_digit >= RADIX
```next_digit = next_digit + this_digit / RADIX