Two Dimensional Array
Two Dimensional Array
The Two-dimensional array is also called a matrix. a two- dimensional array is nothing but a collection of a
number of one- dimensional arrays placed one below the other.
Declaration of Two- dimensional array
Syntax:
Syntax: Storage class Datatype Arrayname[size1][size2];
Where size1 represent the number of rows
Size2 represent the number of columns
int s[ 5 ][ 2 ] ;
Initializing a Two-Dimensional Array
RowWise Assignment
int stud[ 4 ][ 2 ] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
};
Combine Assignment: Remember that, while initializing a 2-D array, it is necessary to mention the second
(column) dimension, whereas the first dimension (row) is optional.
int arr[ 2 ][ 3 ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ 3 ] = { 12, 34, 23, 45, 56, 45 };
Selective Assignment
int stud[ 4 ][ 2 ] = {
{ 56 },
{ 1, 33 },
{ 80 },
{ 2, 78 }
};
output;
56 0
1 33
80 0
2 78
//Write a program to add two matrices of dimension 3*3 and store the result in another matrix.
#include<stdio.h>
void main()
{
int i, j,a[3][3], b[3][3], c[3][3];
//input in first matrix
printf("Insert your matrix elements in first matrix elements :\n");
for (i= 0; i< 3; i++)
{
for (j = 0; j< 3; j++)
{
scanf("%d",&a[i][j]);
}
}
//input in second matrix
printf(" Insert your matrix elements in second matrix elements :\n");
for (i= 0; i< 3; i++)
{
// Write a program in C to input two 3x3 matrix from the user and print multiplication as the result in
matrix form. //(Write comments also at appropriate places in the program)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[3][3],b[3][3],c[3][3];
//input in first matrix
printf("\nenter ist matrices\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
//input in 2nd matrix
printf("\nenter 2nd matrices\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
//display of ist matrix
printf("\n ist matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//display of 2nd matrix
printf("\n second matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
//logic of matrix multiplication
printf("\nMultiplicaiton of matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
//Display the result of matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}