Arrays - 2D
Double-Dimensional Arrays or 2d-Arrays are the combination of rows and columns. Normally, a 2D-Array is use to represent data in the form of matrix.
Syntax to declare 2d-Arrays:
int a[3][4]; //3 rows and 4 columns
In the above syntax, 1st square bracket represents number of rows and 2nd square bracket represent number of columns. The above declaration tells the compiler that we are declaring 3 rows and 3 columns total 9 elements. We can specify any value in row and column.
Example 1: WAP to input 3x3 matrix and print it.
#include<stdio.h>
void main()
{
int i, j, a[3][3];
printf("Enter 3x3 matrix");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);
}
printf("Your 3x3 matrix is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
}
int a[3][3] = {{10,20,30}, {40,50,60}, {70,80,90} };
Example 2: WAP to initialize the matrix.
#include<stdio.h>
void main()
{
int a[3][3] = { {10,20,30}, {40,50,60}, {70,80,90} };
int i, j;
printf("\n3x3 Matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}