Summary: In this tutorial, we will learn to find the smallest and largest diagonals elements of a square matric using c programming.

The best and easiest way to find the minimum and maximum elements of the diagonals, is to iterate through the elements of the diagonals and select the one which is smallest or largest by comparing them.

To do this, we initially have to assume any element of the diagonal as the smallest or largest. Then, as we iterate through the diagonal, we compare this number with the current element of the diagonal.

If the current element is smaller than the selected smallest number, we update the smallest number variable. If the current number is larger than the selected largest number, we update the largest number variable with the same.

Here is how it can be done in C language:

#include <stdio.h>

int main() {
    int matrix[3][3] = {{1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}};
                      
    int i, smallest, largest;

    // Initialize smallest and largest with the first element of the first diagonal
    smallest = matrix[0][0];
    largest = matrix[0][0];

    // Find the smallest and largest elements in the first diagonal
    for (i = 1; i < SIZE; i++) {
        if (matrix[i][i] < smallest) {
            smallest = matrix[i][i];
        }
        if (matrix[i][i] > largest) {
            largest = matrix[i][i];
        }
    }

    printf("Smallest element in the first diagonal: %d\n", smallest);
    printf("Largest element in the first diagonal: %d\n", largest);

    // Initialize smallest and largest with the first element of the second diagonal
    smallest = matrix[0][SIZE - 1];
    largest = matrix[0][SIZE - 1];

    // Find the smallest and largest elements in the second diagonal
    for (i = 1; i < SIZE; i++) {
        if (matrix[i][SIZE - i - 1] < smallest) {
            smallest = matrix[i][SIZE - i - 1];
        }
        if (matrix[i][SIZE - i - 1] > largest) {
            largest = matrix[i][SIZE - i - 1];
        }
    }

    printf("Smallest element in the second diagonal: %d\n", smallest);
    printf("Largest element in the second diagonal: %d\n", largest);

    return 0;
}

Output:

Smallest element in the first diagonal: 1
Largest element in the first diagonal: 9
Smallest element in the second diagonal: 3
Largest element in the second diagonal: 7

As you can see, we got the output as expected.

In the above program, we have explicitly defined the elements of the matrix.

There are two for loops in the program. In the first for loop, we compare the each element of the first diagonal with the smallest and largest variable.

If the element is smaller than the smallest. we update the value of the smallest with it. If it is larger than the largest, we update the value of the largest variable.

By doing so, we get the smallest and largest element of the first diagonals.

We use the same logic in the second for loop to find the smallest and largest elements of second diagonal of the matrix.

Leave a Reply