-1

I know this kind of question is asked before, but I am a noob and cannot understand where the array is out of bounds.

P.S this is not the exact duplicate of the every " system.IndexOutOfRange exception in C# " as the situation and the context of the problem is totally different from each other.So please read the question first before reporting it as the " exact duplicate ".

 class Program
{
    static void Main(string[] args)
    {
        int highestcount = 0, e = 0, h = 0;
        string highest = "", a = "", b = "", j = "";
        int y = int.Parse(Console.ReadLine());
        e = y - 1;
        string[] savArray = new string[e];

        for (int count = 0; count < y; count++)
        {
            var x = Console.ReadLine().Split(' ');

            if (count+1 <= y)
            {
                j = x[count + 1];
                savArray[count] = j;
            }


        }

        for (int count1 = 0; count1 < y; count1++)
        {
            h = 0;
            a = savArray[count1];
            for (int count2 = 1; count2 < y; count2++)
            {
                b = savArray[ count2];
                if (a == b)
                {
                    h = h + 1;
                    if (highestcount <= h)
                    {
                        highestcount = h;
                        highest = a;
                    }
                }
            }

        }

        Console.Write(highest);
        Console.Read();
    }

}

The program is to find the most common sport. First, the user has to enter how many no: of entries are there on the list.

After that, the user has to enter his name and then his favorite sport.

If you read the program what I meant to do is simply save the 2nd value of x in the array because every second value is the sport which the user has given, as I want to save the sport's name to check which sport is more common.

But the exception is ruining the simple idea and I am stuck with it, please help.

  • Can you not step through the code with the debugger to see what's going on? – Paul T. Nov 11 '17 at 01:34
  • Likely they don't know how to, https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger Might help! The end goal is to step through the loop for each iteration, and inspect the values of the variables/state of the code to see when, why and what is causing the index to be out of range. – Gry- Nov 11 '17 at 01:36
  • I have done that, but as I mentioned I am a newbie and just cannot catch where's the problem. – Syed Hassan Farman Nov 11 '17 at 01:40
  • You have got to learn how to [use a debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Do that now. – Dour High Arch Nov 11 '17 at 01:49
  • _"the context of the problem is totally different from each other"_ -- the exact context isn't important. What matters is you have the exact exception that that Q&A is specifically talking about. Frankly, no one should ever need to ask a question on Stack Overflow about fixing an `IndexOutOfRangeException`. It's a simple matter of debugging, which the marked duplicate addresses. – Peter Duniho Nov 11 '17 at 02:48
  • If you debug it and still can't figure out the problem, then you should be able to ask the question without ever even mentioning `IndexOutOfRangeException`, because you'll have gotten far enough into the problem to know the proximate cause of the exception and can express the issue in terms of what led up to that, rather than the exception itself. – Peter Duniho Nov 11 '17 at 02:48
  • sir, you were absolutely right. I debugged the code thoroughly and have finally resolved the issue. P.S I did debug before but after your comment, i was able to complete and solve the problem. – Syed Hassan Farman Nov 12 '17 at 01:04
  • now I have to enter the correct code and I don't know how to? @PeterDuniho – Syed Hassan Farman Nov 12 '17 at 01:19

2 Answers2

0
for (int count = 0; count < y; count++)
    {
        var x = Console.ReadLine().Split(' ');

        if (count+1 <= y)
        {
            j = x[count + 1];
            savArray[count] = j;
        }


    }

Haven't double checked but... [count + 1] Given the for loop constraints (count < y) the maximum possible value for count is y - 1... but.... count + 1 when count = y -1... and thus is out of range... I think. Again. Haven't double checked.

Gry-
  • 176
  • 9
0

Let's say that savArray has 1 element.

Then you do this:

 var x = Console.ReadLine().Split(' ');

 //Let's say x is "X" which is creates an array of size 1

 if (count + 1 <= y) <----this is true (0+1<=1)
 {  
      j = x[count + 1]; <----- BOOM x[1] when x has only one element.
tymtam
  • 31,798
  • 8
  • 86
  • 126