C++ Multi-Dimensional Arrays

A multi-dimensional array is essentially an array of arrays, allowing data to be stored in structured layers.

Tutorials dojo strip

Declaring a Multi-Dimensional Array

Each additional set of square brackets ([][]) defines another dimension for the array.

Example: Two-Dimensional Array

C++

This creates a 2×4 array, meaning:

  • 2 rows (categories)
  • 4 columns (models per category)

Values can be assigned using nested curly braces:

C++

Three-Dimensional Arrays

Adding another set of brackets [3] creates a three-dimensional array, increasing complexity:

C++

With three dimensions, values are organized in layers.

Accessing Multi-Dimensional Array Elements

To retrieve a specific value, reference each dimension’s index:

Example: Getting an Element

C++

Indexes start at 0, so:

  • [0][2] retrieves the third element (“Goldwing”).

Modifying an Element

Changing an array element follows the same approach:

C++

Looping Through a Multi-Dimensional Array

To iterate over a multi-dimensional array, use nested loops.

Example: Iterating Over Two-Dimensional Array

C++

Looping Through a Three-Dimensional Array

For three dimensions, add another loop:

C++

Practical Application of Multi-Dimensional Arrays

Multi-dimensional arrays are useful for structured data storage, such as creating grids, tables, and even game mechanics.

Example: Tracking Stock Availability

C++

This structure could represent:

  • 0: Out of stock
  • 1: Available

Tutorials dojo strip