Array 2d

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

MutliDimensional array

i) Definition

It can be defined as an array of arrays

int abc [size][size] ; 2D array


float xyz [size] [size][size] ; 3D array

char a[size][size][size][size] ; 4D array

int g[size][size][size]....[size n]; nD array

ii) Declaration

one can declare N-dimensional array as follows :

data- type array-name [size1][size2][size 3]....[sizeN];

iii) Example
int a[3][4] ; // 2- D array

int b[2][3][4]; // 3-D array

size of multidimensional array is calculated by multiplying the


size of all the dimensions.
II ) Two dimensional array ( Two subscripted variable )

I) 2-d Array

i) Definition :

When an array is used to store data values(elements)


in tabulur form (table) then it called as , 2-D array.

ii) Declaration :

one can use following syntax to declare 2-d array

data-type array-name [ row ][column] ;

where ,

datatype : It is basic data type .

array -name : It is a valid name of an array .

row: This is first dimension & should be positive integer

column: This is Second dimension & should be positive


integer

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];

Memory Mapping diagram is using index

int a[0][0] = first element

Row / Column Column 0 Column 1 Column 2


Row 0 (0,0) ( 0, 1 ) ( 0, 2 )
Row 1 (1 , 0 ) (1,1) (1, 2)

In order to access data element in 1st row & 2nd column ,

a[0][1] = 1st row & 2nd column

a[0][2] = 1st row & 3rd column etc.


IV) Initialization (Passing values ) of an 2-d array

i) After declaring an 2-d array one must initialize( pass values


to) an array elements otherwise it may hold garbagevalue ( any
random value) by using one the following method :

I) compile-time initialization ( initialization method)


II) Run-time initialization

I) compile time initialization ( initialization method)


i) Just like normal variable initializtion , an 2-d array also can be
initialize at time it’s declaration .

ii) syntax
data – type array-name [x] [y] = {list of values };

iii) Example

I) int a[2][3] = { {1,2,3},{4,5,6}};

index for row : 0 to 1 & index for column : 0 to 2

a[0][0]= 1 , a[0][1] = 2 , a[0][2]=3


a[1][0]=4, a[1][1]= 5 , a[1][2]= 6
a[2][0]

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)

II) int a[2][3] ={ { 1,2,3},{4,5,6} };

III) char ab[3][2] = {{‘2’, ‘w’ },{‘m’, ‘=’},{‘w’, ‘4’} };


II) run time initialization
i) Here, one can pass values to the 2-D array by scanf() which
interms demands nested for loop .

ii) This is usefull for initializing larger arrays values .

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

1) int abc[3][3]; // valid

2) float xyz[][3] ; // valid

3) char pqr[][]; // invalid

Example :

1) WAP to declare an array , input & output.


2) WAP to find sum of two matrices
3) WAP to find product of two matrices
4) WAP to find transpose of a matrix
5) WAP to check given matrix is a sparse matrix
Hint: sparse matrix : the matrix that has a greater number of
zero elements than the non-zero elements.
1) WAP to declare an array , input & output

#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);

printf("\n Enter i/p for an array abc \n");


for(a=0 ; a<r ; a++)
{
for(b=0 ; b<c ; b++)
{
scanf("%d",&abc[a][b]);
}
}

printf("\n o/p for an array abc are:\n");

for(a=0 ; a<r ; a++)


{
for(b=0 ; b<c ; b++)
{
printf("%d\t",abc[a][b]);
}
printf("\n");
}
}
O/P :
Enter the dimension i.e.row & column for an array abc
2
3

Enter i/p for an array abc


11
22
33
44
55
66

o/p for an array abc are:


11 22 33
44 55 66
2) WAP to find sum of two matrices ( Addition )

case I : Matrices without dimension

// Program to perform addition of 2 matrices

#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);

printf("\n Enter the i/p for 1st matrix\n");


for(a=0; a<r ;a+=1)
{
for(b=0 ;b<c ; b++)
{
scanf("%d",&mat1[a][b]);
}
}

printf("\n Enter the i/p for 2nd matrix\n");


for(a=0; a<r ;a+=1)
{
for(b=0 ;b<c ; b++)
{
scanf("%d",&mat2[a][b]);
}
}
printf("\n I/p Matrices are :\n");

printf("\n 1st matrix \n");


for(a=0; a<r ;a+=1)
{
for(b=0 ;b<c ; b++)
{
printf("%d\t",mat1[a][b]);
}
printf("\n");
}

printf("\n2nd matrix \n");


for(a=0; a<r ;a+=1)
{
for(b=0 ;b<c ; b++)
{
printf("%d\t",mat2[a][b]);
}
printf("\n");
}

for(a=0; a<r ;a+=1)


{
for(b=0 ;b<c ; b++)
{
add[a][b] = mat1[a][b] + mat2[a][b] ;
}
printf("\n");
}
printf("\nAddition \n");
for(a=0; a<r ;a+=1)
{
for(b=0 ;b<c ; b++)
{
printf("%d\t",add[a][b]);
}
printf("\n");
}
}

O/P :-
Enter the dimension i.e.row & column for an array abc
2
3

Enter the i/p for 1st matrix


1
2
3
4
5
6

Enter the i/p for 2nd matrix


6
5
4
3
2
1
I/p Matrices are :

1st matrix
1 2 3
4 5 6

2nd matrix
6 5 4
3 2 1

Addition
7 7 7
7 7 7

case II : Matrices with dimension

// Program to perform addition of 2 matrices 3*4

#include<stdio.h>

void main()
{
int mat1[3][4], mat2[3][4] ,add[3][4];
int a,b;

printf("\n Enter the i/p for 1st matrix\n");


for(a=0; a<=2 ;a+=1)
{
for(b=0 ;b<=3 ; b++)
{
scanf("%d",&mat1[a][b]);
}
}
printf("\n Enter the i/p for 2nd matrix\n");
for(a=0; a<=2 ;a+=1)
{
for(b=0 ;b<=3 ; b++)
{
scanf("%d",&mat2[a][b]);
}
}

printf("\n I/p Matrices are :\n");


printf("\n 1st matrix \n");
for(a=0; a<=2 ;a+=1)
{
for(b=0 ;b<=3 ; b++)
{
printf("%d\t",mat1[a][b]);
}
printf("\n");
}
printf("\n2nd matrix \n");
for(a=0; a<=2 ;a+=1)
{
for(b=0 ;b<=3 ; b++)
{
printf("%d\t",mat2[a][b]);
}
printf("\n");
}

for(a=0; a<=2 ;a+=1)


{
for(b=0 ;b<=3 ; b++)
{
add[a][b] = mat1[a][b] + mat2[a][b] ;
}
printf("\n");
}

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

Enter the i/p for 2nd matrix


12
11
10
9
8
7
6
5
4
3
2
1

I/p Matrices are :

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 :

WAP to perform subtraction of two matrices 3*3


3) WAP to find product of two matrices

// Multiplication of 2 d matrices

#include<stdio.h>

void main()
{
int mat1[100][100] , mat2[100][100] , mat[100][100]={0,0};

int r,c, a,b,k;

printf("\n Enter the value of row & column \n");


scanf("%d%d",&r,&c);

printf("\Enter the i/p for first matrix \n");


for(a=0 ; a<r ; a++ )
{
for(b=0 ; b<c; b+=1)
{
scanf("%d",&mat1[a][b]);
}
}

printf("\Enter the i/p for second matrix \n");


for(a=0 ; a<r ; a++ )
{
for(b=0 ; b<c; b+=1)
{
scanf("%d",&mat2[a][b]);
}
}
printf("\n I/p matrices are :\n");

printf("\n 1st matrix \n");

for(a=0 ; a<r ; a++ )


{
for(b=0 ; b<c; b+=1)
{
printf("%d\t",mat1[a][b]);
}
printf("\n");
}

printf("\n 2nd matrix \n");

for(a=0 ; a<r ; a++ )


{
for(b=0 ; b<c; b+=1)
{
printf("%d\t",mat2[a][b]);
}
printf("\n");
}

for(a=0 ; a<r ; a++ )


{
for(b=0 ; b<c; b+=1)
{
for(k=0; k<c; k++)
{
mat[a][b]=mat[a][b] + mat1[a][k]*mat2[k][b] ;

}
}
}

printf("\n Multiplication :\n");

for(a=0 ; a<r ; a++ )


{
for(b=0 ; b<c; b+=1)
{
printf("%d\t",mat[a][b]);
}
printf("\n");
}

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

I/p matrices are :

1st matrix
2 3
4 5

2nd matrix
6 7
8 9

Multiplication :
36 41
64 73

Do by yourself :

WAP to perform multiplication of two 3*2 matrices


4) WAP to find transpose of a matrix

// Transpose of a matrix

#include<stdio.h>
void main()
{
int mat[100][100] , t[100][100];

int r,c, a,b,k;

printf("\n Enter the value of row & column \n");


scanf("%d%d",&r,&c);

printf("\Enter the i/p for matrix \n");


for(a=0 ; a<r ; a++ )
{
for(b=0 ; b<c; b+=1)
{
scanf("%d",&mat[a][b]);
}
}
printf("\n matrix :\n");

for(a=0 ; a<r ; a++ )


{
for(b=0 ; b<c; b+=1)
{
printf("%d\t",mat[a][b]);
}
printf("\n");
}
for(a=0; a<r ; a++ )
{
for(b=0;b<c;b++)
{
t[b][a] = mat[a][b];
}
}
printf("\n Transpose of a matrix is :\n");
for(b=0;b<c;b++)
{
for(a=0;a<r;a++)
{
printf("\t %d",t[b][a]);
}
printf("\n");
}

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 :

WAP to calculate transpose of 3*2 matrice


5) WAP to check given matrix is a sparse matrix

// sparse matrix

#include<stdio.h>
void main()
{
int mat[100][100] , r, c,count=0,a,b;

printf("\n Enter the rows & column for i/p matrix\n");


scanf("%d%d",&r,&c);

printf("\n Enter the i/p matrix\n");

for(a=0; a<r; a++ )


{
for(b=0; b<c; b+=1)
{
scanf("%d",&mat[a][b]);
}
}

for(a=0; a<r; a++ )


{
for(b=0; b<c; b+=1)
{
if(mat[a][b] == 0 )
count++;
}
}
if(count > (r*c)/2)
printf("\n Given matrix is sparse matrix \n");
else
printf("\n Given matrix is non-sparse matrix\n");
}
O/P:-
Enter the rows & column for i/p matrix
2
3

Enter the i/p matrix


4
5
0
60
0
0

Given matrix is non-sparse matrix

You might also like