Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to add two big integers
- Integer40 *big40Add(Integer40 *p, Integer40 *q)
- {
- // Variable declarations
- Integer40 *result;
- int i, int1, int2, sum, carry = 0;
- // If any NULL pointers are passed in, return NULL
- if (p == NULL || q == NULL)
- return NULL;
- // Create the array to hold a big integer by calling our create function
- result = createIntArray(MAX40);
- // Take care of the cases when create function returns NULL
- if(result == NULL)
- return NULL;
- // Add both integers and store it in the digits array
- for(i = 0; i < MAX40; i++)
- {
- // If i is less than 40, make int1/int2 equal to the ith digit of the array, else make it 0
- int1 = (i < MAX40) ? p->digits[i] : 0;
- int2 = (i < MAX40) ? q->digits[i] : 0;
- // Add both integer arrays, making sure we carry if the addition results in a double-digit number
- sum = int1 + int2 + carry;
- carry = sum / 10;
- // Store the added integers in the digits array, ignoring the last carry
- result->digits[i] = sum % 10;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement