A two dimensional array is an array that has two dimensions like rows and columns. The total number of elements in a two dimensional array is calculated by multiplying the number of rows and columns. A two dimensional array can be accessed using two indices like A[i][j] where i represents the row and j represents the column. Common operations on two dimensional arrays include storing and retrieving elements, finding the sum of boundary elements, finding the sum of diagonal elements, adding, subtracting and multiplying two dimensional arrays.
Two Dimensional Array Definition, Operations & Examples in C
1. Two Dimensional Array
Definition
Two dimensional array is an array that has two dimensions, such as row and column. Total number
of elements in a two dimensional array can be calculated by multiplication of the numbers of rows
and the number of columns. If there are m rows and n columns then the total number of elements
is mxn, and mxn is called the size of the array. Of course, the data elements of the array will be
same type.
In mathematics, the two dimensional array can be expressed as follows:
Aij or A[I,j] for 1<=i<=m and 1<=j<=n (where m and n are the number of rows and columns
respectively)
A[1…….m, 1………n]
m rows n columns
Fig 1: Symbolic representation of two dimensional array
1 2 3 4 5 6 7
1 0 10 12 13 19 20 18
2 23 5 63 72 79 80 65
3 - - - - - - -
4 - - - - - - -
5 - - - - 75 - -
6 20 31 32 33 39 40 33
Fig 2: Graphical representation of two dimensional array
Store and retrieve values in and from a 2-D array
for(i=0; i<2; i++) {
for(j=0;j<3;j++)
scanf("%d", &B[i][j]);
}
for(i=0; i<2; i++) {
for(j=0;j<3;j++)
printf("%d ", B[i][j]);
}
Array B
Size=6x7
Cell B[5][5]
2. C program to store and retrieve values in and from a 2-D array
#include<stdio.h>
int main(){
int disp[10][10];
int m,n;
int i, j;
printf("Enter the number of row:n");
scanf("%d",&m);
printf("Enter the number of column:n");
scanf("%d",&n);
for(i=0; i<m; i++) {
for(j=0;j<n;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:n");
for(i=0; i<m; i++) {
for(j=0;j<n;j++) {
printf("%dt", disp[i][j]);
if(j==n-1){
printf("n");
}
}
}
return 0;
}
3. Sample Output
Enter the number of row:
3
Enter the number of column:
4
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[0][3]:4
Enter value for disp[1][0]:5
Enter value for disp[1][1]:6
Enter value for disp[1][2]:7
Enter value for disp[1][3]:8
Enter value for disp[2][0]:9
Enter value for disp[2][1]:10
Enter value for disp[2][2]:11
Enter value for disp[2][3]:12
Two Dimensional array elements:
1 2 3 4
5 6 7 8
9 10 11 12
Algorithm to find out the summation of boundary elements
1. Input: a two-dimensional array
A[1….m,1….n]
Sum=0;
2. Find each boundary element
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
4. if(i = 1 || j = 1 || i = m || j = n), sum=sum+A[i.j],
3. Output: Print sum as the result of summation of boundary elements
C program to find out the summation of boundary elements
#include<stdio.h>
int main (){
int i,j, arr[100][100],sum = 0,row,col;
printf("Enter the number of row: ");
scanf("%d",&row);
printf("Enter the number of column: ");
scanf("%d",&col);
printf("Enter row and column n");
for(i = 1; i <= row; i++){
for(j = 1; j <= col; j++){
scanf("%d",&arr[i][j]);
}
}
for(i = 1; i <= row; i++){
for(j = 1; j <= col; j++){
if(i == 1 || j == 1 || i == row || j == col){
sum+=arr[i][j];
}
}
}
printf("Sum of boundary is: %d ",sum);
return 0;
}
5. Sample Output
Enter the number of row: 3
Enter the number of column: 3
Enter row and column
1 2 3
4 5 6
7 8 9
Sum of boundary is: 40
Algorithm to find out the summation of diagonal elements
1. Input: a two-dimensional array
B[1….n,1….n]
Sum=0;
2. Find each diagonal element and add them with sum
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(i = j || i+j = n+1), sum=sum+B[i.j],
3. Output: Print sum as the result of summation of diagonal elements
C program to find out the summation of diagonal elements
#include<stdio.h>
void main()
{
int B[10][10];
int i,j,n,sum=0;
printf("Enter the number of rows and columns for 1st
matrixn");
scanf("%d%d",&n,&n);
printf("Enter the elements of the matrixn");
for(i=1;i<=n;i++)
{
7. Sample Output
Enter the number of rows and columns for 1st matrix
3 3
Enter the elements of the matrix
1 2 3
4 5 6
7 8 9
The matrix
1 2 3
4 5 6
7 8 9
The sum of diagonal elements of a square matrix = 25
Algorithm for matrix addition
Step 1: Start
Step 2: Declare matrix A[r][c];
and matrix B[r][c];
and matrix C[r][c]; r= no. of rows, c= no. of columns
Step 3: Read r, c, A[][] and B[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < r
5.1: Repeat until j < c
C[i][j]=A[i][j] + B[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix after addition
Step 7: Stop
8. C program for matrix addition
//A matrix can only be added to (or subtracted from) another
matrix if the two matrices have the same dimensions .
#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 matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%dt", sum[c][d]);
}
printf("n");
}
return 0;
}
9. Sample Output
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
2 1
Sum of entered matrices:-
6 8
5 5
Algorithm for matrix subtraction
Step 1: Start
Step 2: Declare matrix A[r][c] // Matrix 1;
and matrix B[r][c] // Matrix 2;
and matrix C[r][c]; r= no. of rows, c= no. of columns
Step 3: Read r, c, A[][] and B[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < r
5.1: Repeat until j < c
C[i][j]=A[i][j] - B[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix after subtraction
Step 7: Stop
10. C Program to perform matrix subtraction
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10],
difference[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &second[c][d]);
printf("Difference of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
difference[c][d] = first[c][d] - second[c][d];
printf("%dt",difference[c][d]);
}
printf("n");
}
return 0;
}
11. Sample Output
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
4 3
2 1
Enter the elements of second matrix
1 2
1 1
Difference of entered matrices:-
3 1
1 0
Algorithm for matrix multiplication
Step 1: Start
Step 2: Declare matrix A[m][n]
and matrix B[p][q]
and matrix C[m][q]
Step 3: Read m, n, p, q.
Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be
multiplied and an error message is generated.
Step 5: Read A[][] and B[][]
Step 4: Declare variable i=0, k=0 , j=0 and sum=0
Step 5: Repeat Step until i < m
5.1: Repeat Step until j < q
5.1.1: Repeat Step until k < p
Set sum= sum + A[i][k] * B[k][j]
Set multiply[i][j] = sum;
Set sum = 0 and k=k+1
12. 5.1.2: Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix.
Step 7: Stop
C Program to perform matrix multiplication
//To multiply two matrices, the number of columns of the first
matrix should be equal to the number of rows of the second
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first
matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second
matrixn");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied
with each other.n");
else
{
printf("Enter the elements of second matrixn");
for ( c = 0 ; c < p ; c++ )
13. for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%dt", multiply[c][d]);
printf("n");
}
}
return 0;
}
14. Sample Output
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix
1 2 0
0 1 1
2 0 1
Enter the number of rows and columns of second matrix
3 3
Enter the elements of second matrix
1 1 2
2 1 1
1 2 1
Product of entered matrices:-
5 3 4
3 3 2
3 4 5