Here's a C program to enter values in a matrix and print values in a matrix using For Loops and Nested Loops with output and explanation.
# include <stdio.h> # include <conio.h> void main() { int mat[10][10] ; int i, j, row, col ; 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]) ; printf("\n\nThe elements in the matrix are: \n\n") ; for(i = 0 ; i < row ; i++){ for(j = 0 ; j < col ; j++){ printf("%d", mat[i][j]) ; printf("\t"); } printf("\n"); } getch() ; }
Output of above program -
Enter the order of the matrix : 3 3
Enter the elements of the matrix :
1
2
3
4
5
6
7
8
9
The elements in the matrix are:
1 2 3
4 5 6
7 8 9
Explanation of above program -
In this program we’ve several variables. Let’s first
understand the purpose of each variable –
- mat[10][10] – is a two dimensional integer array representing a matrix containing 10 rows (first index) and 10 columns (second index). This is how matrices are represented in C.
- i and j – are loop variables of two different for loops where i points to the rows and j points to the columns of our matrix.
- row and col – are the number of rows and columns respectively. These values are entered by user.
Now, the program first asks the user to enter the order of
the matrix i.e. number of rows and columns and stores these values in row and
col variables respectively. Next step is to enter the values in our matrix. To
do this, we use a nested for loop where the outer for loop (loop variable i)
points to every row of the matrix and inner for loop (loop variable j) points
to every column of the matrix.
The outer for loops iterates for the value of i
= 0 to i < row. The inner for loop iterates for the value of j = 0 to j <
col. Inside the inner for loop, the program asks the user to enter the values
in the matrix.After all the values in the matrix are entered, using
another nested loop we print the matrix.
Suppose user entered row = 3 and col = 3. The outer for loop
works for i = 0, 1 and 2 and the inner for loop works for j = 0, 1 and 2.
The working of nested for loop is as follows –
When i = 0, the inner for loop iterates three times i.e. for
j = 0, 1 and 2.
- When user first enters a value it is stored at mat[i][j] or mat[0][0]. Now the value of j is incremented by 1 so j becomes equal to 1 and inner loop iterates again. The value of i remains unchanged i.e. i = 0.
- Now the second value is stored at mat[i][j] or mat[0][1]. Again the value of j is incremented by 1 so j becomes equal to 2 and inner loop iterates again. The value of i remains unchanged i.e. i = 0.
- Now the next value is stored at mat[i][j] or mat[0][2]. Again the value of j is incremented by 1 so j now becomes equal to 3. Now the looping condition of inner for loop no longer holds true, so inner loop is not executed again and control goes to the outer for loop. Now the value of i is incremented by 1 so now i = 1.
When i = 1, the inner for loop again iterates three times
i.e. for j = 0, 1 and 2.
- Now the next value is stored at mat[i][j] or mat[1][0]. Now the value of j is incremented by 1 so j becomes equal to 1 and inner loop iterates again. The value of i remains unchanged i.e. i = 1.
- Now the next value is stored at mat[i][j] or mat[1][1]. Again the value of j is incremented by 1 so j becomes equal to 2 and inner loop iterates again. The value of i remains unchanged i.e. i = 1.
- Now the next value is stored at mat[i][j] or mat[1][2]. Again the value of j is incremented by 1 so j now becomes equal to 3. Now the looping condition of inner for loop no longer holds true, so inner loop is not executed again and control goes to the outer for loop. Now the value of i is incremented by 1 so now i = 2.
When i = 2, the inner for loop again iterates three times
i.e. for j = 0, 1 and 2.
- Now the next value is stored at mat[i][j] or mat[2][0]. Now the value of j is incremented by 1 so j becomes equal to 1 and inner loop iterates again. The value of i remains unchanged i.e. i = 2.
- Now the next value is stored at mat[i][j] or mat[2][1]. Again the value of j is incremented by 1 so j becomes equal to 2 and inner loop iterates again. The value of i remains unchanged i.e. i = 2.
- Now the next value is stored at mat[i][j] or mat[2][2]. Again the value of j is incremented by 1 so j now becomes equal to 3. Now the looping condition of inner for loop no longer holds true, so inner loop is not executed again and control goes to the outer for loop. Now the value of i is incremented by 1 so now i = 3. Now the looping condition for outer for loop also becomes false, so the nested loop terminates.
After this our matrix “mat” now contains values in three
rows and three columns.To print these values we use another nested for loop which
works exactly in the same way but this time instead of entering the values in
matrix using scanf(), we print the values in the matrix using a printf()
statement.
Hello there, nice post. Just a little question: How to input the matrix values in the form of the matrix, just like the output.
I want as
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13 format
Thank u very much,it helped me a lot.
Very good explanation....Helped me.
I haven't understood last two printf statements