-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
  • 58,317
  • 10
  • 91
  • 160
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
  • 36,115
  • 32
  • 128
  • 201
  • 2
    Actually since the OP initializes max to 0 it would work with `for (i = 1; i < team.Length; i++)` It's just the ` – juharr Oct 20 '21 at 20:44