C++ Multidimensional Arrays


In C++, you can create an array of an array known as multi-dimensional array. For example:
int x[3][4];
Here, x is a two dimensional array. It can hold a maximum of 12 elements.
You can think this array as table with 3 rows and each row has 4 columns as shown below.
Elements in two dimensional array in C++ Programming
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 multidimensional 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, 12, 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.
int test[2][3][4] = { 
                     { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
                     { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
                 };

Example 1: Two Dimensional Array

C++ Program to display all elements of an initialised two dimensional array.
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int test[3][2] =
  6. {
  7. {2, -5},
  8. {4, 0},
  9. {9, 1}
  10. };
  11. // Accessing two dimensional array using
  12. // nested for loops
  13. for(int i = 0; i < 3; ++i)
  14. {
  15. for(int j = 0; j < 2; ++j)
  16. {
  17. cout<< "test[" << i << "][" << j << "] = " << test[i][j] << endl;
  18. }
  19. }
  20. return 0;
  21. }
  22. Output
    test[0][0] = 2
    test[0][1] = -5
    test[1][0] = 4
    test[1][1] = 0
    test[2][0] = 9
    test[2][1] = 1
    

    Example 2: Two Dimensional Array

    C++ Program to store temperature of two different cities for a week and display it.
    1. #include <iostream>
    2. using namespace std;
    3. const int CITY = 2;
    4. const int WEEK = 7;
    5. int main()
    6. {
    7. int temperature[CITY][WEEK];
    8. cout << "Enter all temperature for a week of first city and then second city. \n";
    9. // Inserting the values into the temperature array
    10. for (int i = 0; i < CITY; ++i)
    11. {
    12. for(int j = 0; j < WEEK; ++j)
    13. {
    14. cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
    15. cin >> temperature[i][j];
    16. }
    17. }
    18. cout << "\n\nDisplaying Values:\n";
    19. // Accessing the values from the temperature array
    20. for (int i = 0; i < CITY; ++i)
    21. {
    22. for(int j = 0; j < WEEK; ++j)
    23. {
    24. cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl;
    25. }
    26. }
    27. return 0;
    28. }
    Output
    Enter all temperature for a week of first city and then second city. 
    City 1, Day 1 : 32
    City 1, Day 2 : 33
    City 1, Day 3 : 32
    City 1, Day 4 : 34
    City 1, Day 5 : 35
    City 1, Day 6 : 36
    City 1, Day 7 : 38
    City 2, Day 1 : 23
    City 2, Day 2 : 24
    City 2, Day 3 : 26
    City 2, Day 4 : 22
    City 2, Day 5 : 29
    City 2, Day 6 : 27
    City 2, Day 7 : 23
    
    
    Displaying Values:
    City 1, Day 1 = 32
    City 1, Day 2 = 33
    City 1, Day 3 = 32
    City 1, Day 4 = 34
    City 1, Day 5 = 35
    City 1, Day 6 = 36
    City 1, Day 7 = 38
    City 2, Day 1 = 23
    City 2, Day 2 = 24
    City 2, Day 3 = 26
    City 2, Day 4 = 22
    City 2, Day 5 = 29
    City 2, Day 6 = 27
    City 2, Day 7 = 23
    

    Example 3: Three Dimensional Array

    C++ Program to Store value entered by user in three dimensional array and display it.
    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. // This array can store upto 12 elements (2x3x2)
    6. int test[2][3][2];
    7. cout << "Enter 12 values: \n";
    8. // Inserting the values into the test array
    9. // using 3 nested for loops.
    10. for(int i = 0; i < 2; ++i)
    11. {
    12. for (int j = 0; j < 3; ++j)
    13. {
    14. for(int k = 0; k < 2; ++k )
    15. {
    16. cin >> test[i][j][k];
    17. }
    18. }
    19. }
    20. cout<<"\nDisplaying Value stored:"<<endl;
    21. // Displaying the values with proper index.
    22. for(int i = 0; i < 2; ++i)
    23. {
    24. for (int j = 0; j < 3; ++j)
    25. {
    26. for(int k = 0; k < 2; ++k)
    27. {
    28. cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
    29. }
    30. }
    31. }
    32. return 0;
    33. }
    Output
    Enter 12 values: 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Displaying Value stored:
    test[0][0][0] = 1
    test[0][0][1] = 2
    test[0][1][0] = 3
    test[0][1][1] = 4
    test[0][2][0] = 5
    test[0][2][1] = 6
    test[1][0][0] = 7
    test[1][0][1] = 8
    test[1][1][0] = 9
    test[1][1][1] = 10
    test[1][2][0] = 11
    test[1][2][1] = 12
    
    As the number of dimension increases, the complexity also increases tremendously although the concept is quite similar.

Comments

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website