0% found this document useful (0 votes)
19 views

2D Array

notes for 2nd sem

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

2D Array

notes for 2nd sem

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Two dimensional Arrays

2D array consists of element of same type arranged in rows & columns. It has two subscripts (indices).

Declaration of two dimensional arrays:


As we declare and define variables before they are used in a program, an array also must be declared and
defined before it is used. The declaration and definition informs the compiler about the:
 Type of each element of the array.
 Name of the array
 Number of elements( size of the array)

The syntax is shown below:


data_type array_name [row size ][column size] ;
Where data type can be int, float , char or double.
array_name is name of the array,
rowsize ,column size are the integral constants thatspecify the number of rows and columns in the
array.
For Ex: int a[3][4];

Initialization of two-Dimensional Array


There are 2 ways of initialization:
i) Compile time
ii) Run time

i) Compile time Initialization:


Array elements are initialized at the time of declaration. The syntax is shown below:
data_type array_name[row_size][column_size] = { { a1, a2, a3, . . . an },{b1,b2…bn}….} ;
where data_ type can be int, float char or double.
ii) Run time Initialization:
An array can be explicitly initialized at run time using for loop. Ex: int i,j,m=3,n=2,a[10][10];
for(i=0;i<m;i++) for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
Accessing Matrix elements:
There are two ways:
i)Row major
ii) Column major

Row major order: firstly all elements in 0th row are accessed, then all elements in 1st row areaccessed &
so on.
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}

Column major order: firstly all elements in 0th column are accessed, then all elements in 1stcolumn are
accessed & so on.
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i][j]);
}
}

C program to read and display elements using 2D ARRAY


#include<stdio.h>
int main()
{
int i,j ,m, n, a[20][20];
printf(“enter the number of rows & columns \n”);
scanf(“%d %d”,&m,&n);
printf(“enter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“array elements are:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,a[i][j]);
}
}
return 0;
}
OPERATIONS ON TWO DIMENSIONAL ARRAY
1. Transpose

#include<stdio.h>
int main()
{
int i,j ,m, n, a[20][20],b[10][10];
printf(“enter the number of rows & columns \n”);
scanf(“%d %d”,&m,&n);
printf(“enter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=a[i][j];
}
}
printf(“transpose of the matrix”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(“%d\t”,b[i][j]);
}
}
return 0;
}
2. Sum

#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
}
printf("\n");
}
printf("Sum of entered matrices:");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}

return 0;
}

3. Difference

4. Product

program to perform matrix multiplication


#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j,k ;
printf("Enter the size of first matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter the size of second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
Printf(“Matrix Addition is not possible”);
exit());
}
printf("Enter the elements of first matrix\n") ;
for(i=0 ;i<r1 ;i++)
{
for(j=0 ;j<c1 ;j++)
{
scanf("%d",&a[i][j]) ;
}
}
printf("Enter the elements of second matrix\n") ;
for( i=0 ;i<r2 ;i++)
{
for(j=0 ;j<c2 ;j++)
{
scanf("%d",&b[i][j]) ;
}
}
printf("matrix A is\n") ;
for(i=0 ;i<r1 ;i++)
{
for(j=0 ;j<c1 ;j++)
{
printf("%d\t",a[i][j]) ;
}
printf("\n") ;
}
printf("matrix B is\n") ;
for(i=0 ;i<r2 ;i++)
{
for(j=0 ;j<c2 ;j++)
{
printf("%d\t",b[i][j]) ;
}
printf("\n") ;
}

for(i=0 ;i<m ;i++)


{
for(j=0 ;j<q ;j++)
{
c[i][j]=0 ;
for(k=0 ;k<n ;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j] ;
}
}

}
printf("the product of two matrices is\n") ;
for(i=0 ;i<m ;i++)
{
for(j=0 ;j<q ;j++)
{
printf("%d\t",c[i][j]) ;
}
printf("\n")
}
return 0;
}

Passing the entire 2D array


To pass a two dimensional array to a function, we use the array name as the actual parameter.
(The same we did in case of a 1D array). However, the parameter in the called function must
indicate that the array has two dimensions.
PROGRAM ILLUSTRATING PASSING ENTIRE ARRAY TO A FUNCTION
#include<stdio.h>
void read_matrix(int mat[][], int, int);
void sum_matrix(int mat1[][], int mat2[][], int, int);
void display_matrix(int mat[5][5], int r, int c);
int main()
{
int row, col, mat1[5][5], mat2[5][5];
printf(“\n Enter the number of rows and columns of the matrix : “);
scanf(“%d %d”, row, col);
read_matrix(mat1, row, col);
printf(“\n Enter the second matrix : “);
read_matrix(mat2, row, col);
sum_matrix(mat1, mat2, row, col);
display_matrix(sum, r, c);
}
void read_matrix(int mat[5][5], int r, int c)
{
int i, j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d“, &mat[i][j]);
}
}
}
void sum_matrix(int mat1[5][5], mat2[5][5], int r, int c)
{
int i, j, sum[5][5];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void display_matrix(int mat[5][5], int r, int c)
{
int i, j;
for(i=0;i<r;i++)
{
printf(“\n”);
for(j=0;j<c;j++)
{
printf(“\t mat[%d][%d] = %d“, mat[i][j]);
}
}
}

You might also like