Search code examples
c#arraysalgorithmadditionarray-algorithms

inaccurate results with function to add an array of digits together


so i have this function:

        static int[] AddArrays(int[] a, int[] b)
        {
            int length1 = a.Length;
            int length2 = b.Length;
            int carry = 0;

            int max_length = Math.Max(length1, length2) + 1;
            int[] minimum_arr = new int[max_length - length1].Concat(a).ToArray();
            int[] maximum_arr = new int[max_length - length2].Concat(b).ToArray();
            int[] new_arr = new int[max_length];

            for (int i = max_length - 1; i >= 0; i--)
            {
                int first_digit = maximum_arr[i];
                int second_digit = i - (max_length - minimum_arr.Length) >= 0 ? minimum_arr[i - (max_length - minimum_arr.Length)] : 0;
                if (second_digit + first_digit + carry > 9)
                {
                    new_arr[i] = (second_digit + first_digit + carry) % 10;
                    carry = 1;
                }
                else
                {
                    new_arr[i] = second_digit + first_digit + carry;
                    carry = 0;
                }
            }

            if (carry == 1)
            {
                int[] result = new int[max_length + 1];
                result[0] = 1;
                Array.Copy(new_arr, 0, result, 1, max_length);
                return result;
            }
            else
            {
                return new_arr;
            }
        }

it basically takes 2 lists of digits and adds them together. the point of this is that each array of digits represent a number that is bigger then the integer limits. now this function is close to working the results get innacurate at certein places and i honestly have no idea why. for example if the function is given these inputs:

"1481298410984109284109481491284901249018490849081048914820948019" and "3475893498573573849739857349873498739487598" (both of these are being turned into a array of integers before being sent to the function)

the expected output is: 1,481,298,410,984,109,284,112,957,384,783,474,822,868,230,706,430,922,413,560,435,617

and what i get is:

1,481,298,410,984,109,284,457,070,841,142,258,634,158,894,233,092,241,356,043,561,7

i would very much appreciate some help with this ive been trying to figure it out for hours and i cant seem to get it to work perfectly.


Solution

  • I suggest Reverse arrays a and b and use good old school algorithm:

    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;
    }
    

    Demo:

    string a = "1481298410984109284109481491284901249018490849081048914820948019";
    string b = "3475893498573573849739857349873498739487598";
    
    string c = string.Concat(AddArrays(
      a.Select(d => d - '0').ToArray(), 
      b.Select(d => d - '0').ToArray()));
    
    Console.Write(c);
    

    Output:

    1481298410984109284112957384783474822868230706430922413560435617