Trouble with an addition function

This is a discussion on Trouble with an addition function within the C Programming forums, part of the General Programming Boards category; Hello I am trying to make a program that can do addition and multiplication on super high numbers. What I ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    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 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.

    Thanks in advance.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    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.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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.

    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. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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.
    Last edited by hk_mp5kpdw; 10-15-2012 at 07:50 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    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. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    Quote Originally Posted by Gu7g34r View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with function
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-24-2009, 04:35 PM
  2. Trouble with function
    By belkins in forum C Programming
    Replies: 19
    Last Post: 11-10-2008, 05:11 PM
  3. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  4. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 04:23 AM
  5. IDEA: Addition function
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 09-16-2002, 09:43 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21