I have this function:
static int[] AddArrays(int[] a, int[] b)
{
Array.Reverse(a);
Array.Reverse(b);
int[] result = new int[Math.Max(a.Length, b.Length) + 1];
int carry = 0;
int value = 0;
for (int i = 0; i < Math.Max(a.Length, b.Length); ++i)
{
value = (i < a.Length ? a[i] : 0) + (i < b.Length ? b[i] : 0) + carry;
result[i] = value % 10;
carry = value / 10;
}
if (carry > 0)
result[result.Length - 1] = carry;
else
Array.Resize(ref result, result.Length - 1);
// Let's restore a and b
Array.Reverse(a);
Array.Reverse(b);
Array.Reverse(result);
return result;
}
the function takes 2 arrays of digits these arrays represent a big number which is outside of the integer limits. the function adds together the elements and returns the result. my question is how can i adjust this function to subtract the numbers, for example:
input:
int[] a = 5,475,982,475,984,574,238,975,248,522,952,789,229,899,999,999,9
int[] b = 5,475,982,475,984,574,238,975,248,522,952,789,229,899,999,999,8
(as digit lists meaning each index in the arr is one digit)
expected output:
1
(since 5,475,982,475,984,574,238,975,248,522,952,789,229,899,999,999,9 - 5,475,982,475,984,574,238,975,248,522,952,789,229,899,999,999,8 = 1
)