Read and Display two dimensional 2D array in C Language
In this tutorial topic we will learn how to read and Access Elements of Two dimensional 2D array used in C Language?.
In previous tutorial topics we did learn what is array,single and two dimensional array?. also learned How to declare and initialize array ?. Now it is time to learn how to read and access the 2D array elements.
To access individual elements from 2d array the array name along with column index ,row index numbers are used.
Given below is the syntax to acces individual elements from two dimensional array in c programming Language.
e.g array_name[row_index][column_index];
array_name: is the name of two dimensional array.
row_index: is the row number of two dimensional array. it starts from 0.
column_index:is the column number of two dimensional array. it also starts from 0.
Let us learn how to read and access two dimensional array elements in C programming language with help of given programme and figure.
1. Read and Accessing Array Elements using Array Subscript Operator [][]:
Example to read and Accessing 2D Array Elements using Array Subscript Operator[][]
/* The given C Programme that illustrates how to read and access two dimensional arrayarray elements.*/
#include <stdio.h>
int main()
{
/* declaration of 2D array*/
int matrix[3][3] ,i,j;
/* input or read matrix elements */
printf("\n Enter 3x3 Matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf(" \n Accesing Elements in Matrix");
/* accessing first element in the matrix at index 0,0 */
printf("\n First Element in Matrix is=: %d\n", matrix[0][0]);
/* accessing fifth element in the matrix at index 1,1 */
printf("\n Fifth Element in Matrix is=: %d\n", matrix[1][1]);
return (0);
}
output:
Enter 3x3 Matrix
11 44 33
88 34 21
76 43 29
First Element in Matrix is=: 11
Fifth Element in Matrix is=: 34
Programme Explanation: Let us learn in detail from the given diagram .
Access Array elements in C programming.
Let understand how to access two dimensional array elements from the Given Diagram .
To access any element of two dimensional array in C program use the [ ][ ] subscript operator and row index and column index value of the element.
In two dimensional array One thing we need to keep in our mind is that array row index and column index always start with 0,0 i.e. the first element in the array is at index 0,0 and the last element is at the rowsize-1, columnsize-1 where size is the number of element in the array.
The given flow digram is the explanation of the program given above used for to read 3x3 matrix elements and store them in matrix[][] 2d array.
the given flow diagram shows step by step explanation of the program how nested for loop works in the given above program.
1. Declare
int matrix[3][3],i,j;
Is the first step to declare variables that is use in the program. the variable matrix[3][3] is for to store 3x3 elements and i,j variables has used for row index and column index. The given 3x3 matrix has 3 row and 3 coluns.
2. printf("\n Enter 3x3 Matrix\n");
The printf() statement above prompt the message " Enter 3x3 Matrix".
after that control enters into the
3. nested for loop.
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix[i][j]);
}
}
the outer for loop for(i=0;i<3;i++) executes three time.
and inner for(j=0;j<3;j++) loop executes three time for each execution of outer loop.
scanf() store the elements in the given memory location.
let say we have enter the matrix.
11 44 33
88 34 21
76 43 29
so above nested for loop executes total 9 times and stores or read the entered 9 elements in the matrix[3][3] variable .
After that control comes out of nested for loop and start executing
printf(" \n Accesing Elements in Matrix");
printf("\n First Element in Matrix is=: %d\n", matrix[0][0]);
printf("\n Fifth Element in Matrix is=: %d\n", matrix[1][1]);
shows the message "Accessing Elements in Matrix".
First Element in Matrix is=:11
Fifth Element in Matrix is=:34
Access 2d array elements using subscript [][] operator.
Syntax:
array_name [row_index][column_index];
array_name: is the name of of the array variable.
[row_index][column_index]: is the row and column index of the array element to be accessed from array_name.
2. Read and Display 2D Array Elements using for loop
For loop is used to traversal array elements.
Traversal is the process of visiting every element of the array. For traversing or accessing arrays we use loops to iterate each elements of the array.
Read and Display Array Elements using for loop(C array Traversal):
// C Program to illustrate read and display array elements
#include <stdio.h>
int main()
{
/* declaration of 2D array*/
int matrix[3][3] ,i,j;
/* input or read matrix elements */
printf("\n Enter 3x3 Matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf(" \n The Elements of 3x3 Matrix are:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",matrix[i][j]);
}
printf("\n");
}
return (0);
}
Output:
Enter 3x3 Matrix:
81 54 63
80 37 20
77 43 19
The Elements of 3x3 Matrix are:
81 54 63
80 37 20
77 43 19