0
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.

2
  • Could you please share HugeIntegers code, especially the constructor? IndexOutOfRange seems like your hiDigits arrays are not of the same size. Have you checked that? Commented Dec 6, 2018 at 7:05
  • Edited as asked, and yeah I guess the array size is why the exception is there. I can't seem to fix it though. Commented Dec 6, 2018 at 21:09

1 Answer 1

3

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);
   }  
Sign up to request clarification or add additional context in comments.

3 Comments

So I'm thinking my approach with arrays are wrong? should I be using lists instead?
@cybernautmik: it's not wrong (you can create a temporary array of length + 1 length), but List<int> is more convenient.
Thank you, I'm reading up on the difference between array and lists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.