How to Read in a File of Numbers and Make Two Dimensional Array Oop

An array of arrays is known every bit 2D array. The two dimensional (2d) array in C programming is also known equally matrix. A matrix tin can be represented as a table of rows and columns. Before we discuss more well-nigh two Dimensional array lets have a look at the following C program.

Simple 2 dimensional(2D) Array Instance

For now don't worry how to initialize a two dimensional assortment, we volition hash out that part later. This program demonstrates how to shop the elements entered by user in a second array and how to display the elements of a two dimensional array.

#include<stdio.h> int main(){    /* 2d array declaration*/    int disp[2][3];    /*Counter variables for the loop*/    int i, j;    for(i=0; i<2; i++) {       for(j=0;j<3;j++) {          printf("Enter value for disp[%d][%d]:", i, j);          scanf("%d", &disp[i][j]);       }    }    //Displaying array elements    printf("Two Dimensional assortment elements:\north");    for(i=0; i<2; i++) {       for(j=0;j<3;j++) {          printf("%d ", disp[i][j]);          if(j==two){             printf("\n");          }       }    }    return 0; }

Output:

Enter value for disp[0][0]:1 Enter value for disp[0][1]:2 Enter value for disp[0][ii]:3 Enter value for disp[ane][0]:iv Enter value for disp[1][one]:5 Enter value for disp[i][two]:half-dozen Two Dimensional array elements: 1 two 3  four 5 6          

Initialization of 2D Assortment

There are two means to initialize a 2 Dimensional arrays during declaration.

int disp[two][4] = {     {10, xi, 12, xiii},     {14, 15, 16, 17} };

OR

int disp[two][4] = { 10, 11, 12, 13, 14, 15, xvi, 17};

Although both the above declarations are valid, I recommend you to use the outset method every bit it is more than readable, because yous can visualize the rows and columns of 2d array in this method.

Things that you must consider while initializing a 2d array

We already know, when we initialize a normal array (or you tin say one dimensional array) during annunciation, we need not to specify the size of it. Still that's non the case with 2D array, you must e'er specify the second dimension even if yous are specifying elements during the declaration. Let's sympathize this with the help of few examples –

/* Valid declaration*/ int abc[2][2] = {ane, 2, 3 ,4 }   /* Valid proclamation*/  int abc[][2] = {1, two, 3 ,4 }   /* Invalid declaration – you must specify second dimension*/ int abc[][] = {1, 2, iii ,4 }    /* Invalid because of the same reason  mentioned above*/ int abc[2][] = {1, 2, iii ,4 }

How to store user input data into 2nd array

We can calculate how many elements a two dimensional assortment tin can have by using this formula:
The array arr[n1][n2] can have n1*n2 elements. The array that nosotros have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts. So this array has first subscript value as v and 2d subscript value equally four.
So the assortment abc[5][4] tin take 5*4 = 20 elements.

To store the elements entered by user nosotros are using 2 for loops, one of them is a nested loop. The outer loop runs from 0 to the (first subscript -ane) and the inner for loops runs from 0 to the (second subscript -1). This mode the the gild in which user enters the elements would be abc[0][0], abc[0][ane], abc[0][2]…then on.

#include<stdio.h> int principal(){    /* 2D array proclamation*/    int abc[5][4];    /*Counter variables for the loop*/    int i, j;    for(i=0; i<v; i++) {       for(j=0;j<4;j++) {          printf("Enter value for abc[%d][%d]:", i, j);          scanf("%d", &abc[i][j]);       }    }    return 0; }

In above example, I have a 2nd assortment abc of integer type. Conceptually you tin can visualize the above array like this:
2D-array

However the actual representation of this array in memory would exist something like this:
memory-2D-diagram

Pointers & second array

As we know that the ane dimensional assortment name works as a pointer to the base element (start element) of the array. Nevertheless in the case 2D arrays the logic is slightly different. You can consider a 2nd array as collection of several ane dimensional arrays.

So abc[0] would accept the address of first element of the first row (if we consider the above diagram number 1).
similarly abc[1] would accept the address of the commencement chemical element of the second row. To sympathise it meliorate, lets write a C plan –

#include <stdio.h> int primary() {    int abc[5][four] ={             {0,1,2,3},             {four,v,6,vii},             {8,9,10,11},             {12,13,14,15},             {16,17,18,19}             };     for (int i=0; i<=4; i++)     {         /* The correct way of displaying an address would exist          * printf("%p ",abc[i]); merely for the demonstration          * purpose I am displaying the address in int and so that          * you tin can relate the output with the diagram above that          * shows how many bytes an int element uses and how they          * are stored in contiguous retention locations.          *          */     	printf("%d ",abc[i]);     }     return 0; }          

Output:

1600101376 1600101392 1600101408 1600101424 1600101440

The bodily address representation should be in hex for which nosotros utilise %p instead of %d, as mentioned in the comments. This is but to show that the elements are stored in contiguous memory locations. You can chronicle the output with the diagram above to see that the difference betwixt these addresses is actually number of bytes consumed by the elements of that row.

The addresses shown in the output belongs to the outset chemical element of each row abc[0][0], abc[i][0], abc[2][0], abc[3][0] and abc[4][0].

piercepasuch.blogspot.com

Source: https://beginnersbook.com/2014/01/2d-arrays-in-c-example/

0 Response to "How to Read in a File of Numbers and Make Two Dimensional Array Oop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel