You are on page 1of 21

2-Dimensional Array

19-05-2022 1
Definition

• The two-dimensional array can be defined as an


array of arrays.
• The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
• 2D arrays are created to implement a relational
database lookalike data structure.
• It provides ease of holding the bulk of data at once
which can be passed to any number of functions
wherever required.

19-05-2022 2
Declaration of two dimensional Array in C

• The syntax to declare the 2D array is given below :-


data_type array_name[rows][columns];
• Consider the following example :-
int twodimen[4][3];
• Here, 4 is the number of rows, and 3 is the number
of columns.

19-05-2022 3
Initialization of 2D Array in C

• An array can be initialized in two ways, which are as


follows:
1. Compile time initialization.
2. Runtime initialization.

19-05-2022 4
Compile time initialization
#include<stdio.h>
main ( )
{
int a[3][3] = {10,20,30,40,50,60,70,80,90};
int i,j;
printf ("elements of the array are");
for ( i=0; i<3; i++)
{
for (j=0;j<3; j++)
{
printf("%d \t", a[i] [j]);
}
printf("\n");
}
}
19-05-2022 5
Output

elements of the array are:


10 20 30
40 50 60
70 80 90

19-05-2022 6
Run time initialization
#include<stdio.h>
main ( )
{
int a[3][3] ,i,j;
printf ("enter elements of array");
for ( i=0; i<3; i++)
{
for (j=0;j<3; j++)
{
scanf("%d", &a[i] [j]);
}
}
printf("elements of the array are");
for ( i=0; i<3; i++)
{
for (j=0;j<3; j++)
{
printf("%d\t", a[i] [j]);
}
printf("\n");
}
}
19-05-2022 7
Output

Enter elements of array : 1 2 3 4 5 6 7 8 9


Elements of the array are
1 2 3
4 5 6
7 8 9

19-05-2022 8
Two-dimensional array example in C
#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
for(i=0;i<4;i++) //traversing 2D array
{
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}
}
return 0;
}
19-05-2022 9
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
19-05-2022 10
Storing elements in a matrix and printing it.
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
19-05-2022 11
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
printing the elements ....
56 10 30
34 21 34
45 56 78
19-05-2022 12
Sum of two matrices
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
} // Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
} // adding corresponding elements of two arrays
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
} // Displaying the sum
printf("\nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("%.1f\t", result[i][j]);
if (j == 1)
printf("\n");
}
return 0;
}
19-05-2022 13
Output
Enter elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
19-05-2022 14
Multiplication of two matrices
#include<stdio.h>
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]);
}
}
19-05-2022 15
Contd..
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
19-05-2022 16
Output
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

19-05-2022 17
Transpose of a matrix
#include<stdio.h>
void main()
{
int a[10][10], b[10][10], m, n, i, j;
printf("\nEnter number of rows & columns of array : ");
scanf("%d %d", &m, &n);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\n\n2-D array before transposing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\t%d", a[i][j]);
}
printf("\n\n");
}
19-05-2022 18
Contd..
/* Transposing array */
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
b[j][i] = a[i][j];
}
}
printf("\n\n2-D array after transposing:\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n\n");
}
getch();
}
19-05-2022 19
Output
Enter number of rows & columns of array : 3 x 3
Enter elements of 2-D array:
25 12 4 62 34 23 6 4 3
2-D array before transposing:
25 12 4
62 34 23
6 4 3
2-D array after transposing:
25 62 6
12 34 4
4 23 3
19-05-2022 20
19-05-2022

You might also like