Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // Function to add two big integers
  2. Integer40 *big40Add(Integer40 *p, Integer40 *q)
  3. {
  4. // Variable declarations
  5. Integer40 *result;
  6. int i, int1, int2, sum, carry = 0;
  7.  
  8. // If any NULL pointers are passed in, return NULL
  9. if (p == NULL || q == NULL)
  10. return NULL;
  11.  
  12. // Create the array to hold a big integer by calling our create function
  13. result = createIntArray(MAX40);
  14.  
  15. // Take care of the cases when create function returns NULL
  16. if(result == NULL)
  17. return NULL;
  18.  
  19. // Add both integers and store it in the digits array
  20. for(i = 0; i < MAX40; i++)
  21. {
  22. // If i is less than 40, make int1/int2 equal to the ith digit of the array, else make it 0
  23. int1 = (i < MAX40) ? p->digits[i] : 0;
  24. int2 = (i < MAX40) ? q->digits[i] : 0;
  25.  
  26. // Add both integer arrays, making sure we carry if the addition results in a double-digit number
  27. sum = int1 + int2 + carry;
  28. carry = sum / 10;
  29.  
  30. // Store the added integers in the digits array, ignoring the last carry
  31. result->digits[i] = sum % 10;
  32. }
  33.  
  34. return result;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement