![]() |
| C-Two Dimensional Array | Complete Explanation with Examples |
2D Array Definition
2-D Array in C: A two-dimensional array in C is a data structure that can hold multiple values arranged in row and columns using square brackets. Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value.
Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value.
The indexing of array always start with row 0 and column index value is always integer value. Arrays is allow to store multiple values of the same data type in a single variable.
Syntax of One Dimensional Array in C
- Data_type: The array data type of data that the array will hold (int, float, char, etc.).
- Array_name: The name you choose for the 2D array.
- Row: The number of rows in the 2D array.
- Columns: The number of columns in the array.
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; |
2D Array Initialization in C
Array is the are several ways to initialize 2D arrays. The choice of initialization method depends on the requirements of your C program. Here are some common methods for initializing 2D arrays in C.
Syntax for 2D Array Initialization at Declaration
a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 |
The values to initialize the array with the number of elements inside the curly braces must match the size of the array.
We declare and initialize an integer array numbers with a size of row 3 and column size 3 and we provide 9 values.
2D Array Example Program in C
| #include<stdio.h> void main(){ clrscr(); int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; printf("Value of a[0][0]:%d\n",a[0][0]); printf("Value of a[0][1]:%d\n",a[0][1]); printf("Value of a[0][2]:%d\n",a[0][2]); printf("Value of a[1][0]:%d\n",a[1][0]); printf("Value of a[1][1]:%d\n",a[1][1]); printf("Value of a[1][2]:%d\n",a[1][2]); printf("Value of a[2][0]:%d\n",a[2][0]); printf("Value of a[2][1]:%d\n",a[2][1]); printf("Value of a[2][2]:%d\n",a[2][2]); getch(); } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
Print Array Element Using For Loop
| #include<stdio.h> void main(){ clrscr(); int i,j; int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; for(i=0;i<3;i++){ for(j=0;j<3;j++) { printf("Value of a[%d][%d]:%d\n",i,j,a[i][j]); } } getch(); } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
2d Array Matrix Program in C
| #include<stdio.h> void main(){ clrscr(); int i,j; int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; for(int i=0;i<3;i++){ for(j=0;j<3;j++) { printf("%d ",a[i][j]); } printf("\n"); } getch(); } ============OUTPUT============ 10 20 30 40 50 60 70 80 90 | 10 20 30 | 40 50 60 | 70 80 90 |
Size of 2 Dimensional Array in C
How To Get Array Length in C
| #include<stdio.h> void main(){ clrscr(); int i,j; int a[5][5] = {{20, 48, 87,56,68}, {56, 19,10,89,80},{54,56,34,68,45},{67,45,76,34,68},{44,34,56,77,55}}; for(i=0;i<5;i++){ for(j=0;j<5;j++){ printf("%d ",a[i][j]); } printf("\n"); } getch(); } ============OUTPUT============ 20 48 87 56 68 56 19 10 89 80 54 56 34 68 45 67 45 76 34 68 44 34 56 77 55 |
Run Time Initialization of 2D Array in C
How to Take User Input in 2D Array in C
| #include<stdio.h> void main(){ clrscr(); int a[3][3]; int i,j,r,c; printf("Enter Row Size:"); scanf("%d",&r); printf("Enter Colum Size:"); scanf("%d",&c); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("Enter 2D Array Element[%d][%d]:",i,j); scanf("%d",&a[i][j]); } } printf("Your 2D Matrix\n"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("%d ",a[i][j]); } printf("\n"); } getch(); } ============OUTPUT============ Enter Row Size:3 Enter Column Size:3 Enter 2D Array Elements[0][0]:12 Enter 2D Array Elements[0][1]:13 Enter 2D Array Elements[0][2]:45 Enter 2D Array Elements[1][0]:67 Enter 2D Array Elements[1][1]:87 Enter 2D Array Elements[1][2]:45 Enter 2D Array Elements[2][0]:78 Enter 2D Array Elements[2][1]:98 Enter 2D Array Elements[2][2]:12 Your 2D Matrix 12 13 45 67 87 45 78 98 12 |
Update 2D Array Elements in C
How To Update 2D Array Element in C
| #include<stdio.h> void main(){ clrscr(); int i,j,a[3][3] = {{10, 28, 80},{56, 90, 20},{20,60,70}}; printf("Before Update 2D Matrix\n"); for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ printf("%d ", a[i][j]); } printf("\n"); } printf("After Update 2D Matrix\n"); a[1][0] = 99; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ printf("%d ", a[i][j]); } printf("\n"); } getch(); } ============OUTPUT============ Before Update 2D Matrix 10 28 80 56 90 20 20 60 70 After Update 2D Matrix 10 28 80 99 90 20 20 60 70 |
2D Array Addition Program in C
void main(){ clrscr(); int x[3][3]; int y[3][3]; int z[3][3]; int i,j,r,c; printf("Enter First Matrix Row Size:"); scanf("%d",&r); printf("Enter First Matrix Colum Size:"); scanf("%d",&c); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("Enter First Matrix Array Element[%d][%d]:",i,j); scanf("%d",&x[i][j]); } } printf("Your First Matrix\n"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("%d ",x[i][j]); } printf("\n"); } printf("Enter Second Matrix Row Size:"); scanf("%d",&r); printf("Enter Second Matrix Colum Size:"); scanf("%d",&c); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("Enter Second Matrix Array Element[%d][%d]:",i,j); scanf("%d",&y[i][j]); } } printf("Your Second Matrix\n"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("%d ",y[i][j]); } printf("\n"); } printf("Addition of Two Matrix\n"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ z[i][j]=x[i][j]+y[i][j]; //sign change create more program. printf("%d ",z[i][j]); } printf("\n"); } } ============OUTPUT============ Enter First Matrix Row Size:2 Enter First Matrix Colum Size:2 Enter First Matrix Array Element[0][0]:1 Enter First Matrix Array Element[0][1]:2 Enter First Matrix Array Element[1][0]:3 Enter First Matrix Array Element[1][1]:4 Your First Matrix 1 2 3 4 Enter Second Matrix Row Size:2 Enter Second Matrix Colum Size:2 Enter Second Matrix Array Element[0][0]:5 Enter Second Matrix Array Element[0][1]:6 Enter Second Matrix Array Element[1][0]:7 Enter Second Matrix Array Element[1][1]:8 Your Second Matrix 5 6 7 8 Addition of Two Matrix 6 8 10 12 |

