Multi-dimensional Arrays
A multi-dimensional array is an array of arrays. The most common type is a 2D array (two-dimensional), but you can have 3D, 4D, or even higher dimensions.
Imagine a multi-dimensional array as a spreadsheet or table with rows and columns. Where each row is an array of cells, but if you look at the sheet sheet, sheet is nothing but an array of rows.
2D Array Visualization
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
This represents a 3×3 grid where each element is accessed using two indices: [row][column].
Declaring Multi-dimensional Arrays
2D Array Declaration
// Method 1: Specify both dimensions
int matrix[3][4]; // 3 rows, 4 columns
// Method 2: Initialize during declaration
int numbers[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Method 3: Let compiler determine first dimension
int grid[][3] = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
3D Array Declaration
// A 3D array - think of it as multiple 2D arrays stacked together
int cube[2][3][4]; // 2 layers, 3 rows, 4 columns each
// With initialization
int data[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
Accessing Elements
Use multiple indices to access elements in multi-dimensional arrays:
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing elements
printf("Element at [0][0]: %d\n", matrix[0][0]); // Output: 1
printf("Element at [1][2]: %d\n", matrix[1][2]); // Output: 6
printf("Element at [2][1]: %d\n", matrix[2][1]); // Output: 8
// Modifying elements
matrix[1][1] = 100;
printf("Modified element at [1][1]: %d\n", matrix[1][1]); // Output: 100
return 0;
}
Working with 2D Arrays
Printing a 2D Array
#include <stdio.h>
int main() {
int scores[3][4] = {
{85, 90, 78, 92},
{88, 76, 95, 89},
{91, 83, 87, 94}
};
printf("Student Scores:\n");
for(int i = 0; i < 3; i++) {
printf("Student %d: ", i + 1);
for(int j = 0; j < 4; j++) {
printf("%d ", scores[i][j]);
}
printf("\n");
}
return 0;
}
Finding Sum of All Elements
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int sum = 0;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
printf("Sum of all elements: %d\n", sum); // Output: 21
return 0;
}
Memory Layout
Multi-dimensional arrays are stored in row-major order in memory:
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
Memory layout: [1][2][3][4][5][6]
Common Use Cases
| Use Case | Example |
|---|---|
| Game Boards | Tic-tac-toe, Chess, Sudoku |
| Images | Pixel data (RGB values) |
| Mathematical Matrices | Linear algebra operations |
| Tables/Spreadsheets | Student grades, sales data |
| Maps/Grids | 2D game worlds, maze representations |
Important Notes
- Size must be known at compile time for static arrays
- First dimension can be omitted during initialization, but subsequent dimensions must be specified
- Nested loops are commonly used to traverse multi-dimensional arrays
- Memory is allocated contiguously in row-major order
Multi-dimensional arrays are powerful tools for organizing data in a structured, grid-like format. They're essential for many programming tasks, from simple data storage to complex mathematical computations!