2-D Array: Syntax: Data - Type: Type of Data To Be Stored
2-D Array: Syntax: Data - Type: Type of Data To Be Stored
Syntax:
data_type array_name[x][y];
data_type: Type of data to be stored
Elements in two-dimensional arrays are commonly referred by x[i][j] where
i is the row number and ‘j’ is the column number.
A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’
columns where the row number ranges from 0 to (x-1) and column number
ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3
columns is shown below:
Initializing Two – Dimensional Arrays: There are two ways in which a Two-
Dimensional array can be initialized.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array have 3 rows and 4 columns. The elements in the braces from
left to right are stored in the table also from left to right. The elements will be
filled in the array in the order, first 4 elements from the left in first row, next 4
elements in second row and so on.
Q.1) WAP in C to input the elements of 3x3 matrix and print them.
#include <stdio.h>
void main ()
scanf("%d",&arr[i][j]);
for(i=0;i<3;i++)
for (j=0;j<3;j++)
printf("%d\t",arr[i][j]);
}
}
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
56 10 30
34 21 34
45 56 78
Q.2) WAP in C to input the matrix of 3x3 order and find the transpose of the
matrix.
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],i,j; /*a is the original matrix and b will contain the
transpose*/
printf("\nEnter the elements of matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe Transpose of matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i]; /*transpose means interchanging the rows and columns of
original matrix*/
printf("%d\t",b[i][j]);
}
}
return 0;
}
//OUTPUT:
//Enter the elements of matrix:
123456789
//The Transpose of matrix is:
147
258
369
/* we need 3 matrices a,b,sum i.e sum[i][j]=a[i][j]+b[i][j]. the size of the matrix is not given
so we cannot declare the matrix as a[m][n] because variable name cannot be written. so
assumption as we done earlier.a[10][10] ,b[10][10] sum[10][10]*/
#include <stdio.h>
int main() {
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows (between 1 and 10): ");
scanf("%d", &r); //the actual no of row size inputted by user
printf("Enter the number of columns (between 1 and 10): ");
scanf("%d", &c); //the actual no of row size inputted by user
}
}
return 0;
}
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
10 8 6
Q.4) WAP in C to multiply two matrices A[r][c] and B[r][c] and display the
resultant matix.
A user inputs the orders and elements of the matrices. If the multiplication isn't possible, an
error message is displayed. You may have studied the method to multiply matrices in
Mathematics.
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
Output:
Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the
figure given below: