I have to remake a recursive program to dynamic for university task and when doing this I bumped into an error:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
The error seems to happen in line, where is max = Max(sum2[l], array[j - 1, i - 1]);
static int F2(int k, int n)
{
int[] sum2 = new int[100];
int max = 0;
int[,] array = new int[n + 1, k + 1];
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= k; j++)
{
//array[i, j] = int.MaxValue;
if (i == 0) array[i, j] += 0;
else if (j == 1) array[i, j] += p[i];
else
{
for (int l = 1; l < n; l++)
{
for (int e = l; e < n; e++)
sum2[l] += p[i];
max = Max(sum2[l], array[j - 1, i - 1]);
array[i, j] = int.MaxValue;
if (max < array[i, j]) array[i, j] = max;
}
}
}
}
return array[k, n];
}
public static int Max(int a, int b) { return a > b ? a : b; }
So any toughts on how to fix this and what do I do wrong?