Sum(int[,] a, row, col) - Adds all elements together
Average(int[,] a, row, col) - Finds average value of elements
Max(int[,] a, row, col) - Finds maximum value
Min(int[,] a, row, col) - Finds minimum value
I have the sum function working correctly
public int Sum(int[,] a, int row, int col)
{
temp = 0;
for (int m = row; m < a.GetLength(0); m++)
temp += a[m, col];
for (int m = col + 1; m < a.GetLength(1); m++)
temp += a[row, m];
if (row + 1 < a.GetLength(0) && col + 1 < a.GetLength(1))
{
temp += Sum(a, row + 1, col + 1);
}
return temp;
}
What is giving me trouble is the Average function.
What I've attempted is to use the sum function, and simply divide it by the number of elements. However, because it is a recursive function, it will divide it every time it calls itself, giving an incorrect answer.
Here's my attempt at the average function.
public int Average(int[,] a, int row, int col)
{
temp = 0;
for (int m = row; m < a.GetLength(0); m++)
temp += a[m, col];
for (int m = col + 1; m < a.GetLength(1); m++)
temp += a[row, m];
if (row + 1 < a.GetLength(0) && col + 1 < a.GetLength(1))
{
temp += Average(a, row + 1, col + 1);
}
return temp / (a.GetLength(0) * a.GetLength(1));
}
Am I going about the whole function the wrong way or am I just failing to see where/how the division needs to be performed?

New Topic/Question
Reply
MultiQuote








|