Thread: Please help! I am so lost as to how to fix this!

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    29

    Please help! I am so lost as to how to fix this!

    I am relatively new to programming, and I have been working on this program for several days. I have gotten all my functions to work perfectly, except for my intAdd function, and I cant seem to figure out where it is going wrong or how to fix it.

    The intAdd function adds two very large integers together, by putting each digit of the large integer in an array, putting the arrays into reverse order, and adding them together.

    An example of this would be

    12345+1

    [5][4][3][2][1]
    +[1]
    _____________
    [6][4][3][2][1]

    so that when it is flipped back to normal again, it is 12346.



    When I try to run the function however, it does not add it correctly. It seems to fill the entire array with zeros except for the one's place, and I cant see why this is happening or how I can fix it.


    Any advice as to what is causing this and how I may possibly fix this would be greatly appreciated.

    Here is the header file with the function and structure definition:
    Code:
    typedef struct HugeInteger
    {
     // a dynamically allocated array to hold the digits of a huge integer
     int *digits;
     // the number of digits in the huge integer (approx. equal to array length)
     int length;
    } HugeInteger;
    
    // Functional Prototypes
    HugeInteger *hugeAdd(HugeInteger *p, HugeInteger *q);
    and here is the function that is causing me trouble:
    Code:
    //create function for hugeAdd
    HugeInteger *hugeAdd(HugeInteger *p, HugeInteger *q){
    int i;
    //return NULL if either pointer is NULL
    if(p == NULL || q == NULL)
        return NULL;
    //create a new HugeInteger to store the result in
    HugeInteger *a = NULL;
    HugeInteger *trim = NULL;
    
    a = malloc(sizeof(HugeInteger));
    trim = malloc(sizeof(HugeInteger));
    //check if malloc was successful
    if(a == NULL){
        printf("\n23: Malloc failed\n");
        return NULL;
    }
    a->digits = NULL;
    //if p is bigger than q
    if (p->length > q->length){
        a->digits = calloc(p->length+1, sizeof(int));
        a->length = p->length;
        //check to make sure calloc was successful
        if(a->digits == NULL){
            printf("\n34: Calloc failed\n");
            a->length = 0;
            free(a);
            return NULL;
        }
        //to find where the error occurs with the addition
        printf("\np contains the elements: \t\t\t");
        for(i=0; i<p->length; i++)
            printf("%d",p->digits[i]);
        printf("\nq contains the elements: \t\t\t");
        for(i=0; i<q->length; i++)
            printf("%d", q->digits[i]);
    
        for (i=0; i<q->length; i++){
            //if sum is >10 do this, since each cell should only hold 0-9
            if (p->digits[i]+q->digits[i]>=10){
                a->digits[i] += (p->digits[i]+q->digits[i])-10;
                a->digits[i+1] = 1;
            }
            else{
                a->digits[i] += p->digits[i]+q->digits[i];
            }
        }
    }
    //if q is bigger than p
    else{
    
        a->digits = calloc(q->length+1, sizeof(int));
        a->length = q->length;
        //check to make sure calloc was successful
        if(a->digits == NULL){
            printf("\n62: Calloc Failed\n");
            a->length = 0;
            free(a);
            return NULL;
        }
        for (i=0; i<p->length; i++){
            //if sum is >10 do this, since each cell should only hold 0-9
            if (p->digits[i]+q->digits[i]>=10){
                a->digits[i] += (p->digits[i]+q->digits[i])-10;
                a->digits[i+1] += 1;
            }
            else{
                a->digits[i] += p->digits[i]+q->digits[i];
            }
        }
    }
    //if there is a hanging 0 at the end
        if (a->digits[a->length] == 0)
        {
            trim->digits = malloc(sizeof(a->length-1));
            if(trim->digits == NULL)
            {
                printf("\ntrim malloc failed");
                free(trim);
                return NULL;
            }
            //transfer digits over to new struct
            for (i=0; i<a->length-1; i++)
            {
                trim->digits[i] = a->digits[i];
            }
            trim->length = a->length-1;
            /*  Dont know why I have this
            for(i=0; i<trim->length; i++)
            {
                trim->digits[i]=a->digits[i];
                free(a->digits);
                free(a);
            }
            */
        printf("\nHere is the result of the Addition function (still in reverse):\t");
    //to determine where the error is occurring
        for (i=0; i<trim->length; i++)
            printf("%d", trim->digits[i]);
        return trim;
        }
    return a;
    }
    Please tell me what is causing this issue! Thank you so much!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You forget to copy the extra digits to "a" from "p" and "q".
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am lost
    By davidmaslowski in forum C Programming
    Replies: 4
    Last Post: 11-19-2010, 06:02 PM
  2. Lost
    By Aparavoid in forum General Discussions
    Replies: 36
    Last Post: 06-13-2009, 03:16 AM
  3. lost value??
    By Hussain Hani in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 06:55 AM
  4. okay im lost
    By D00M3D2 in forum Game Programming
    Replies: 11
    Last Post: 04-11-2006, 09:37 PM
  5. I'm lost on this one
    By brutal in forum C Programming
    Replies: 6
    Last Post: 05-14-2002, 07:01 PM