cybernautmik
cybernautmik

Reputation: 63

adding array elements of two objects

class HugeInteger
{
    public const int HUGE_INTEGER_LIMIT = 40;
    public int[] hiDigits;
    public bool[] comparison;
    public int hiLength;
    private string hugeInteger;


    //constructor
    public HugeInteger()
    {
        hiDigits = new int[HUGE_INTEGER_LIMIT];
    }

    public HugeInteger(string hi)
    {
        hiDigits = new int[HUGE_INTEGER_LIMIT];
        hugeInteger = hi;
        Input(hi);   
    }

    public void Input(string input)
    {

        char[] hiDigitss = new char[HUGE_INTEGER_LIMIT];
        hiDigitss = input.ToCharArray();
        hiLength = hiDigits.Length;
        for (int i = hiLength - 1; i > 0; i--)
        {

            hiDigits[i] = hiDigitss[i] - '0';

    }

    public override string ToString()
    {
        string num = string.Join("", hiDigits.Select(x => x.ToString()).ToArray());
        return num;
    }

    public HugeInteger Add(HugeInteger val)
    {
        var result = new HugeInteger();
        int carry = 0;
        int sum = 0;
        hiLength = Math.Max(val.hiDigits.Length, this.hiDigits.Length);
        for (int i = 0; i < result.hiLength - 1; i++)
        {
            sum = this.hiDigits[i] + val.hiDigits[i] + carry;
            result.hiDigits[i] = sum % 10;
            carry = sum / 10;
        }

        //int[] result = new int[number1.length];
        //for (int i = number1.length - 1; i >= 0; i--)
        //{
        //    sum = number1[i] + number2[i] + carry;
        //    result[i] = sum % 10;
        //    carry = sum / 10;
        //}`enter code here`

        return result;
    }

    public bool IsEqualTo(HugeInteger hi)
    {
        comparison = new bool[hi.hiDigits.GetUpperBound(0)];
        for (int i = 0; i < this.hiDigits.GetUpperBound(0); i++)
        {
            if (this.hiDigits[i] == hi.hiDigits[i])
            {
                comparison[i] = true;
            }
            else
            {
                comparison[i] = false;
            }
        }
        if(comparison.All(c => c.Equals(true)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

In the code above, I am trying to add two objects in main by using

 num1.Add(num2)

num1 and num2 both hold an array of ints (int[]) that represent digits in a string of numbers. I am trying to create a method that will create a new array called result from adding num1 array and num2 array. Going through debugging, it's giving me

Index out of range

and val(num2) isn't seen when adding but this(num1) is.

I am also trying to make another method to substract.

edit: pasted more code as requested. Currently trying to change/fix input method.

Upvotes: 0

Views: 96

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

It may appear, that you want a bigger array than initial ones:

    988 +  // 3 digits : int[3]
     45    // 2 digits : int[2]
   ----    
   1033    // 4 digits (cause of index out of range exception): should be int[4]

Let's use List<int> for that (assuming both values are non negative, so we don't have to pay attention to signs):

   public HugeInteger Add(HugeInteger val) {
     if (null == val)   
       throw new ArgumentNullException(nameof(val));

     int length = Math.Max(val.hiDigits.Length, this.hiDigits.Length);

     List<int> list = new List<int>(length + 1);

     int carry = 0;

     for (int i = 0; i < length; ++i) {
       // ? : - be careful; lengths can be different (another source of index out of range)
       int sum = ((i < val.hiDigits.Length) ? val.hiDigits[i] : 0) + 
                 ((i < this.hiDigits.Length) ? this.hiDigits[i] : 0) +
                 carry;

       list.Add(sum % 10);
       carry = sum / 10;
     }

     // do not forget to add carry (which can be 1)  
     list.Add(carry);
     list.Reverse();

     int[] array = list
       .SkipWhile(item => item == 0) // remove leading zeros: 00123 -> 123
       .DefaultIfEmpty()             // at least one digit: 000 -> 0
       .ToArray();

     //TODO: check the right syntax here
     return new HugeInteger(array);
   }  

Upvotes: 3

Related Questions