0

Hello :) I am novice programmer, please help me with the error "Index was out of bounds of the array" I think I am taking into consideration that the value that I am trying to use as my lcv is higher than that of the size of the array. I would really appreciate simplicity in the solution.

static void Main(string[] args)
        {
            Console.WriteLine("enter a number");
            /**********************************************************/
            string inputString = Console.ReadLine();
            int inputNum;
            int.TryParse(inputString, out inputNum);
            int[] divisorsArray = new int[inputNum -2];
            int[] filteredArray = new int[] { };
            int divisor = 1;
            int yum = 0;
            /**********************************************************/
            for (int i = 0; i < inputNum -2; i++)
            {

                if ( divisor != inputNum && inputNum % divisor == 0)
                    divisorsArray[i] = divisor;                  
                Console.WriteLine(divisorsArray[i]);
                divisor++;
                if (divisorsArray[i]>0)
                {

                    filteredArray[yum] = divisorsArray[i];
                    yum++;
                }
            }
            Console.ReadKey();

        }

1 Answers1

0

Its the line filteredArray[yum] = divisorsArray[i]; which is not possible because you initialized fiteredArray to an empty array. Initilize it as you do with divisorsArray and you should do fine.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
ChristianMurschall
  • 1,621
  • 15
  • 26