Here's a C program with output and explanation to find smallest element of given matrix using Nested Loops and For Loops.
# include <stdio.h> # include <conio.h> void main() { int mat[10][10] ; int i, j, row, col, small ; clrscr() ; printf("Enter the order of the matrix : ") ; scanf("%d %d", &row, &col) ; printf("\nEnter the elements of the matrix : \n\n") ; for(i = 0 ; i < row ; i++) for(j = 0 ; j < col ; j++) scanf("%d", &mat[i][j]) ; small = mat[0][0] ; for(i = 0 ; i < row ; i++){ for(j = 0 ; j < col ; j++){ if(mat[i][j] < small) small = mat[i][j] ; } } printf("\nThe smallest element in the matrix is : %d\n\n",small); getch() ; }
Output of above program -
Enter the order of the matrix : 2 2Enter the elements of the matrix :
4 5
1 2
The smallest element in the matrix is : 1
Explanation of above program -
In this program, we have a matrix mat of maximum size 10 x 10. The variables row and col holds the value of number of rows and columns of matrix respectively and the variable small contains the value of smallest element of the matrix mat.
First the program asks the user to enter the order of the matrix and then using a nested for loops the matrix is populated. Now using the code - "small = mat[0][0]" we are setting a benchmark for the rest of the calculation.
Using the next nested loop, we're now calculating the smallest element in the matrix. Lets understand this process with an example -
Working of Nested For Loop -
Suppose, row = col = 2 and the values in the matrix are same as shown in the output. Also the initial value of small would be mat[0][0] or in this case it'd be equal to 4. In this case, the outer for loop will run for i = 0 to i < row. Similarly, inner for loop will run for j = 0 to j < col.
When i = 0 -
- The value of j is also 0.
- Next, using IF condition the program checks whether the current matrix element that we're checking to be the smallest is smaller than the value already stored in the variable small. If the current element is indeed smaller than the variable small, the program updates the value of variable small with this current matrix element. Otherwise the program continues with other values in the matrix.
- So initially small = 4 and i = j = 0, so, in the first iteration the IF condition becomes false as mat[i][j] < small or mat[0][0] < 4 is not true. So the inner loop continues incrementing the value of j to 1.
- Now, the value of j is 1.
- Now, mat[0][1] = 5 and small = 4. So 5 < 4 which is false. So the inner loop continues incrementing the value of j to 2 making the looping condition for the inner loop false. So the inner for loop also quits.
- Now the value of i is incremented to 1.
When i = 1 -
- The value of j is 0.
- Now, mat[1][0] = 1 and small = 4. So 1 < 4 which is true. So now the value of small is updated to mat[1][0] i.e. now small = 1 and the inner loop continues incrementing the value of j to 1.
- Now j = 1, so mat[1][1] = 2 and small = 1. So 1 < 2 which is false. So the inner loop continues incrementing the value of j to 2 making the looping condition for the inner loop false. So the inner for loop quits.
- Now the value of i is incremented to 2 making the looping condition for the outer loop also false. So the outer for loop also quits.
After this nested loop the variable small contains the smallest value in the matrix.
If anyone reads this, how can I calculate the minimum of a specific line of the matrix?