Arrays in C (2D Array) – Set 5

Till now I educated you regarding the 1D arrays yet today I will talk about the 2D exhibits or two-dimensional arrays in c. arrays is a gathering of components with comparable information type.

We can make clusters with any measurement. Anyway, software engineers once in a while go past 3D clusters.

I will likewise give you a review on 3D exhibits in the resulting instructional exercises. Be that as it may, today lets talk about the 2D exhibits quickly.

2D Arrays in C

As its name recommends, 2D clusters are the exhibits having 2 measurements. Those two measurements are for the most part called lines and segments. 2D clusters are additionally called network.

The revelation of 2D Array

A two-dimensional exhibit can be proclaimed in the following manner.

int a[3][3];

This is a 2D exhibit with 3 lines and 3 sections. The all-out components in the cluster are 9 (3×3).

Instatement of 2D Array

Like 1D clusters, a 2D exhibit can likewise be introduce at the hour of its assertion. Given beneath are a portion of the introduction strategies that are utilized every now and again.

int num[3][2] = {

{43,56},

{56,54},

{65,98}

};

This is one of the least complex method for introducing a 2D cluster.

int num[3][2] = {43, 56, 56, 54, 65, 98};

This technique will likewise work yet it will diminish the coherence of the cluster as well.

int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;

int num[][2] = {

{43,56},

{56,54},

{65,98}

{87,86}

};

It is discretionary to give the line measurement of a 2D cluster on the off chance that we introduce it. Keep in mind giving segment measurement is constantly obligatory.

Let’s take one basic program to comprehend a 2D array in C.

#include<stdio.h>
 
int main()
{
 int student[6][2];
 int x;
 
 for(x=0;x<6;x++)
 {
  printf(" Enter roll no. and marksn ");
  scanf("%d %d",&student[x][0],&student[x][1]);
 }
 
 for(x=0;x<6;x++)
  printf("n %d %d",student[x][0],student[x][1]);
 
 return 0;
 
}

Output

Enter roll no. and marks
1 25 
Enter roll no. and marks 
2 24 
Enter roll no. and marks 
3 27 
Enter roll no. and marks 
4 30 
Enter roll no. and marks 
5 27 
Enter roll no. and marks 
6 22 

1 25 
2 24 
3 27 
4 30 
5 27 
6 22 

Clarification

In the main explanation, I have announced the 2D cluster with the name understudy.

Keep in mind the 2D cluster additionally stores components with list 00. Components will be put away along these lines.

00 01

10 11

20 21

Etc.

Presently by utilizing two for circles I have put away the qualities inside 2D cluster and show it on the screen.

Consider cautiously the printf() and scanf() work with contentions “&stud[x][0], &student[x][1]”. We are putting away and getting to values inside 2D exhibit by push savvy. We can likewise store esteems and access them section savvy as well.

Memory Allocation of 2D Array

2D clusters likewise store its qualities like 1D exhibits. They additionally store components in bordering memory areas.

Components of the first line will be put away first, after that components of the second line will be put away.

This methodology will proceed until the cluster components close. Given beneath is the memory distribution of a 2D whole number cluster s[4][2].

Leave a Comment

error: Alert: Content is protected!!