Accessing 2-D Array Elements In C Programming
Accessing Array Elements
- To Access Every 2-D Array we requires 2 Subscript variables.
- i - Refers the Row number
- j - Refers Column Number
- a[1][0] refers element belonging to first row and zeroth column
Accept & Print 2×2 Matrix from user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> int main() { int i, j, a[3][3]; // i : For Counting Rows // j : For Counting Columns for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("\nEnter the a[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } } //Print array elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d\t", a[i][j]); } printf("\n"); } return (0); } |
How it Works ?
- For Every value of row Subscript , the column Subscript incremented from 0 to n-1 columns
- i.e For Zeroth row it will accept zeroth,first,second column ( a[0][0],a[0][1],a[0][2]) elements
- In Next Iteration Row number will be incremented by 1 and the column number again initialized to 0 .
Accessing 2-D Array
1 | a[i][j] == Element From i-th Row and j-th Column |