# 2-D array in C language

“F.E.A.R.
has two meaning-
Forget Everything And Run
OR
Face Everything And Rise
The choice is yours”

Hello everyone!!!!
Hope you all are doing good…..
Today we are going to discuss 2-D array in detail.
Soo let’s start quickly 🙂

When the number of dimensions specified is two then it is called as a two-dimensional array.

•Syntax of two dimensional array is:-
datatype arryname[max_rows][max_columns];

•Initializing 2D Arrays:-
We know that, when we declare and initialize one dimensional array in C programming simultaneously, we Don’t need to specify the size of the array. However this will not work with 2D arrays. We will have to define at least the second dimension of the array.
int arr = {0,1,2,3};

There are two main techniques of storing 2D array elements into memory.

1. Row Major ordering :-

• In row major ordering, all the rows of the 2D array are stored into the memory contiguously. considering the array shown in the above image, its memory allocation according to row major order is shown as follows.
• First, the 1st row of the array is stored into the memory completely, then the 2nd row of the array is stored into the memory completely and so on till the last row.

2) Column Major ordering:-

• According to the column major ordering, all the columns of the 2D array are stored into the memory contiguously. First, the 1st column of the array is stored into the memory completely, then the 2nd column of the array is stored into the memory completely and so on till the last column of the array.

Passing array to function:-
•In C, there are various general problems which requires passing more than one variable of the same type to a function. For example, consider a function which sorts the 10 elements in ascending order. Such a function requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different numbers and then passing into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity since the function will now work for any number of values.
•As we know that the array_name contains the address of the first element. Here, we must notice that we need to pass only the name of the array in the function which is intended to accept an array. The array defined as the formal parameter will automatically refer to the array specified by the array name defined as an actual parameter.

•Syntax of function prototype:-
Return type functionname(arrayname);

•Syntax of function call:-
Functionname(arrayname);

•Syntax of function definition
Return type functionname(arrayname)
{
….
}

This is all about explanation part. Let’s move towards some interesting examples using 2-D array, which will help us to understand the above concept more clearly.

Q1) Write a ‘C’ program to add two matrices of order mXn.

#include<stdio.h>
int main()
{
int a,b,c,i,j;
printf(“Enter first matrix:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter second matrix:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t%d”, c[i][j]);
}
}
}

Output:-
Enter first matrix:-1 2 3 4 5 6 7 8 9
Enter second matrix:-0 6 7 3 2 1 6 5 4.
Addition of two matrices is:- 1 8 10 7 7 7 13 13 13

Q2) Write ‘C’ program to subtract two matrices of order mXn.

#include<stdio.h>
int main()
{
int a,b,c,i, j;
printf(“Enter first matrix:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“\t%d”,&a[i][j]);
}
}
printf(“Enter second matrix:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf (“\t%d”, &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf(“Subtraction of two matrices is:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t%d”, c[i][j]);
}
}
}

Output:-
Enter first matrix:-1 2 3 4 5 6 7 8 9

Enter second matrix:-1 0 9 2 3 4 8 7 6

Subtraction of two matrices is:- 0 2 -6 2 2 2 -1 1 3

Q3) Write a ‘C’ program to accept two matrices of size m x n and find multiplication of Matrices.

#include<stdio.h>
main()
{
int a,b,c,m, n, i, j, k;
printf(“enter the number of row=”);
scanf(“%d”, &m);
printf(“enter the number of column=”);
scanf(“%d”, &n);
printf(“enter the first matrix element=\n”);
for(i=0;i<m; i++)
{
for (j=0;j<n; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“enter the second matrix element=\t”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf (“%d”, &b[i][j]);
}
}
printf(“multiplication of the matrices is=\t”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0; k<n; k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}

Output:-
enter the number of row=2
enter the number of column=2
enter the first matrix element=1 2 3 4
enter the second matrix element=3 4 5 6
multiplication of the matrices is= 13 16 29 36

Q4) Write a ‘C’ program to accept a matrix of size m x n and display transpose of a given Matrix.

#include<stdio.h>
main()
{
int a,i, j;
printf(“Enter first matrix:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{

scanf(“%d”,&a[i][j]);
}
}
printf(“Transpose of matrix is:-“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t%d”, a[j][i]);
}
}
}

Output:-
Enter first matrix:-1 2 3 4 5 6 7 8 9
Transpose of matrix is:- 1 4 7 2 5 8 3 6 9

Q5) Write a C program to display sum of diagonal elements of matrix.

#include<stdio.h>
void main()
{
int a;
int i, j, m, n, sum=0;
printf(“Enter the number of rows and columns for 1st matrix:-\n”);
scanf(“%d %d”, &m, &n);
printf(“Enter the elements of the matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“The matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum= sum+ a[i][j];
}
}
}
printf(“The sum of diagonal elements of a square matrix = %d\n”, sum);
}

Output:-
Enter the number of rows and columns for 1st matrix:-
2
2
Enter the elements of the matrix
1
2
3
4
The matrix
1 2
3 4
The sum of diagonal elements of a square matrix = 5

Soo this is all about 2-D array.