• 10-14-2012
Gu7g34r
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 adarray[90][3];   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-- ) {     adarray[a][0] = adarray[a][2] + adarray[a][1];     while ( adarray[a][0] >= 10 ) {       d = a - 1;       adarray[a][0] = adarray[a][0] - 10;       adarray[d][0] = adarray[d][0] + 1;/* trouble is just here */     }   }   for ( a = 0; a < 90; a++ ) {     printf( "%d", adarray[a][2] );   }   printf( "\n" );   for ( a = 0; a < 90; a++ ) {     printf( "%d", adarray[a][1] );   }   printf( "\n" );   for ( a = 0; a < 90; a++ ) {     printf( "%d", adarray[a][0] );   } }```
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.

• 10-14-2012
oogabooga
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.
• 10-15-2012
Nominal Animal
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) {         high += low / RADIX;         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.
• 10-15-2012
AndiPersti
Code:

```for ( a = 89; a > 0 ; a-- ) {     adarray[a][0] = adarray[a][2] + adarray[a][1];     while ( adarray[a][0] >= 10 ) {         d = a - 1;         adarray[a][0] = adarray[a][0] - 10;         adarray[d][0] = adarray[d][0] + 1;/* trouble is just here */     } }```
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.

Quote:

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
• 10-15-2012
hk_mp5kpdw
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.
• 10-16-2012
Gu7g34r
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.
• 10-17-2012
Nominal Animal
Quote:

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     this_digit = this_digit - RADIX     next_digit = next_digit + 1 end while```
you should do this:
Code:

```next_digit = next_digit + this_digit / RADIX this_digit = this_digit % RADIX```
I also tried to say that you will find it easier to handle very big numbers if you store the least significant digit first, i.e. the rightmost digit in [0]. For example, when you sum two numbers together, you can then use a single index to access the components of both summands.

In your current approach, if you try to sum say 45-digit number A and a 30-digit number B, the first (least significant, or rightmost) digit before overflow check is A[44]+B[29]. See the indices? You'll need two separate index variables when walking through the arrays, and it makes the code hard to read, write, and understand.