0

I'm kinda of having this error:

"System.IndexOutOfRangeException: Index was out of the Array"

When I change int=1 to long=1 it says it can't convert long to int.

I'm trying to save all output of N into the Array and show at the end what is saved into arrays.

static void Main(string[] args)
{
    int a = 0;
    a = Convert.ToInt64(Console.ReadLine());

    int[] array = new int[a];

    if (a == 0)
    {
        Console.WriteLine("Falsche eingabe.");
    }
    else
    {
        long n = 1;
        for (int i = 1; i <= array.Length; i++)
        {
            n *= i;
            array[i] = n;

            Console.WriteLine("N: " + i + " Fakultät von N: " + array[i]);
        }
    }
    Console.ReadKey();
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • 1
    longs are bigger than ints, if you had a long that was bigger than int and you tried to convert it to an int, what would you expect to happen? – Stuart Aug 15 '17 at 16:27
  • `i <= array.Length` is wrong - if `i` is `array.Length` it means it's out of range as arrays are 0 based – Jakub Dąbek Aug 15 '17 at 16:28
  • I think knowing if the language you are coding in is zero-indexed or one-indexed is pretty primitive - if you already know that then please learn to debug your problems more carefully. – crazyGamer Aug 15 '17 at 16:30
  • @JakubDąbek it makes sense, I change array.Length to just "a". It underlimes the "array[i] = n;" and says the same problem asit was before. –  Aug 15 '17 at 17:51

1 Answers1

1
  • Indexes of arrays begin from 0 not from one. Therefore in your for loop, i <= array.Length, when i is equal to the length you enter and try array[array.Length] which throws the exception. Change to:

    for (int i = 0; i < array.Length; i++)
    

    Reason indexes are zero based is that the language is based on C where an array is a pointer to the location that was allocated for the array:

    int *arrayPointer;
    

    And then to go through the array one needs to go to arrayPointer + sizeof(int)*i. So for the first position of the array i should start from zero

  • In addition this line doesn't compile:

    a = Convert.ToInt64(Console.ReadLine());
    

    a is of type int while you are converting to long. Use ToInt32


Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • without array[i] = n; line everything works fine without any problem. It works also when i define a as a long and int64 sane as int62 why? –  Aug 15 '17 at 17:53
  • as long I write array [i] in console.writeline for example Console.WriteLine("N: " + array[i] + " Fakultät von N: " + n); I get that error.. –  Aug 15 '17 at 17:55
  • For the reason I explain - debug and see. At the last iteration of the loop, when `i` equals `array.Length` you get the exception. Because of out of bounds. – Gilad Green Aug 15 '17 at 17:56
  • @eXme - Did you manage to solve it? – Gilad Green Aug 16 '17 at 08:17
  • no I did not solved the problem. I also tryed same thing with list and having almost same problem System.ArgumentOutOfRangeException dont understand yet, how to solve the problem. –  Aug 16 '17 at 11:44
  • @eXme - Did you change the loop condition as I suggested? – Gilad Green Aug 16 '17 at 11:44
  • no but i found the problem it was in for (int i = 1; i <= array.Length; i++) and in Console.WriteLine("N: " + i + " Fakultät von N: " + `array[i]`); my i start from 1 and it should start from 0 in array because there is no array[1] in the first loop and only 0 :D so if i declate variable j = 0; and then add array[j++] it works of course as it should be, the fak... part took me whole day lol :D –  Aug 16 '17 at 14:22
  • @eXme - Yes - because of what I said before. when you access list[i] in a place that is not in the array. – Gilad Green Aug 16 '17 at 14:23
  • I didnt get it, im not that advanced in yet doing it for some days only :) but thanks tho if you tho it was like that. –  Aug 16 '17 at 14:25