C++ Multi Dimensional Array

Hi everyone, inside this article we will see about C++ Multi Dimensional Array.

In C++, a multidimensional array is an array that has more than one dimension. It can be thought of as a table of elements, where each element is identified by a pair of indices.

A Basic Example

Here is an example program that demonstrates how to declare and initialize a two-dimensional array in C++:

#include <iostream>

using namespace std;

int main() {
  // Declare a 2D array of 3 rows and 4 columns
  int arr[3][4];

  // Initialize the elements of the array
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      arr[i][j] = i * j;
    }
  }

  // Print the elements of the array
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      cout << arr[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}

In this program, we declare a two-dimensional array called arr with 3 rows and 4 columns. We then initialize the elements of the array using a nested for loop. Finally, we print the elements of the array using another nested for loop.

The output of this program will be:

0 0 0 0
0 1 2 3
0 2 4 6

In this example, we initialized the elements of the array using the formula i * j, where i represents the row and j represents the column. However, you can initialize the elements to any values you want, such as user input or values calculated by your program.

Properties of C++ Multi Dimensional Array

In C++, a multidimensional array is an array that has more than one dimension. Here are some of the key properties of a multidimensional array in C++:

Homogeneous: A multidimensional array is homogeneous, meaning all elements of the array must be of the same data type.

Fixed Size: The size of a multidimensional array is fixed at the time of declaration, meaning the number of elements in each dimension cannot be changed during the execution of the program.

Contiguous Memory Allocation: The elements of a multidimensional array are stored in contiguous memory locations, meaning each element is adjacent to the previous and next elements in memory.

Indexed Access: The elements of a multidimensional array are accessed using one or more indices, depending on the number of dimensions in the array.

Easy Traversal: The elements of a multidimensional array can be traversed easily using nested loops.

Time Complexity: The time complexity for accessing an element in a multidimensional array is constant, O(1), because the indices can be used to access the element directly.

Direct Memory Access: In C++, a multidimensional array provides direct access to the memory location of its elements, which allows for fast and efficient memory access.

Multi-Dimensional: A multidimensional array can have two or more dimensions, making it possible to represent complex data structures in a compact and efficient way.

These properties make multidimensional arrays a useful data structure for storing and manipulating collections of related data in C++. However, it is important to note that as the number of dimensions increases, the complexity of accessing and manipulating the data also increases.

We hope this article helped you to understand about C++ Multi Dimensional Array in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.