Tech Study

Multidimensional Array in C

A multidimensional array in c can also be considered as an array of arrays that are supposed to store homogeneous data in tabular form. Data present in multidimensional arrays are generally supposed to be stored in row-major order.

Declaring N-dimensional arrays : 

ri_data_type ri_array_name[risize1][risize2]….[risizeN];

ri_data_type: Type of data that we want to be stored in the array.

ri_array_name: Name of the desired array

risize1, risize2,… ,sriizeN: These are the sizes of the dimension

Examples of Multidimensional Arrays :

Two dimensional array example:

int ri_two_d[10][20];

Three dimensional array example:

int ri_three_d[10][20][30];

 

Size of Multidimensional Arrays:

We can calculate the size of the multidimensional array (no. of elements we can store in a multidimensional array) by multiplying the dimensions of the array in c.

Example of Multidimensional Arrays:

The array int rix[10][20] is supposed to be storing a total (10*20) = 200 elements.

Similarly array int rix[5][10][20] is supposed to be storing a total (5*10*20) = 1000 elements.

Two-Dimensional Array

Two – dimensional array can be considered to be the simplest form of a multidimensional array. We are allowed to see a two – dimensional array as an array of the one-dimensional array for the sake of easier understanding.

The basic syntax for declaring a two-dimensional array of size rx, ry:

Syntax of Two – Multidimensional Arrays :

ri_data_type ri_array_name[rx][ry];

Here, ri_data_type is the type of data that is supposed to be stored.

 We are allowed to declare a two-dimensional integer array foe example ‘rx’ of size 10,20 as below:

int rx[10][20];

Elements present in two-dimensional arrays are generally referred to by rx[i][j] in such cases i depicts the row number while ‘j’ depicts the column number.

Initializing Two – Dimensional Arrays: There can be various ways present in which a Two-Dimensional array is supposed to be initialized.

 First Method:

int rx[3][4] = {11, 1 ,21 ,3 ,41 , 5 , 61 , 7 , 81 , 9 , 101 , 11}

The array present above is having 3 rows and 4 columns. The elements that are present in the braces from left to right are supposed to be stored in the table which is also from left to right. The elements are going to be filled in the array in order, the first 4 elements are going to take the places from the left in the first row, the next 4 elements are taking the places accordingly in the second row, and so on.

Second Method:

int rx[3][4] = {{11,1,21,3}, {41,5,61,7}, {81,9,101,11}};

Third Method: 

int rx[3][4];

for(int ri = 0; ri < 3; ri++){

for(int rj = 0; rj < 4; rj++){

     cin >> rx[ri][rj];

}

}

Fourth Method:

int** rx = new int*[3];

for(int ri = 0; ri < 3; ri++){

rx[ri] = new int[4];

for(int rj = 0; rj < 4; rj++){

     cin >> rx[ri][rj];

}

}

Accessing Elements of Two-Dimensional Arrays: Elements present in Two-Dimensional arrays are generally accessed with the help of row indexes and column indexes.

Example for Two – Multidimensional Arrays in c: 

int rx[2][1];

Three-Dimensional Array

Initializing Three-Dimensional Array: Initialization for a Three-Dimensional array can be considered to be same as that of the Two-dimensional arrays. The only difference present is as the count of dimensions is found to be increasing so the count of nested braces is also supposed to be increasing.

Method 1:

int rx[2][3][4] = {11, 1, 21, 3, 41, 5, 61, 7, 81, 9, 101,

              11, 121, 13, 141, 15, 161, 17, 181, 19,

              201, 21, 221, 23};

Method 2(Better): 

int rx[2][3][4] =

 {

   { {11,1,21,3}, {41,5,61,7}, {81,9,101,11} },

   { {121,13,141,15}, {161,17,181,19}, {201,21,221,23} }

 };

 

Accessing elements in Three-Dimensional Arrays: Accessing the present elements in Three-Dimensional Arrays can also be considered to be similar to that of Two-Dimensional Arrays. The present difference is we are supposed to use three loops in place of two loops for one more additional dimension present in Three-dimensional Arrays.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more