I had to write code in C# that should determine the minimum and maximum in all even and odd columns, but the code does not include the last column at all, which in case the matrix is 3*4, would be odd. it does not check the numbers in the last column at all.
int min_o = Int32.MaxValue;
int max_o = Int32.MinValue;
int min_e = Int32.MaxValue;
int max_e = Int32.MinValue;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j % 2 != 0)
{
if (a[i, j] < min_o)
min_o = a[i, j];
else if (a[i, j] > max_o)
max_o = a[i, j];
}
else
{
if (a[i, j] < min_e)
min_e = a[i, j];
else if (a[i, j] > max_e)
max_e = a[i, j];
}
}
}
max_o
and min_o
are used for maximum and minimum for odd columns, and max_e
and min_e
are used for even columns.
here is example of what input and output for my program is:
n: 3
m: 4
1 2 3 4
5 4 6 7
9 1 7 9
Minimum for odd columns is: 1, maximum for odd columns is: 4Minimum for even columns is: 1, maximum for even columns is: 9
I tried to input 4*5 matrix, so that last column can be even, because I thought, maybe there is a problem when the last column is odd, but same problem is there too.
I think it's because you are using n in the inner for loop rather than m (assuming n is the number of rows and m is the number of columns)
I should be
for (int j = 0; j < m; j++)
NOT
for (int j = 0; j < n; j++)