After failing to answer an interview question, I went back home and coded the answer for my own improvement. The questions was: Given two arrays containing a number, but with each element of the array containing a digit of that number, calculate the sum and give it in the same format.There must be no leading zeros in the resulting array.My main concern is that I am being redundant with my logic, or that my code is not clear. int *sumOfTwo(int *a1,int *a2, int lengthOfA1, int lengthOfA2, int *lengthDest) {      //I assume there is error checking for null pointers, etc.      int *answer;      int maxSize, minSize, diff;      int a1IsLongest = TRUE;      if(lengthOfA1 > lengthOfA2) {          maxSize = lengthOfA1;          minSize = lengthOfA2;          a1IsLongest = TRUE;      }      else {          maxSize = lengthOfA2;          minSize = lengthOfA1;          a1IsLongest = FALSE;      }      diff = maxSize - minSize;      answer = malloc(maxSize * sizeof(int));      int i, extraTen, result;      extraTen = 0;      *lengthDest = maxSize;      //printf(This is lengthDest: %d\n, *lengthDest);      for (i = maxSize-1; i >= 0; i--) {           //add numbers, check if bigger than 10           //check if arrays are of different sizes we want to access         //the end from each, so need to account for this.          if(diff == 0) {              //arrays are equal in size              result = a1[i] + a2[i] + extraTen;              extraTen = 0;          }          else{              //arrays are different sizes              if(! ((i - diff) < 0)) {                  if(a1IsLongest) {                      result = a1[i] + a2[i - diff] + extraTen;                      extraTen = 0;                  }                  else {                      result = a1[i - diff] + a2[i] + extraTen;                      extraTen = 0;                  }              }              else {                  if(a1IsLongest) {                  result = a1[i] + extraTen;                  extraTen = 0;              }              else {                  result = a2[i] + extraTen;                  extraTen = 0;              }          }      }      //the new malloc can only happen here      if (result >= 10) {         //check if at beginning of array         if(i == 0) {              maxSize = maxSize + 1;              *lengthDest = maxSize;              //printf(This is lengthDest: %d\n, *lengthDest);              //overflowing, need to realloc              int *temp = malloc(maxSize * sizeof (int));              int j;              //copy old elements into new array               for(j = 0; j < maxSize; j++) {                  //printf(index: %d\n, j);                  if(j == 0) {                      temp[j] = 1;                  }                  else if(j == 1) {                      temp[j] = result % 10;                  }                  else {                      temp[j] = answer[j-1];                  }              }              free(answer);              answer = temp;              return answer;          }          else{              //printf(in the else of the i equals 0.\n);              answer[i] = result % 10;              extraTen = 1;          }      }      else {         //printf( results is less than 10\n);         answer[i] = result;     } } return answer;}
Adding two numbers, each digit as a single element of array
c
 int *sumOfTwo(int *a1,int *a2, int lengthOfA1, int lengthOfA2, int *lengthDest) {      //I assume there is error checking for null pointers, etc.      int *answer;      int maxSize, minSize, diff;      int a1IsLongest = TRUE;      if(lengthOfA1 > lengthOfA2) {          maxSize = lengthOfA1;          minSize = lengthOfA2;          a1IsLongest = TRUE;      }      else {          maxSize = lengthOfA2;          minSize = lengthOfA1;          a1IsLongest = FALSE;      }      diff = maxSize - minSize;      answer = malloc(maxSize * sizeof(int));This seems more complicated than necessary.  Consider typedef struct Number {    int *digits;    int length;} Number;Number *sumOfTwo(const Number *a1, const Number *a2) {    //I assume there is error checking for null pointers, etc.    if (a1->length > a2->length) {        return _sumOfTwo(a1, a2);    } else {        return _sumOfTwo(a2, a1);    }}Number *_sumOfTwo(const Number *a1, const Number *a2) {    Number *answer;    answer = malloc(sizeof(*answer));    answer->length = (a1->length + 1);    answer->digits = calloc(answer->length, sizeof(*(answer->digits)));Now, a1 is always as long as or longer than a2.  So you can do away with your temporary variables.  So far, it's about the same length, but we've simplified the function signature.      if (answer == NULL) {        // do something    }    if (answer->digits == NULL) {        // do something    }As noted elsewhere, you should check that malloc and calloc succeed.        int i, extraTen, result;      extraTen = 0;      *lengthDest = maxSize;      //printf(This is lengthDest: %d\n, *lengthDest);      for (i = maxSize-1; i >= 0; i--) {           //add numbers, check if bigger than 10           //check if arrays are of different sizes we want to access         //the end from each, so need to account for this.          if(diff == 0) {              //arrays are equal in size              result = a1[i] + a2[i] + extraTen;              extraTen = 0;          }          else{              //arrays are different sizes              if(! ((i - diff) < 0)) {                  if(a1IsLongest) {                      result = a1[i] + a2[i - diff] + extraTen;                      extraTen = 0;                  }                  else {                      result = a1[i - diff] + a2[i] + extraTen;                      extraTen = 0;                  }              }              else {                  if(a1IsLongest) {                  result = a1[i] + extraTen;                  extraTen = 0;              }              else {                  result = a2[i] + extraTen;                  extraTen = 0;              }          }      }Now this code we can make a lot shorter with fewer checks.        int i = 0;      for (; i < a2->length; i++) {          answer->digits[i] += a1->digits[i] + a2->digits[i];          process_carry(answer->digits, i);      }      for (; i < a1->length; i++) {          answer->digits[i] += a1->digits[i];          process_carry(answer->digits, i);      }Since a1 is always longer than a2, we don't have to check the lengths.  We avoid checking the diff by changing the order from most significant digit at the zero position to the least significant at the zero position.  Now we have to use two loops, but we don't have to check anything.  We can use += because calloc zeroes out its elements.  If there's a carry, we add to it.  If not, there's already a 0 there.  Create process_carry:    const int BASE = 10;  void process_carry(int *digits, int position) {      if (digits[position] > BASE) {          digits[position] -= BASE;          digits[position+1] += 1;      }  }You can create a more generic version by replacing the if with a while.  Or    void process_carry(int *digits, int position) {      digits[position+1] += digits[position] / BASE;      digits[position] %= BASE;  }This may be more efficient than using a while.  Note how neither version requires an extraTen variable.  This also uses BASE rather than the magic number 10.  Now if you want to change to a different base, you can do so easily.        //the new malloc can only happen here      if (result >= 10) {         //check if at beginning of array         if(i == 0) {              maxSize = maxSize + 1;              *lengthDest = maxSize;              //printf(This is lengthDest: %d\n, *lengthDest);              //overflowing, need to realloc              int *temp = malloc(maxSize * sizeof (int));              int j;              //copy old elements into new array               for(j = 0; j < maxSize; j++) {                  //printf(index: %d\n, j);                  if(j == 0) {                      temp[j] = 1;                  }                  else if(j == 1) {                      temp[j] = result % 10;                  }                  else {                      temp[j] = answer[j-1];                  }              }              free(answer);              answer = temp;              return answer;          }          else{              //printf(in the else of the i equals 0.\n);              answer[i] = result % 10;              extraTen = 1;          }      }      else {         //printf( results is less than 10\n);         answer[i] = result;     } } return answer;This could be a lot simpler.   if (answer->length > 1 && answer->digits[answer->length - 1] == 0) {     answer->length--;     realloc(answer->length * sizeof(answer->digits[0]));     if (answer->digits == NULL) {         free(answer->digits);         // fail gracefully     } } return answer;Now we don't have to copy.  And we already handled the carry.  We just resize the array if we made it too big to start.  And then return the result.  Note that this allows for the situation where we are returning zero while stripping off a single leading zero otherwise.  