I'm trying to write my own function from scratch to add large numbers. This function takes the input of an unsigned long long array, with each number being 10 didgets long (the reason for the long long signed is becouse I might expand it) and adds it to the output array (same format). It seems for some reason the output array remains null. Would anyone point out any problems? Thanks.

#include <stdio.h>

void add(long long unsigned int input[], int isize,
        long long unsigned output[], int osize );

int main() {
  long long unsigned int output[4] = {0};
  long long unsigned int input[2][3] =
   {{9999999999LL, 9999999999LL, 9999999999LL},
   {9999999999LL, 9999999999LL, 9999999999LL}};
   add(input[0], 3, output, 10);
   add(input[1], 3, output, 10);
   printf("%llu%llu%llu%llu\n", output[0], output[1], output[2], output[3]);

  return 0;
}

void add(long long unsigned int input[], int isize,
         long long unsigned output[], int osize) {
/* This function will add input to output and store it in output. Here's a
   picture of how a 5 didget one would work:
   |     1 |     1 |       |<-carry
   |-----------------------|
   | 00000 | 99999 | 99999 |<-in
   | 00000 | 99999 | 99999 |<-out
   |-----------------------|
   |     1 | 99999 | 99998 |<-temp
   After every addition, the last 5 didgets of temp become the current 'out',
   and the rest of the didgets become the next 'carry'. This function is a 10
   didget one. */

 long long unsigned int carry = 0, temp = 0;
 int count = 0;

  while(count != osize) {    /* While there is output left */
    if(isize <= count) {     /* if there is more input */
      temp = input[count];   /* load up temp (input + output + carry) */
      temp += output[count];
      temp += carry;
      if(temp >= 10000000000LL) {      /* If there will be a carry */
        output[count] = temp % 10000000000LL; /* output = last 10 didgets */
        carry = temp / 10000000000LL;     /* carry = the rest of the didgets */ 
      }
      else {              /* If there is going to be no carry */
        output[count] = temp;
        carry = 0;
      }
    }
    else {         /* if there is no more input, but more output */
      temp = output[count]; /* load everthing into temp (output + carry) */
      temp += carry;
      if(temp >= 10000000000LL) { /* If there is going to be a carry */
        output[count] = temp % 10000000000LL; /* output = last 10 didgets */
        carry = temp / 10000000000LL;   /* carry = the rest of the didgets */
      }
      else { /* If there is no carry */
        output[count] = temp;
        return; /* no need to go further */
      }
    }
    count++; /* Can't forget this ;-) */
  }
}

Found one bug... Not sure why, but the loop is only running once per call.

BOOM?

Your indexing count is 10.
How big are your input and output arrays?

BOOM?

Your indexing count is 10.
How big are your input and output arrays?

Would you explain? The else block on line 49 is ment to be used after input has, well, no more input.

Would you explain? The else block on line 49 is ment to be used after input has, well, no more input.

Oh, wait. Duh! It's ment to be 4 not 10! I feel stupid :$

Input array is 6 integers in size total.
Ouput is 4 integers in size. LONG ints but still ints.

long long unsigned int output[4] = {0};
  long long unsigned int input[2][3] =

You pass osize in as 10

while(count != osize) { /* While there is output left */

so count ranges from 0...9

temp = input[count];
    temp += output[count];

I'm gettin closer. The problem now is the carry isnt functioning properly.

#include <stdio.h>

void add(long long unsigned int input[], int isize,
        long long unsigned output[], int osize );

int main() {
  long long unsigned int output[4] = {0};
  long long unsigned int input[2][3] =
   {{9999999999LL, 9999999999LL, 9999999999LL},
   {9999999999LL, 9999999999LL, 9999999999LL}};
   add(input[0], 3, output, 4);
   add(input[1], 3, output, 4);
   printf("%llu%llu%llu%llu\n", output[0], output[1], output[2], output[3]);

  return 0;
}

void add(long long unsigned int input[], int isize,
         long long unsigned output[], int osize) {
/* This function will add input to output and store it in output. Here's a
   picture of how a 5 didget one would work:
   |     1 |     1 |       |<-carry
   |-----------------------|
   | 00000 | 99999 | 99999 |<-in
   | 00000 | 99999 | 99999 |<-out
   |-----------------------|
   |     1 | 99999 | 99998 |<-temp
   After every addition, the last 5 didgets of temp become the current 'out',
   and the rest of the didgets become the next 'carry'. This function is a 10
   didget one. */

 long long unsigned int carry = 0, temp = 0;
 int count = 0;

  while(count != osize) {    /* While there is output left */
    if(count <= isize) {     /* if there is more input */
      temp = input[count];   /* load up temp (input + output + carry) */
      temp += output[count];
      temp += carry;
      if(temp >= 10000000000LL) {      /* If there will be a carry */
        output[count] = temp % 10000000000LL; /* output = last 10 didgets */
        carry = temp / 10000000000LL;     /* carry = the rest of the didgets */ 
      }
      else {              /* If there is going to be no carry */
        output[count] = temp;
        carry = 0;
      }
    }
    else {         /* if there is no more input, but more output */
      temp = output[count]; /* load everthing into temp (output + carry) */
      temp += carry;
      if(temp >= 10000000000LL) { /* If there is going to be a carry */
        output[count] = temp % 10000000000LL; /* output = last 10 didgets */
        carry = temp / 10000000000LL;   /* carry = the rest of the didgets */
      }
      else { /* If there is no carry */
        output[count] = temp;
        return; /* no need to go further */
      }
    }
    count++; /* Can't forget this ;-) */
  }
}

Still out running your input array.
You're trying to pass in a row of [2][3] as input
so input[]
But you're accessing index #4. Since you now changed your osize from 10 to 4. Your output is [4].
So still a problem with input reading past end of buffer!

Try this:

while(count != osize) {    /* While there is output left */
    printf("%d\n", count);

On line 35 & 36. Its output is

0
1
2
3
0
1
2
3
9999999998999999999999999999999999999998

Is it really going all the way to 4 still?

YES!!!

Array[4] is index {0,1,2,3}

So your Array of [2][3] where as you're passing one row appears as
Array[3] so valid index is {0,1,2} index 3 is past end of array!


Change your input array to [2][4]

Is it really going all the way to 4 still?

No, your right wildgoose. Its calling input[3], which is out of bounds. Thanks, I should be able to fix it now.

P.S. It get really annoying when we submit posts at close to the same time, dosent it? :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.