A two-dimensional array is an array that stores data in the form of rows and columns. It is also called a matrix. In C language, a 2D array is declared using two indexes, where the first index represents the row and the second index represents the column.
In this program, a 4 x 4 two-dimensional array is created. The user enters values into the array using nested for loops. After storing the values, another nested loop is used to display the complete matrix.
How It Works?
m[4][4] to store matrix values.for loops to accept values for rows and columns.for loop to display all array elements.#include <stdio.h> #include <conio.h> main() { int m[4][4], k, p; clrscr(); for(k = 0; k <= 3; k++) { for(p = 0; p <= 3; p++) { printf("\nEnter any no : "); scanf("%d", &m[k][p]); } } for(k = 0; k <= 3; k++) { for(p = 0; p <= 3; p++) { printf("%d ", m[k][p]); } printf("\n"); } getch(); }
for loops.int m[3][3]. Use one for loop for rows and another for loop for columns to accept and display the values.#include <stdio.h> #include <conio.h> main() { int m[3][3], k, p; clrscr(); for(k = 0; k <= 2; k++) { for(p = 0; p <= 2; p++) { printf("\nEnter any no : "); scanf("%d", &m[k][p]); } } for(k = 0; k <= 2; k++) { for(p = 0; p <= 2; p++) { printf("%d ", m[k][p]); } printf("\n"); } getch(); }
for loop accepts values from the user and stores them in rows and columns. The second nested for loop displays all elements of the matrix in proper row and column format.