Multidimensional Array Documentation

1. What is a Multidimensional Array?

A multidimensional array is an array of arrays. Instead of a simple linear structure (like a 1D array), you have multiple dimensions — each index leads to another array.

[
 [1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12]
]

This can be visualized as a table with 3 rows and 4 columns.

2. Types of Multidimensional Arrays

Type Description Example Dimensions
1D Array Linear collection of elements [1, 2, 3, 4]
2D Array Array of arrays [[1,2],[3,4]]
3D Array Array of 2D arrays [[[1],[2]],[[3],[4]]]
N-D Array Extends to N dimensions (rare) N > 3

3. Declaration & Initialization

C / C++

int arr[3][4];
int arr[2][2][3];

Java

int[][] arr = new int[3][4];
int[][][] arr = new int[2][2][3];

Python

arr = [[1, 2], [3, 4]]

import numpy as np
arr = np.array([[1, 2], [3, 4]])

JavaScript

let arr = [
  [1, 2],
  [3, 4]
];

4. Accessing Elements

arr[1][2]
arr[1][2][0]

5. Memory Representation

Row-major order (C/C++)

arr[2][3] = {
  {1,2,3},
  {4,5,6}
}
Memory: 1 2 3 4 5 6

Column-major order (Fortran/Matlab)

Memory: 1 4 2 5 3 6

6. Traversing Techniques

C++

for(int i = 0; i < rows; i++)
  for(int j = 0; j < cols; j++)
    cout << arr[i][j];

Python

for row in arr:
    for col in row:
        print(col)

7. Advantages & Use Cases

Use Case Description
Matrix Operations Used in math-heavy computations
Images (Pixels) Each pixel has rows and columns
Gaming (Grids, Boards) For representing chess, maze, etc.
Tabular Data Store spreadsheets, table records
Simulations Weather, physics, etc.
Deep Learning Tensors Used in neural networks

8. Common Operations

Operation Example Syntax (Python) Meaning
Access arr[i][j] Get value
Update arr[i][j] = 10 Set value
Append Row arr.append([7,8]) Add row
Remove Row arr.pop(1) Remove row
Length len(arr) No. of rows
Column Length len(arr[0]) No. of columns

9. Real-world Examples

Tic Tac Toe Board

let board = [
  ['X', 'O', 'X'],
  ['O', 'X', ''],
  ['X', '', 'O']
];

Image Representation

image = [
  [0, 255, 128],
  [128, 128, 255],
  [0, 64, 128]
]

10. Common Mistakes

Mistake Why It Happens Fix
Index Out of Bounds Accessing beyond limit Validate indices
Jagged Array Confusion Assuming all rows same length Check row lengths
Memory Misunderstanding Especially in C/C++ Understand memory layout
Wrong Loop Order Reversed loop indexing Ensure correct order

11. Language Comparison Table

Language Multi-D Support Jagged Arrays Row-major
C/C++ Yes No Yes
Java Yes (Jagged default) Yes Yes
Python No (via lists) Yes Yes
JavaScript No (via arrays) Yes Yes
MATLAB Yes No No

12. Diagrams

2D Array (Row-Major)

Matrix:
[ [1, 2, 3],
  [4, 5, 6] ]

Memory:
Index  -> 0  1  2  3  4  5
Element-> 1  2  3  4  5  6

3D Array Representation

arr[2][2][2] = [
   [ [1, 2], [3, 4] ],
   [ [5, 6], [7, 8] ]
]

13. Summary

3×4 Random Matrix Generator

Code:

let arr = Array.from({ length: 3 }, () => 
  Array.from({ length: 4 }, () => Math.floor(Math.random() * 100))
);
process.stdout.write(`${JSON.stringify(arr)}\\n`);

Explanation:

Sample Output:

[[14, 73, 6, 91],
 [65, 43, 9, 87],
 [27, 88, 19, 54]]