-3

I am trying to find the biggest number in that struct array with this code:

max=0;
for (i = 1; i <= team.Length; i++)
{
    if (team[i].Point > team[max].Point) 
    { 
        max = i; 
    }

It gives me an error that it System.OutOfRangeException. Please help.

Igor
  • 60,821
  • 10
  • 100
  • 175
Csetox
  • 9
  • 1

1 Answers1

2

Arrays in C# are 0-based, not 1-based. Change your for loop:

for (i = 1; i < team.Length; i++)

NOTE: Edited based on feedback by @juharr

JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • 2
    Actually since the OP initializes max to 0 it would work with `for (i = 1; i < team.Length; i++)` It's just the `<` instead of `<=` that fixes it. – juharr Oct 20 '21 at 20:44