Multidimensional arrays in C++ programming



Two-dimensional arrays in C++

A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.

An array is useful for storing and working with a set of data. Sometimes, though, it’s necessary to work with multiple sets of data.

To define a two-dimensional array, two size declarators are required:

  • the first one is for the number of rows
  • the second one is for the number of columns.

Program below illustrates how to pass a two-dimensional array to a function. When a two-dimensional array is passed to a function, the parameter type must contain a size declarator for the number of columns.

Example - pass a two-dimensional array to a function:


    #include <iostream>
    #include <iomanip>
     using namespace std;
     const int NUM_COLS = 4; // Number of columns in each array
     const int TBL1_ROWS = 3; // Number of rows in table1
     const int TBL2_ROWS = 4; // Number of rows in table2
     void showArray(int [][NUM_COLS], int); // Function prototype
     int main() {
     int table1[TBL1_ROWS][NUM_COLS] = {{1, 2, 3, 4},
                                       {5, 6, 7, 8},
                                       {9, 10, 11, 12}};
     int table2[TBL2_ROWS][NUM_COLS] = {{ 10, 20, 30, 40},
                                        { 50, 60, 70, 80},
                                        { 90, 100, 110, 120},
                                        {130, 140, 150, 160}};
     cout << "The contents of table1 are:\n";
     showArray(table1, TBL1_Rows);
     cout << "\nThe contents of table2 are:\n";
     showArray(table2, TBL2_Rows);
     return 0;
     }
     void showArray(int array[][NUM_COLS], int numRows) {
     for (int row = 0; row < numRows; row++) { 
     for (int col = 0; col < NUM_COLS; col++) {
     cout << setw(5) << array[row][col] << " ";
     }
    cout << endl;    }   }

    Program Output :
    The contents of table1 are:
    1 2 3 4
    5 6 7 8
    9 10 11 12

    The contents of table2 are:
    10 20 30 40
    50 60 70 80
    90 100 110 120
    130 140 150 160

Multidimensional array in C++

C++ permits arrays to have multiple dimensions. C++ allows you to create arrays with virtually any number of dimensions. Here is an example of a three-dimensional (3D) array definition:


    double seat[3][5][8];

This array can be thought of as three sets of five rows, with each row containing eight elements.

The array might be used, for example, to store the price of seats in an auditorium that has three sections of seats, with five rows of eight seats in each section.

The following program stores and displays theater seat prices. It demonstrates how to pass a 3-dimensional array to a function. The data is read in from a file.


    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    const int NUM_SECTIONS = 3,
    ROWS_IN_SECTION = 5,
    SEATS_IN_ROW = 8;
    typedef double seatTable[][ROWS_IN_SECTION][SEATS_IN_ROW];
    void fillArray(seatTable);
    void showArray(seatTable);
    int main() {
    double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW];
    fillArray(seats);
    showArray(seats);
    return 0;
    }
    void fillArray(seatTable array) {
    ifstream dataIn;
    dataIn.open("seats.dat");
    if (!dataIn)
    cout << "Error opening file.\n";
    else { 
    for (int section = 0; section < NUM_SECTIONS; section++)
    for (int row = 0; row < ROWS_IN_SECTION; row++)
    for (int seat = 0; seat < SEATS_IN_ROW; seat++)
    dataIn >> array[section][row][seat];
    dataIn.close(); }   }

    void showArray(seatTable array) {
    cout << fixed << showpoint << setprecision(2);
    for (int section = 0; section < NUM_SECTIONS; section++) {
    cout << "\n\nSection" << (section+1);
    for (int row = 0; row < ROWS_IN_SECTION; row++) {
    cout << "\nRow " << (row+1) << ": ";
    for (int seat = 0; seat < SEATS_IN_ROW; seat++)
    cout << setw(7) << array[section][row][seat]; }   }
    cout << endl;
    }

    Output :
    
    Section1
    Row 1: 18.00 18.00 18.00 18.00 18.00 18.00 18.00 18.00
    Row 2: 15.00 15.00 15.00 15.00 15.00 15.00 15.00 15.00
    Row 3: 15.00 15.00 15.00 15.00 15.00 15.00 15.00 15.00
    Row 4: 15.00 15.00 15.00 15.00 15.00 15.00 15.00 15.00
    Row 5: 12.00 12.00 12.00 12.00 12.00 12.00 12.00 12.00
    
    Section2
    Row 1: 12.00 12.00 12.00 12.00 12.00 12.00 12.00 12.00
    Row 2: 12.00 12.00 12.00 12.00 12.00 12.00 12.00 12.00
    Row 3: 12.00 12.00 12.00 12.00 12.00 12.00 12.00 12.00
    Row 4: 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00
    Row 5: 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00
    
    Section3
    Row 1: 8.00 8.00 10.00 10.00 10.00 10.00 8.00 8.00
    Row 2: 8.00 8.00 10.00 10.00 10.00 10.00 8.00 8.00
    Row 3: 5.00 5.00 8.00 8.00 8.00 8.00 5.00 5.00
    Row 4: 5.00 5.00 8.00 8.00 8.00 8.00 5.00 5.00
    Row 5: 5.00 5.00 8.00 8.00 8.00 8.00 5.00 5.00

As with one-dimensional and two-dimensional arrays, the parameter lists can be simplified if a typedef statement is used to create an alias for the array type.


Ads Right