C – Multidimensional Array

After reading this C multidimensional array topic, you will understand its theory and examples also you will able to implement two-dimensional array in C programming.

Often there is a need to store and manipulate two-dimensional data structure such as tables and matrices. The C Multidimensional Array facility helps to manipulate tables, matrices, etc. in programming.

An array with a finite number of same type elements where each element referred by more than one index and this array called as multidimensional array. The simplest form of multidimensional array is two-dimensional array.

Declaration of a two-dimensional array

Let us consider two dimensional array to be declared for example:

int ra[5][4];

Indicate that

  •  ra is the name of the two-dimensional array.
  • int represent data type is integer.
  • The size of two dimensional array is 20 (4×5=20).

Example:

Program (1): To accept elements of two dimensional arrays from the user and read/print them in matrix form.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[3][3];
int i,j;
//accepting values into an array a
printf("Accept elements in array a\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
     {
     printf("Enter element :- ");
       scanf("%d",&a[i][j]);
       }
}
//Print array elements in matrix form
printf("\nThe matrix is\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
     {
       printf("%6d",a[i][j]);
       }
       printf("\n");
}
getch();
}

Output (1)

Accept elements in array a
Enter element :- 23
Enter element :- 4
Enter element :- 12
Enter element :- 67
Enter element :- 4
Enter element :- 34
Enter element :- 78
Enter element :- 33
Enter element :- 2

The matrix is
    23     4    12
    67     4    34
    78    33     2

Initialization of two-dimensional array

Like one dimensional array, two-dimensional array can be initialized during declaration. For example,

int ra[2][3]={2,10,23,79,100,12};

Indicate that

  • ra is a name of two dimensional array.
  • int represent data type is integer.
  • The size of array is 6 (2×3=6).
  • The elements of two dimensional array are 2,10,23,79,100 and 12.

Example:

Program (1): To print 2×2 matrix and also find the sum of all elements of 2×2 matrix. The elements of 2×2 matrix are given as 12, 34, 2 and 10.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j;
int a[2][2]={12,34,2,10};
int sum=0;
//to print original matrix
printf("\nThe original matrix is\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
       printf("%6d",a[i][j]);
       }
       printf("\n");
}
//Calculate sum of all elements and print result
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
     sum=sum+a[i][j];
       }
}
printf("The sum is %d",sum);
getch();
}

Output (1)

The original matrix is
    12    34
     2    10
The sum is 58

Two dimensional array memory/storage representation

Consider the array initialized as:

int ra[2][3]={2,10,23,79,100,12};

When two dimensional array int ra[2][3] declared, the compiler allocates 1st element having data 2 associated with index number [0][0]. For the last element having data 12 associated with index number [1][2] as shown above.

Example:

Program (1): To find the addition of two matrices.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[2][2],b[2][2],c[2][2];
int i,j;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
     printf("Enter %d element :- ",i+1);
       scanf("%d",&a[i][j]);
       }
}
//accepting values into an array b
printf("Accept elements in Matrix B\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
     printf("Enter %d element :- ",i+1);
       scanf("%d",&b[i][j]);
       }
}
//to find addition of two matrices and print result
printf("\nThe resultant matrix is\n");
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
       c[i][j]=a[i][j]+b[i][j];
       printf("%6d",c[i][j]);
       }
       printf("\n");
}
getch();
}

Output (1)

Accept elements in Matrix A
Enter 1 element :- 2
Enter 1 element :- 34
Enter 2 element :- 22
Enter 2 element :- 67
Accept elements in Matrix B
Enter 1 element :- 54
Enter 1 element :- 77
Enter 2 element :- 345
Enter 2 element :- 3

The resultant matrix is
    56   111
   367    70

Program (1): To find largest element in matrix.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[2][2],lar;
int i,j;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<2;i++)
{     
for(j=0;j<2;j++)   
  {       
scanf("%d",&a[i][j]);      
 } 
}
//to print matrix
printf("\nThe matrix is\n");
for(i=0;i<2;i++)
{     
for(j=0;j<2;j++)    
 {      
 printf("%6d",a[i][j]);       
}      
 printf("\n");
 }
//to find largest element
lar=a[0][0];
for(i=0;i<2;i++)
{
     for(j=0;j<2;j++)
     {
if(a[i][j]>lar)
{
lar=a[i][j];
}
}
}
printf("largest element %d\n",lar);
getch();
}

Output (1)

Accept elements in Matrix A
3
54
23
556

The matrix is
     3    54
    23   556
largest element 556

Program (1): To find transpose of a matrix.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a[2][2],b[2][2];
int i,j;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
//to print original matrix
printf("\nThe original matrix is\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%6d",a[i][j]);
}
printf("\n");
}
//to find transpose of matrix
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
}
}
//to print transpose matrix
printf("\nThe transpose matrix is\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%6d",b[i][j]);
}
printf("\n");
}
getch();
}

Output (1)

Accept elements in Matrix A
2
3
5
67

The original matrix is
     2     3
     5    67

The transpose matrix is
     2     5
     3    67

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *