In this article two dimensional array we give the information about There may be situations where a table of values has to be stored. C allows us to define tables of objects using two-dimensional arrays.
Two-dimensional array
So far we have discussed array variables that can store a list of values. There may be situations where a table of values has to be stored. C allows us to define tables of objects using two-dimensional arrays. In C language, two dimensional arrays are represented as rows and columns, also called matrix. This is also called array of array or array list.
The two-dimensional arrays are declared as follows:
Syntax:
Data_type array_name [row_size] [column_size];
Below is a simple example of declaring a two-dimensional array.
Initialization of 2D Array in C
A way to initialize the two dimensional array at the time of declaration is given below.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; clracr();
//traversing 2D array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(“arr[%d] [%d] = %d \n”,i,j,arr[i][j]);
}//end of j
}//end of i
getch();
}
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
Two Dimensional array Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int mat1[2][2],mat2[2][2],mat3[2][2];
clrscr();
printf(“\n\n\n Enter the Elements of First Matrix:”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&mat1[i][j]);
}
}
printf(“\n\n First Matrix:\n “);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“\t%d”,mat1[i][j]);
}
printf(“\n”);
}
printf(“\n\n\n Enter the Elements of Second Matrix:”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&mat2[i][j]);
}
}
printf(“\n\n Second Matrix:\n “);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“\t%d”,mat2[i][j]);
}
printf(“\n”);
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf(“\n\n Addition of above two Matrices:\n “);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“\t%d”,mat3[i][j]);
}
printf(“\n”);
}
getch();
}
Multidimensional array:
C Allows arrays of three or more dimensions. The compiler has set an exact limit. Multidimensional arrays have a common appearance Data_type array_name [s1] [s2] [s3] ……. [Sn]; Where si is the size of the ith dimension.
Here are some examples:
Int Survey [3] [5] [12];
Float table [5] [4] [5] [3];
The survey is a three-dimensional array consisting of 180 integer type elements. Similarly, a table is a four-dimensional array containing 300 elements of the floating-point type.
The array survey may represent rainfall survey data for three years from January to December in five cities. First represents the first index year, the second city, and the third month.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining