Professional Documents
Culture Documents
Array 2d
Array 2d
Array 2d
i) Definition
ii) Declaration
iii) Example
int a[3][4] ; // 2- D array
I) 2-d Array
i) Definition :
ii) Declaration :
where ,
iii) Example
i) int abc [3][2]; // 3*2 matrix
ii) float pqr[3][3]; // 3*3 matrix
III) accessing an array elements
i) Here, one can access an 2-d array using row index & column
index.
Consider Example ,
int a [2][3];
ii) syntax
data – type array-name [x] [y] = {list of values };
iii) Example
Row / Column C0 C1 C2
R0 1 ( 0,0 ) 2 (0,1) 3 (0,2)
R1 4 (1,0) 5 ( 1,1) 6 (1,2)
Iii) Example
#include<stdio.h>
void main( )
{
int abc[3][2];
int a,b ;
printf(“Enter values to data elements for an array abc”);
for(a=0 ; a<3;a++)
{
for(b=0;b<2;b++)
{
scanf(“%d”,&abc[a][b]); //
}
}
for(a=0;a<15;a++)
{
for(b=0;b<2;b++)
{
printf(“%d”,abc[a][b]);
}
printf(“\n”);
}
}
points to remember:
The second subscript is mandatory where as first
subscript is optional
Example :
#include<stdio.h>
void main()
{
int abc[100][100];
int r,c;
int a,b;
printf("\n Enter the dimension i.e.row & column for an array \n");
scanf("%d%d",&r,&c);
#include<stdio.h>
void main()
{
int mat1[100][100], mat2[100][100] ,add[100][100];
int r,c; // variables representing row & column subscript
int a,b;
printf("\n Enter the dimension i.e.row & column for an array abc \n");
scanf("%d%d",&r,&c);
O/P :-
Enter the dimension i.e.row & column for an array abc
2
3
1st matrix
1 2 3
4 5 6
2nd matrix
6 5 4
3 2 1
Addition
7 7 7
7 7 7
#include<stdio.h>
void main()
{
int mat1[3][4], mat2[3][4] ,add[3][4];
int a,b;
printf("\nAddition \n");
for(a=0; a<=2 ;a+=1)
{
for(b=0 ;b<=3 ; b++)
{
printf("%d\t",add[a][b]);
}
printf("\n");
}
}
O/P :-
Enter the i/p for 1st matrix
1
2
3
4
5
6
7
8
9
10
11
12
1st matrix
1 2 3 4
5 6 7 8
9 10 11 12
2nd matrix
12 11 10 9
8 7 6 5
4 3 2 1
Addition
13 13 13 13
13 13 13 13
13 13 13 13
Do by yourself :
// Multiplication of 2 d matrices
#include<stdio.h>
void main()
{
int mat1[100][100] , mat2[100][100] , mat[100][100]={0,0};
}
}
}
O/P:-
Enter the value of row & column
2
2
ter the i/p for first matrix
2
3
4
5
ter the i/p for second matrix
6
7
8
9
1st matrix
2 3
4 5
2nd matrix
6 7
8 9
Multiplication :
36 41
64 73
Do by yourself :
// Transpose of a matrix
#include<stdio.h>
void main()
{
int mat[100][100] , t[100][100];
O/P:-
Enter the value of row & column
2
3
enter the i/p for matrix
2
3
4
5
6
7
matrix :
2 3 4
5 6 7
Transpose of a matrix is :
2 5
3 6
4 7
Do by yourself :
// sparse matrix
#include<stdio.h>
void main()
{
int mat[100][100] , r, c,count=0,a,b;