read and display two 2d dimensional array in C programming Language

  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


Previous Topic:-->> Declare 2D array in C || Next topic:-->>Array Assignments.
Other Topics: