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, big; 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]) ; big = mat[0][0] ; for(i = 0 ; i < row ; i++){ for(j = 0 ; j < col ; j++){ if(mat[i][j] > big) big= mat[i][j] ; } } printf("\nThe biggest element in the matrix is : %d\n\n",big); 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 : 5

### 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 big contains the value of biggest 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 -

*"big = mat[0][0]"*we are setting a benchmark for the rest of the calculation.
Using
the next nested loop, we're now calculating the biggest 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
big 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 biggest is bigger than
the value already stored in the variable
*big*. If the current element is indeed bigger than the variable*big*, the program updates the value of variable big with this current matrix element. Otherwise the program continues with other values in the matrix. - So initially big = 4 and i = j = 0, so, in the first iteration the IF condition becomes false as
*mat[i][j] > big*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 big = 4. So 5 > 4 which is true. So now the value of big is updated to mat[0][1] i.e. now big = 5 and 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 1.

**When i = 1 -**

- The value of j is 0.
- Now, mat[1][0] = 1 and big = 5. So 1 > 5 which is false. So the inner loop continues incrementing the value
of j to 1.

- Now j = 1, so mat[1][1] = 2 and big = 5. So 2 > 5 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

*big*contains the smallest value in the matrix.
Hello There,

11/10!! Your blog is such a complete read. I like your approach with C Program to Find Biggest Element of Matrix. Clearly, you wrote it to make learning a cake walk for me.

I am a beginner of C and am stuck on a problem like below

int i=3, j, k;

j = i++*i++;

k = ++i*++i;

print ("j=%d k=%d", j, k);

As per my calculations the values should be j=12 and k=42 but the value of the code turns out to be j=9 and k=49. I don't understand why it happens.

I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.

Thanks and Regards

Preethi