A Two-Dimensional Array

Introduction

The arrays we used so far were made of a uniform series, where all members consisted of a simple list, like a column of names on a piece of paper. Also, all items fit in one list. This type of array is referred to as one-dimensional.

If you create a list of names, you may want part of the list to include family members and another part of the list to include friends. Instead of creating a second list, you can add a second dimension to the list. In other words, you would create a list of a list, or one list inside another list, although the list is still made of items with common characteristics.

A multidimensional array is a series of arrays so that each array contains its own sub-array(s).

Practical LearningPractical Learning: Introducing Multidimensional Arrays

  1. Start Microsoft Visual Studio
  2. Create a C++ Console App named PayrollPreparation8
  3. Change the document as follows:
    using static System.Console;
    
    long[]    employeesNumbers = new long[] { 293_708, 594_179, 820_392, 492_804, 847_594 };
    string[] employeesFirstNames = new string[] { "Christine", "Peter", "Jonathan", "Lilly", "Gabriel" };
    string[] employeesLastNames = new string[] { "Greenberg", "Keys", "Verland", "Malone", "Reasons" };
    double[] hourlySalaries = new double[] { 22.86, 29.47, 30.72, 18.39, 17.38 };
    
    int emplNbr = 0;
    
    cout << "Payroll Preparation");
    cout << "===================");
    
    try
    {
        Write("Employee #: ");
        emplNbr = int.Parse(ReadLine()!);
    }
    catch(FormatException)
    {
        cout << "You must enter an employee's number.");
    }
    
    Clear();
    
    for (int nbr = 0; nbr <= employeesNumbers.Length - 1; nbr++)
    {
        if (emplNbr == employeesNumbers[nbr])
        {
            cout << "Payroll Preparation");
            cout << "===================================");
            cout << "\nEmployee #:    {employeesNumbers[nbr]}");
            cout << "EmployeeName:  " + employeesLastNames[nbr] + ", " + employeesFirstNames[nbr]);
            cout << "\nHourly Salary: {hourlySalaries[nbr]}");
        }
    }
    
    cout << "===================================");
  4. To execute the application, on the main menu, click Debug -> Start Without Debugging
  5. When requested, type the Employee # as 293708
    Payroll Preparation
    ===================
    Employee #: 293708
  6. Press Enter:
    Payroll Preparation
    ===================================
    Employee #:    293708
    EmployeeName:  Greenberg, Christine
    Hourly Salary: 22.86
    ===================================
    
    Press any key to close this window . . .
  7. Close the window and return to your programming environment

Creating a Two-Dimensional Array

The most basic multidimensional array is made of two dimensions. This is referred to as two-dimensional. To create a two-dimensional array, declare a variable by speciying a data type a space and a name. After the name, add two combinations of square brackets and a semicolon. The primary formula to follow is:

data-type variable-name[][...];

Here is an example that starts a 2-D array:

string names[][];

Initializing a Two-Dimensional Array

There are various ways you can initialize a two-dimensional array. If you are declaring the array variable and are ready to initialize it, use the following formula:

data-type variable-name[number1][number2];   

In this declaration, the names variable contains a list with two parts. Each of the two lists contains 4 elements. This means that the first part contains 4 elements and the second part contains 4 elements. Therefore, the whole list is made of 2 * 4 = 8 elements. Because the variable is declared as a string, each of the 8 items must be a string.

You can also create a two-dimensional array that takes more than two lists, such as 3, 4, 5 or more. Here are examples:

double prices[5][8];
string storeItems[3][7];
float distances[7][4];

You can initialize an array variable when declaring it. To do this, assign some curly brackets to the array variable. This would be done as follows:

string names[2][4] =
{

};

Inside those curly brackets, include a pair of an opening and a closing curly brackets for each internal list of the array. Separater the internal curly brackets with a comma. This would be done as follows:

string names[2][4] =
{
    {},
    {}
};

Then, inside a pair of curly brackets, provide a list of the values of the internal array, just as you would do for a one-dimensional array. Here is an example:

int main()
{
    string names[2][4] =
    {
        { "Celeste", "Mathurin", "Alex", "Germain" },   // First List
        { "Jeremy", "Mathew", "Anselme", "Frederique" } // Second List
    };
}

When initializing a two-dimensional array, remember the dimensions. The number in the left square brackets specifies the number of main lists and the number on the right square brackets specifies the number of elements in each list. Here is an example:

double prices[5][8] = {
    { 10.50, 2.35, 49.75, 202.35, 8.70, 58.20, 34.85, 48.50 },
    { 23.45, 878.50, 26.35, 475.90, 2783.45, 9.50, 85.85, 792.75 },
    { 47.95, 72.80, 34.95, 752.30, 49.85, 938.70, 45.05, 9.80 },
    { 759.25, 73.45, 284.35, 70.95, 82.05, 34.85, 102.30, 84.50 },
    { 29.75, 953.45, 79.55, 273.45, 975.90, 224.75, 108.25, 34.05 }
};

If you use this technique to initialize an array, you can omit specifying the dimension in the left square brackets. Here is an example:

string members[][4] =
{
    {"Celeste", "Mathurin", "Alex", "Germain"},   // First List
    {"Jeremy", "Mathew", "Anselme", "Frederique"} // Second List
};

Accessing the Members of a Two-Dimensional Array

To use the members of a two-dimensional array, you can access each item individually. For example, to initialize a two-dimensional array, you can access each member of the array and assign it a value. The external list is zero-based. In other words, the first list has an index of 0, the second list has an index of 1, and so on. Internally, each list is zero-based and behaves exactly like a one-dimensional array. To access a member of the list, type the name of the variable followed by its square brackets. In the brackets, type the index of the list, a comma, and the internal index of the member whose access you need. If you create an array without initializing it using the curly brackets, you can use this technique of accessing the members by the square brackets to initialize each member of the array. Here is an example:

string members[2][4];

members[0][0] = "Celeste";    // Member of the first list, first item
members[0][1] = "Mathurin";   // Member of the first list, second item
members[0][2] = "Alex";       // Member of the first list, third item
members[0][3] = "Germain";    // Member of the first list, fourth item
members[1][0] = "Jeremy";     // Member of the second list, first item
members[1][1] = "Mathew";     // Member of the second list, second item
members[1][2] = "Anselme";    // Member of the second list, third item
members[1][3] = "Frederique"; // Member of the second list, fourth item

You can use this same technique to retrieve the value of each member of the array. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    string members[2][4];

    members[0][0] = "Celeste";    // Member of the first list, first item
    members[0][1] = "Mathurin";   // Member of the first list, second item
    members[0][2] = "Alex";       // Member of the first list, third item
    members[0][3] = "Germain";    // Member of the first list, fourth item
    members[1][0] = "Jeremy";     // Member of the second list, first item
    members[1][1] = "Mathew";     // Member of the second list, second item
    members[1][2] = "Anselme";    // Member of the second list, third item
    members[1][3] = "Frederique"; // Member of the second list, fourth item

    cout << "Member[0][0]: " << members[0][0] << endl;
    cout << "Member[0][1]: " << members[0][1] << endl;
    cout << "Member[0][2]: " << members[0][2] << endl;
    cout << "Member[0][3]: " << members[0][3] << endl;
    cout << "--------------------------" << endl;
    cout << "Member[1][0]: " << members[1][0] << endl;
    cout << "Member[1][1]: " << members[1][1] << endl;
    cout << "Member[1][2]: " << members[1][2] << endl;
    cout << "Member[1][3]: " << members[1][3] << endl;
    cout << "==========================\n";
}

As we described earlier, a two-dimensional array is a list of two arrays, or two arrays of arrays. In other words, the second arrays are nested in the first arrays. In the same way, if you want to access them using a loop, you can nest one for loop inside a first for loop. The external loop accesses a member of the main array and the second loop accesses the internal list of the current array. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    string members[2][4];

    members[0][0] = "Celeste";
    members[0][1] = "Mathurin";
    members[0][2] = "Alex";
    members[0][3] = "Germain";
    members[1][0] = "Jeremy";
    members[1][1] = "Mathew";
    members[1][2] = "Anselme";
    members[1][3] = "Frederique";

    for (unsigned short External = 0; External < 2; External++)
        for (unsigned short Internal = 0; Internal < 4; Internal++)
            cout << "Person: " << members[External][Internal] << endl;

    cout << "=====================\n";
}

This would produce:

Person: Celeste
Person: Mathurin
Person: Alex
Person: Germain
Person: Jeremy
Person: Mathew
Person: Anselme
Person: Frederique
=====================

Press any key to continue . . .

Practical LearningPractical Learning: Accessing the Members of a Two-Dimensional Array

  1. Change the code as follows:
    using static System.Console;
    
    long[]   employeesNumbers    = new long[]   { 293_708, 594_179, 820_392, 492_804, 847_594 };
    string[] employeesFirstNames = new string[] { "Christine", "Peter", "Jonathan", "Lilly", "Gabriel" };
    string[] employeesLastNames  = new string[] { "Greenberg", "Keys", "Verland", "Malone", "Reasons" };
    double[] hourlySalaries      = new double[] { 22.86, 29.47, 30.72, 18.39, 17.38 };
    
    /* This is an array that represents time sheet values for 5 employees. 
     * Each employee works 5 days a week. */
    double[][] timesWorked = new double[5, 5]
    {
        {  8.00,  8.00, 8.00, 8.00, 8.00 },
        {  6.00,  7.50, 6.00, 8.50, 6.00 },
        { 10.00,  8.50, 9.00, 8.00, 9.50 },
        {  8.50,  6.00, 7.50, 6.00, 7.00 },
        {  9.00, 10.50, 8.00, 7.50, 9.00 }
    };
    
    int emplNbr = 0;
    
    cout << "Payroll Preparation");
    cout << "===================");
    
    try
    {
        Write("Employee #: ");
        emplNbr = int.Parse(ReadLine()!);
    }
    catch(FormatException)
    {
        cout << "You must enter an employee's number.");
    }
    
    Clear();
    
    for (int nbr = 0; nbr <= employeesNumbers.Length - 1; nbr++)
    {
        if (emplNbr == employeesNumbers[nbr])
        {
            cout << "Payroll Preparation");
            cout << "================================");
            cout << "\nEmployee #:    {employeesNumbers[nbr]}");
            cout << "EmployeeName:  " + employeesLastNames[nbr] + ", " + employeesFirstNames[nbr]);
            cout << "\nHourly Salary: {hourlySalaries[nbr]}");
            cout << "================================");
            cout << "Time Worked");
            cout << "--------------------------------");
            cout << "\nMonday:        {timesWorked[nbr, 0]:F}");
            cout << "\nTuesday:       {timesWorked[nbr, 1]:F}");
            cout << "\nWednesday:     {timesWorked[nbr, 2]:F}");
            cout << "\nThursday:      {timesWorked[nbr, 3]:F}");
            cout << "\nFriday:        {timesWorked[nbr, 4]:F}");
            cout << "================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging
  3. Type the Employee # as 594179
    Payroll Preparation
    ================================
    Employee #:    594179
    EmployeeName:  Keys, Peter
    Hourly Salary: 29.47
    ================================
    Time Worked
    --------------------------------
    Monday:        6.00
    Tuesday:       7.50
    Wednesday:     6.00
    Thursday:      8.50
    Friday:        6.00
    ================================
    
    Press any key to close this window . . .
  4. Close the window and return to Micrsoft Visual Studio
  5. On the main menu, click Project -> Add Class...
  6. Change the file Name to Calculations
  7. Click Add
  8. Change the class as follows:
    namespace PayrollPreparation2
    {
        internal static class Calculations
        {
            public static double Add(double a, double b)
            {
                return a + b;
            }
    
            public static double Add(double a, double b, double c, double d, double e)
            {
                return a + b + c + d + e;
            }
    
            public static double Subtract(double a, double b)
            {
                return a - b;
            }
    
            public static double Multiply(double a, double b)
            {
                return a * b;
            }
        }
    }
  9. In the Solution Explorer, right-click PayrollPreparation2 -> Add -> Class...
  10. Change the Name to PayrollEvaluation
  11. Click Add
  12. Create a class as follows:
    namespace PayrollPreparation2
    {
        internal sealed class Payroll
        {
            private double timeSpecified;
    
            public Payroll(double salary, double time)
            {
                HourSalary = salary;
                timeSpecified = time;
            }
    
            public double HourSalary { get; set; }
    
            public double OvertimeSalary
            {
                get { return Calculations.Multiply(HourSalary, 1.50); }
            }
    
            public double RegularTime
            {
                get
                {
                    if (timeSpecified <= 40.00)
                        return timeSpecified;
                    else
                        return 40.00;
                }
            }
    
            public double Overtime
            {
                get
                {
                    if (timeSpecified <= 40.00)
                        return 0.00;
                    else
                        return Calculations.Subtract(timeSpecified, 40.00);
                }
            }
    
            public double RegularPay
            {
                get { return Calculations.Multiply(HourSalary, RegularTime); }
            }
    
            public double OvertimePay
            {
                get { return Calculations.Multiply(OvertimeSalary, Overtime); }
            }
    
            public double NetPay
            {
                get { return Calculations.Add(RegularPay, OvertimePay); }
            }
        }
    }
  13. Click the PayrollPreparation.cs tab to access it
  14. Change the document as follows:
    using static System.Console;
    
    long[]    employeesNumbers = new long[] { 293_708, 594_179, 820_392, 492_804, 847_594 };
    string[] employeesFirstNames = new string[] { "Christine", "Peter", "Jonathan", "Lilly", "Gabriel" };
    string[] employeesLastNames  = new string[] { "Greenberg", "Keys", "Verland", "Malone", "Reasons" };
    double[] hourlySalaries      = new double[] { 22.86, 29.47, 30.72, 18.39, 17.38 };
    
    /* This is an array that represents time sheet values for 5 employees. 
     * Each employee works 5 days a week. */
    double[][] timesWorked = new double[5, 5]
    {
        {  8.00,  8.00, 8.00, 8.00, 8.00 },
        {  6.00,  7.50, 6.00, 8.50, 6.00 },
        { 10.00,  8.50, 9.00, 8.00, 9.50 },
        {  8.50,  6.00, 7.50, 6.00, 7.00 },
        {  9.00, 10.50, 8.00, 7.50, 9.00 }
    };
    
    int emplNbr = 0;
    
    cout << "Payroll Preparation");
    cout << "===================");
    
    try
    {
        Write("Employee #: ");
        emplNbr = int.Parse(ReadLine()!);
    }
    catch(FormatException)
    {
        cout << "You must enter an employee's number.");
    }
    
    Clear();
    
    for (int nbr = 0; nbr <= employeesNumbers.Length - 1; nbr++)
    {
        if (emplNbr == employeesNumbers[nbr])
        {
            cout << "Payroll Preparation");
            cout << "====================================");
            cout << "\nEmployee #:         {employeesNumbers[nbr]}");
            cout << "EmployeeName:       " + employeesLastNames[nbr] + ", " + employeesFirstNames[nbr]);
            cout << "\nHourly Salary:      {hourlySalaries[nbr]}");
            cout << "====================================");
            cout << "Time Worked");
            cout << "------------------------------------");
            cout << "\nMonday:        {timesWorked[nbr, 0],10:F}");
            cout << "\nTuesday:       {timesWorked[nbr, 1],10:F}");
            cout << "\nWednesday:     {timesWorked[nbr, 2],10:F}");
            cout << "\nThursday:      {timesWorked[nbr, 3],10:F}");
            cout << "\nFriday:        {timesWorked[nbr, 4],10:F}");
            cout << "====================================");
    
            double totalTime = Calculations.Add(timesWorked[nbr, 0],
                                                timesWorked[nbr, 1],
                                                timesWorked[nbr, 2],
                                                timesWorked[nbr, 3],
                                                timesWorked[nbr, 4]);
    
            Payroll preparation = new Payroll(hourlySalaries[nbr], totalTime);
    
            cout << "Pay Summary");
            cout << "------------------------------------");
            cout << "\nRegular Time:  {preparation.RegularTime,10:F}");
            cout << "\nOvertime:      {preparation.Overtime,10:F}");
            cout << "\nRegular Pay:   {preparation.RegularPay,10:F}");
            cout << "\nOvertime Pay:  {preparation.OvertimePay,10:F}");
            cout << "\nNet Pay:       {preparation.NetPay,10:F}");
            cout << "====================================");
        }
    }
  15. To execute the application, on the main menu, click Debug -> Start Without Debugging
  16. In the Employee # text box, type 820392
  17. Press Enter:
    Payroll Preparation
    ====================================
    Employee #:         820392
    EmployeeName:       Verland, Jonathan
    Hourly Salary:      30.72
    ====================================
    Time Worked
    ------------------------------------
    Monday:             10.00
    Tuesday:             8.50
    Wednesday:           9.00
    Thursday:            8.00
    Friday:              9.50
    ====================================
    Pay Summary
    ------------------------------------
    Regular Time:       40.00
    Overtime:            5.00
    Regular Pay:      1228.80
    Overtime Pay:      230.40
    Net Pay:          1459.20
    ====================================
    
    Press any key to close this window . . .
  18. Close the window and return to Micrsoft Visual Studio

For Each Item in an Array

You can also visit each item of a multi-dimensional array using a for operator. The formula to follow is:

for(variable : array-name[ 1-dimension]) statement;

To use this formula, in the parentheses of the for() loop, add some square brackets to the name of the array. In the square brackets, put the index you want to access from the external array. In the body of the for loop, access the array member using the variable of that loop. Here is an example:

string members[][] = new string[2, 4];

members[0][0] = "Celeste";    // Member of the First List
members[0][1] = "Mathurin";   // Member of the First List
members[0][2] = "Alex";       // Member of the First List
members[0][3] = "Germain";    // Member of the First List
members[1][0] = "Jeremy";     // Member of the Second List
members[1][1] = "Mathew";     // Member of the Second List
members[1][2] = "Anselme";    // Member of the Second List
members[1][3] = "Frederique"; // Member of the Second List

foreach (string name in members)
    cout << "Person: {0}", name);

cout << "=================================");

This would produce:

Person: Celeste
Person: Mathurin
Person: Alex
Person: Germain
---------------------
Person: Jeremy
Person: Mathew
Person: Anselme
Person: Frederique
====================

Press any key to continue . . .

A Multidimensional Array

Introduction

Beyond two dimensions, you can create an array variable that represents various lists, each list containing various internal lists, and each internal list containing its own elements. This is referred to as a multidimensional array. One of the rules you must follow is that, as always, all members of the array must be of the same type.

Creating a Multidimensional Array

To create a multidimensional array, add the desired number of square brackets to the name of the array variable. Here an example that starts a three-dimensional array:

double numbers[][][];

Inside the first square brackets, enter the number of main lists you are creating. This is like the number of pages of a book you are writing. Inside the second square brackes, enther the number of lists that each main list will have. This is like specifying the number of lines on each page of the book. Inside the third square brackes, enther the number of items that will be contained in each list. This is like the number of characters that each line will contain. Here is an example:

double numbers[2][3][5];

For this variable, we are creating 2 groups of items. Each of the two groups is made of three lists. Each list contains 5 numbers. As a result, the array will contain 2 * 3 * 5 = 30 members.

Initializing a Multidimensional Array

As always, there are various ways you can initialize an array. To initialize it, assign some curly brackets to itea. Here is an example:

double numbers[2][3][5] = 
{

};

In those curly brackets, create an opening and a closing curly brackets that correspond to the number specified for the first dimension of the array. Separate the combinations of square brackets with commas. This would be done as follows:

double numbers[2][3][5] = 
{
    // We start with 2 external lists
    
    {

    },
    {

    }
};

Inside those square brackets, create an opening and a closing curly brackets that correspond to the number specified for the second dimension of the array. Separate the combinations of square brackets with commas. This would be done as follows:

double numbers[2][3][5] = 
{
    // We start with 2 external lists
    {
        // We continue with 3 internal lists
        {
            
        },
        {
            
        },
        {
            
        }
    },
    {
        {
            
        },
        {
            
        },
        {
            
        }
    }
};

Inside these last square brackets, create a list of items for each internal list. Separate the elements with commas. This would be done as follows:

double numbers[2][3][5] = 
{
    // We start with 2 external lists
    {
        // We continue with 3 internal lists
        {
            12.44, 525.38,  -6.28,  2448.32, 632.04
        },
        {
            -378.05,  48.14, 634.18,   762.48,  83.02
        },
        {
            64.92,  -7.44,  86.74,  -534.60, 386.73
        }
    },
    {
        {
            48.02, 120.44,   38.62,  526.82, 1704.62
        },
        {
            56.85, 105.48,  363.31,  172.62,  128.48
        },
        {
            906.68, 47.12, -166.07, 4444.26,  408.62
        }
    }
};

Accessing the Members of a Multidimensional Array

To access a member of a multidimensional array, type the name of the array followed by the combination of square brackets of the array variable. Inside the first square brackets, type the 0-based first dimension. Inside the second square brackets, enther the 0-based index based on the second dimension of the array. Inside the third square brackets, enter the 0-based index of each value. Here are examples:

#include <iostream>
using namespace std;

int main()
{
    float numbers[2][3][5] =
    {
        {
            {
                12.44, 525.38, -6.28, 2448.32, 632.04,
            },
            {
                -378.05, 48.14, 634.18, 762.48, 83.02,
            },
            {
                64.92, -7.44, 86.74, -534.60, 386.73,
            },
        },
        {
            {
                48.02, 120.44, 38.62, 526.82, 1704.69,
            },
            {
                56.85, 105.48, 363.31, 172.62, 128.48,
            },
            {
                906.68, 47.12, -166.07, 4444.26, 408.62,
            },
        },
    };

    cout << "1.  Numbers[0][0][0]: " << numbers[0][0][0] << endl;
    cout << "2.  Numbers[0][0][1]: " << numbers[0][0][1] << endl;
    cout << "3.  Numbers[0][0][2]: " << numbers[0][0][2] << endl;
    cout << "4.  Numbers[0][0][3]: " << numbers[0][0][3] << endl;
    cout << "5.  Numbers[0][0][4]: " << numbers[0][0][4] << endl;

    cout << "6.  Numbers[0][1][0]: " << numbers[0][1][0] << endl;
    cout << "7.  Numbers[0][1][1]: " << numbers[0][1][1] << endl;
    cout << "8.  Numbers[0][1][2]: " << numbers[0][1][2] << endl;
    cout << "9.  Numbers[0][1][3]: " << numbers[0][1][3] << endl;
    cout << "10. Numbers[0][1][4]: " << numbers[0][1][4] << endl;

    cout << "11. Numbers[0][2][0]: " << numbers[0][2][0] << endl;
    cout << "12. Numbers[0][2][1]: " << numbers[0][2][1] << endl;
    cout << "13. Numbers[0][2][2]: " << numbers[0][2][2] << endl;
    cout << "14. Numbers[0][2][3]: " << numbers[0][2][3] << endl;
    cout << "15. Numbers[0][2][4]: " << numbers[0][2][4] << endl;

    cout << "16. Numbers[1][0][0]: " << numbers[1][0][0] << endl;
    cout << "17. Numbers[1][0][1]: " << numbers[1][0][1] << endl;
    cout << "18. Numbers[1][0][2]: " << numbers[1][0][2] << endl;
    cout << "19. Numbers[1][0][3]: " << numbers[1][0][3] << endl;
    cout << "20. Numbers[1][0][4]: " << numbers[1][0][4] << endl;

    cout << "21. Numbers[1][1][0]: " << numbers[1][1][0] << endl;
    cout << "22. Numbers[1][1][1]: " << numbers[1][1][1] << endl;
    cout << "23. Numbers[1][1][2]: " << numbers[1][1][2] << endl;
    cout << "24. Numbers[1][1][3]: " << numbers[1][1][3] << endl;
    cout << "25. Numbers[1][1][4]: " << numbers[1][1][4] << endl;

    cout << "26. Numbers[1][2][0]: " << numbers[1][2][0] << endl;
    cout << "27. Numbers[1][2][1]: " << numbers[1][2][1] << endl;
    cout << "28. Numbers[1][2][2]: " << numbers[1][2][2] << endl;
    cout << "29. Numbers[1][2][3]: " << numbers[1][2][3] << endl;
    cout << "30. Numbers[1][2][4]: " << numbers[1][2][4] << endl;

    cout << "=============================\n";
}

This would produce:

1.  Numbers[0][0][0]: 12.44
2.  Numbers[0][0][1]: 525.38
3.  Numbers[0][0][2]: -6.28
4.  Numbers[0][0][3]: 2448.32
5.  Numbers[0][0][4]: 632.04
6.  Numbers[0][1][0]: -378.05
7.  Numbers[0][1][1]: 48.14
8.  Numbers[0][1][2]: 634.18
9.  Numbers[0][1][3]: 762.48
10. Numbers[0][1][4]: 83.02
11. Numbers[0][2][0]: 64.92
12. Numbers[0][2][1]: -7.44
13. Numbers[0][2][2]: 86.74
14. Numbers[0][2][3]: -534.6
15. Numbers[0][2][4]: 386.73
16. Numbers[1][0][0]: 48.02
17. Numbers[1][0][1]: 120.44
18. Numbers[1][0][2]: 38.62
19. Numbers[1][0][3]: 526.82
20. Numbers[1][0][4]: 1704.69
21. Numbers[1][1][0]: 56.85
22. Numbers[1][1][1]: 105.48
23. Numbers[1][1][2]: 363.31
24. Numbers[1][1][3]: 172.62
25. Numbers[1][1][4]: 128.48
26. Numbers[1][2][0]: 906.68
27. Numbers[1][2][1]: 47.12
28. Numbers[1][2][2]: -166.07
29. Numbers[1][2][3]: 4444.26
30. Numbers[1][2][4]: 408.62
=============================

Press any key to close this window . . .

You can use that same technique of accessing an element of the array and involve that element in an operation of your choice, such as assigning a value to the element to replace its previous value. Here are examples:

#include <iostream>
using namespace std;

int main()
{
    float numbers[2][3][5];

    numbers[0][0][0] = 12.44;
    numbers[0][0][1] = 525.38;
    numbers[0][0][2] = -6.28;
    numbers[0][0][3] = 2448.32;
    numbers[0][0][4] = 632.04;
    numbers[0][1][0] = -378.05;
    numbers[0][1][1] = 48.14;
    numbers[0][1][2] = 634.18;
    numbers[0][1][3] = 762.48;
    numbers[0][1][4] = 83.02;
    numbers[0][2][0] = 64.92;
    numbers[0][2][1] = -7.44;
    numbers[0][2][2] = 86.74;
    numbers[0][2][3] = -534.60;
    numbers[0][2][4] = 386.73;
    numbers[1][0][0] = 48.02;
    numbers[1][0][1] = 120.44;
    numbers[1][0][2] = 38.62;
    numbers[1][0][3] = 526.82;
    numbers[1][0][4] = 1704.62;
    numbers[1][1][0] = 56.85;
    numbers[1][1][1] = 105.48;
    numbers[1][1][2] = 363.31;
    numbers[1][1][3] = 172.62;
    numbers[1][1][4] = 128.48;
    numbers[1][2][0] = 906.68;
    numbers[1][2][1] = 47.12;
    numbers[1][2][2] = -166.07;
    numbers[1][2][3] = 4444.26;
    numbers[1][2][4] = 408.62;


    cout << "Numbers[0][0][0]: " <<  numbers[0][0][0] << endl;
    cout << "Numbers[0][0][1]: " <<  numbers[0][0][1] << endl;
    cout << "Numbers[0][0][2]: " <<  numbers[0][0][2] << endl;
    cout << "Numbers[0][0][3]: " <<  numbers[0][0][3] << endl;
    cout << "Numbers[0][0][4]: " <<  numbers[0][0][4] << endl;

    cout << "Numbers[0][1][0]: " <<  numbers[0][1][0] << endl;
    cout << "Numbers[0][1][1]: " <<  numbers[0][1][1] << endl;
    cout << "Numbers[0][1][2]: " <<  numbers[0][1][2] << endl;
    cout << "Numbers[0][1][3]: " <<  numbers[0][1][3] << endl;
    cout << "Numbers[0][1][4]: " <<  numbers[0][1][4] << endl;

    cout << "Numbers[0][2][0]: " << numbers[0][2][0] << endl;
    cout << "Numbers[0][2][1]: " << numbers[0][2][1] << endl;
    cout << "Numbers[0][2][2]: " << numbers[0][2][2] << endl;
    cout << "Numbers[0][2][3]: " << numbers[0][2][3] << endl;
    cout << "Numbers[0][2][4]: " << numbers[0][2][4] << endl;

    cout << "Numbers[1][0][0]: " << numbers[1][0][0] << endl;
    cout << "Numbers[1][0][1]: " << numbers[1][0][1] << endl;
    cout << "Numbers[1][0][2]: " << numbers[1][0][2] << endl;
    cout << "Numbers[1][0][3]: " << numbers[1][0][3] << endl;
    cout << "Numbers[1][0][4]: " << numbers[1][0][4] << endl;

    cout << "Numbers[1][1][0]: " << numbers[1][1][0] << endl;
    cout << "Numbers[1][1][1]: " << numbers[1][1][1] << endl;
    cout << "Numbers[1][1][2]: " << numbers[1][1][2] << endl;
    cout << "Numbers[1][1][3]: " << numbers[1][1][3] << endl;
    cout << "Numbers[1][1][4]: " << numbers[1][1][4] << endl;

    cout << "Numbers[1][2][0]: " << numbers[1][2][0] << endl;
    cout << "Numbers[1][2][1]: " << numbers[1][2][1] << endl;
    cout << "Numbers[1][2][2]: " << numbers[1][2][2] << endl;
    cout << "Numbers[1][2][3]: " << numbers[1][2][3] << endl;
    cout << "Numbers[1][2][4]: " << numbers[1][2][4] << endl;

    cout << "=============================\n";
}

Looping a Multidimensional Array

Since the lists are nested, if you want to use a loop to access the members of the array, you can nest the for loops to incrementally access the values. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    float numbers[2][3][5] =
    {
        {
            {   12.44, 525.38,   -6.28, 2448.32,  632.04 },
            { -378.05,  48.14,  634.18,  762.48,   83.02 },
            {   64.92,  -7.44,   86.74, -534.60,  386.73 }
        },
        {
            {   48.02, 120.44,   38.62,  526.82, 1704.69 },
            {   56.85, 105.48,  363.31,  172.62,  128.48 },
            {  906.68,  47.12, -166.07, 4444.26,  408.62 }
        }
    };

    for(unsigned short outside = 0; outside < 2; outside++)
        for(unsigned short inside = 0; inside < 3; inside++)
            for(unsigned short value = 0; value < 5; value++)
                cout << "Numbers[" << outside << "][" << inside << "][" << value << "]: " << numbers[outside][inside][value] << endl;

    cout << "=============================\n";
}

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2026, FunctionX Monday 06 October 2025, 17:00 Next