Home > Software design >  Find maximum and minimum nondiagonal elements of matrix
Find maximum and minimum nondiagonal elements of matrix

Time:01-27

I need to find maximum and minimum elements that belong to 4 areas of a matrix which are not parts of the diagonals. I have written code that finds minimum and maximum of the 4 areas that are nondiagonal. However, I need to find those elements separately, which would be 4 minimums and 4 maximums.

#include <stdio.h>

int main() {
    int n, i, j; double mat[100][100], min = 100, max = 0;
    scanf("%d", &n);

    for (i = 0; i < n; i  )
        for (j = 0; j < n; j  )
            scanf("%lf", &mat[i][j]);

    for (i = 0; i < n; i  ) {
        for (j = 0; j < n; j  ) {
            // Numbers that are not diagonal
            if (i != j && i   j != n - 1) {
                printf("%g", mat[i][j]);
                if (mat[i][j] < min)min = mat[i][j];
                if (mat[i][j] > max)max = mat[i][j];
            }
            // Diagonal elements
            else
                printf(" ");
        }

        printf("\n");
    }

    printf("min %g\n", min);
    printf("max %g\n", max);

    return 0;
}

CodePudding user response:

The matrix must be at least a 3x3 matrix to have off-diagonal elements. If you have an NxN matrix, then (using r for row and c for column) the diagonals have r == c and r == N - c - 1 (aka r c == N - 1). You can simply ignore elements for which either of those conditions is true.

You must place the other elements into one of the four quadrants aligned with the diagonals. The triangle at the top is the N (north) quadrant; at the bottom is the S (south) quadrant; at the left is the W (west) quadrant; at the right is the E (east) quadrant.

 N=5                      N=4
 r\c 0  1  2  3  4        r\c 0  1  2  3
  0  .  N  N  N  .         0  .  N  N  .
  1  W  .  N  .  E         1  W  .  .  E
  2  W  W  .  E  E         2  W  .  .  E
  3  W  .  S  .  E         3  .  S  S  .
  4  .  S  S  S  .

The dots represent the diagonals. In the 5x5 matrix, the W and S elements satisfy r > c; the N and E elements satisfy r < c (and the elements on the diagonal satisfy r == c). Similarly, the W and N elements satisfy r c < N - 1 while the S and E elements satisfy r c > N -1 (and the elements on the diagonal satisfy r c == N - 1). The 4x4 matrix also satisfies these conditions.

You can then use these conditions to classify a given (row, column) pair into one of the four quadrants using an encoding scheme similar to that suggested by Dèja vu in a comment, but adjusted to use different conditions:

int quadrant = ((r < c) ? 0 : 1)   ((r   c < N - 1) ? 0 : 2);

When quadrant is 0, it corresponds to the N quadrant; when it is 1, to the W quadrant; when it is 2, to the E quadrant; when it is 3, to the S quadrant. Note that this condition assumes that the diagonal elements are not tested. You might write assert(r != c && r c != N - 1); to ensure that it is not invoked for an element on the diagonals.

You can then compare the element mat[r][c] with min[quadrant] and max[quadrant], which is the scheme outlined by kaylum in a comment.

  •  Tags:  
  • Related