C++ Multidimensional Arrays
C++ Multidimensional Arrays
In C++, you can create an array of an array known as multidimensional array.
For Example :
int x [3] [4];
Here, x is two dimensionall array . it can hold a maximum of 12 elements.
You can think this array as table with 3 rows and each rows has 4 columns .
for example. :
Three-dimensional array also works in a similar way. For example :
float x [2][4][3];
This array x can hold a maximum of 24 elements. You can think this example as : Each of the 2 elements can hold 4 elements, which makes 8 elements and each of those 8 elements can hold 3 elements. Hence, total number of elements this array can hold is 24.
Multidimensional Array Initialisation :
you can initialise a multidimmensional array in more than one way.
Initialisation of two dimensional array:
int test[2][3] = { 2, 4, -5, 9, 0, 9};
Better way to initialise this array with same array elements as above.
int test[2][3] = { { 2, 4, 5,},{9, 0, 0}};
Initialisation of three dimensional array
int test [2][3][4] = {3, 4, 2, 3, 0,, -3, 9, 11, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
Better way to initialise this array with same elements as above.
Example - Two Dimensional Array
C++ program to store temperature of two different cities for a week and display it.
Output :
Example - Three Dimensional Array
C++ Program to store value entered by user in three dimensional
array and display it.
Output :
Enter 12 value:
0 comments:
Post a Comment