Read Input and Fill a 2d Array
An assortment of arrays is known as 2d array. The ii dimensional (2D) assortment in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional array lets have a wait at the following C plan.
Uncomplicated Two dimensional(2D) Array Example
For now don't worry how to initialize a 2 dimensional array, we volition talk over that role later. This program demonstrates how to store the elements entered by user in a 2d array and how to display the elements of a two dimensional array.
#include<stdio.h> int main(){ /* 2D assortment declaration*/ int disp[2][iii]; /*Counter variables for the loop*/ int i, j; for(i=0; i<2; i++) { for(j=0;j<three;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying assortment elements printf("Two Dimensional array elements:\northward"); for(i=0; i<two; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==ii){ printf("\n"); } } } return 0; } Output:
Enter value for disp[0][0]:i Enter value for disp[0][1]:two Enter value for disp[0][ii]:3 Enter value for disp[one][0]:4 Enter value for disp[1][i]:5 Enter value for disp[1][2]:6 Two Dimensional array elements: 1 ii 3 4 5 6
Initialization of 2d Array
There are two means to initialize a two Dimensional arrays during declaration.
int disp[ii][four] = { {10, 11, 12, 13}, {14, 15, sixteen, 17} }; OR
int disp[2][4] = { ten, 11, 12, 13, xiv, 15, 16, 17}; Although both the higher up declarations are valid, I recommend you to use the first method as it is more readable, considering you tin visualize the rows and columns of 2d array in this method.
Things that y'all must consider while initializing a 2D array
We already know, when we initialize a normal assortment (or you tin can say one dimensional array) during declaration, we need not to specify the size of information technology. However that'due south non the case with 2nd array, y'all must e'er specify the second dimension fifty-fifty if you are specifying elements during the annunciation. Let'south understand this with the assist of few examples –
/* Valid declaration*/ int abc[2][2] = {1, 2, 3 ,4 } /* Valid annunciation*/ int abc[][2] = {one, 2, 3 ,4 } /* Invalid declaration – y'all must specify 2d dimension*/ int abc[][] = {ane, 2, 3 ,4 } /* Invalid because of the same reason mentioned higher up*/ int abc[2][] = {1, 2, three ,4 } How to shop user input data into 2D assortment
We tin calculate how many elements a ii dimensional array can have past using this formula:
The assortment arr[n1][n2] can have n1*n2 elements. The array that we have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts. And so this array has first subscript value every bit five and second subscript value as 4.
So the assortment abc[5][4] can have five*four = twenty elements.
To store the elements entered by user we are using two for loops, i of them is a nested loop. The outer loop runs from 0 to the (first subscript -1) and the inner for loops runs from 0 to the (2d subscript -1). This way the the social club in which user enters the elements would be abc[0][0], abc[0][1], abc[0][2]…so on.
#include<stdio.h> int main(){ /* 2D assortment declaration*/ int abc[v][4]; /*Counter variables for the loop*/ int i, j; for(i=0; i<5; i++) { for(j=0;j<4;j++) { printf("Enter value for abc[%d][%d]:", i, j); scanf("%d", &abc[i][j]); } } return 0; } In to a higher place example, I accept a 2d assortment abc of integer type. Conceptually you can visualize the higher up array similar this:
Notwithstanding the actual representation of this array in retentiveness would be something like this:
Pointers & 2nd assortment
As we know that the 1 dimensional array proper name works equally a pointer to the base of operations element (first chemical element) of the array. Nevertheless in the case second arrays the logic is slightly dissimilar. You can consider a 2D array as collection of several ane dimensional arrays.
So abc[0] would have the address of first chemical element of the first row (if we consider the above diagram number one).
similarly abc[1] would have the address of the first element of the 2d row. To empathise it better, lets write a C program –
#include <stdio.h> int master() { int abc[5][4] ={ {0,1,2,3}, {4,5,6,7}, {8,nine,10,11}, {12,13,fourteen,15}, {16,17,18,19} }; for (int i=0; i<=4; i++) { /* The correct way of displaying an address would be * printf("%p ",abc[i]); but for the demonstration * purpose I am displaying the address in int so that * yous can chronicle the output with the diagram to a higher place that * shows how many bytes an int element uses and how they * are stored in contiguous retentivity locations. * */ printf("%d ",abc[i]); } return 0; } Output:
1600101376 1600101392 1600101408 1600101424 1600101440
The actual accost representation should be in hex for which we use %p instead of %d, equally mentioned in the comments. This is just to show that the elements are stored in contiguous memory locations. Yous tin relate the output with the diagram above to see that the deviation between these addresses is actually number of bytes consumed by the elements of that row.
The addresses shown in the output belongs to the first element of each row abc[0][0], abc[1][0], abc[2][0], abc[iii][0] and abc[4][0].
Source: https://beginnersbook.com/2014/01/2d-arrays-in-c-example/
Post a Comment for "Read Input and Fill a 2d Array"