0

Can't figure out why the line winningNumbers[i] += count; comes up as outside the bounds of the array.

static int[] CheckWinningNumbers(int[][]lottoNumbers, int[]drawNumbers)
                {
                    int[] winningNumbers = new int[8];
                    for (int i = 0; i < lottoNumbers.Length; i++)
                    {
                        int k = 0;
                        int count = 0;
                        for (int j = 0; j < lottoNumbers[i].Length; j++)
                        {
                            if (lottoNumbers[i][j] == drawNumbers[k])
                            {
                                count +=1;
                            }
                            k += 1;
                        winningNumbers[i] += count;
                        }
                    }       
                return winningNumbers;
Omri Btian
  • 6,499
  • 4
  • 39
  • 65
Cass
  • 1

1 Answers1

2

Just replace

int[] winningNumbers = new int[8];

With

int[] winningNumbers = new int[lottoNumbers.Length];

As long as you are iterating lottoNumbers.Length the variable i will not exceed its boundaries.

Edit

I ran it with the following data and it works

 int[][] lottoNumbers = { new[] { 1, 2 },                                       
                          new[]{ 3, 4 }, 
                          new[]{ 5, 6 }, 
                          new[]{ 7, 8 } };
 int[] drawNumbers = {1, 4, 5, 8};

I don't know what data you are testing it on but you are using jagged arrays intead of multidimensional. this will not guarantee that all your sub arrays have the same length which might be problematic. If all the arrays have the same length and performance is not critical here I'd suggest you use Multidimensional Arrays

Community
  • 1
  • 1
Omri Btian
  • 6,499
  • 4
  • 39
  • 65