C Multidimensional Arrays

Arrays of arrays for complex data structures

🔲 What are Multidimensional Arrays?

Multidimensional arrays in C are arrays containing other arrays as elements. They're perfect for representing matrices, tables, and grid-like data structures with rows and columns for organized data storage.


// 2D array example - 3 rows, 4 columns
int matrix[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
printf("Element at row 1, col 2: %d", matrix[1][2]);
                                    

Types of Multidimensional Arrays

📊

2D Arrays

Most common - like a table

int table[3][4];  // 3 rows, 4 columns
🧊

3D Arrays

Three dimensions - like a cube

int cube[2][3][4];  // 2x3x4 cube
🎯

Matrix Operations

Mathematical matrix operations

int A[2][2], B[2][2], C[2][2];
🎮

Game Boards

Perfect for tic-tac-toe, chess

char board[8][8];  // Chess board

🔹 2D Array Declaration and Initialization

2D arrays represent data organized in rows and columns, similar to a matrix or spreadsheet. You can declare them as int array[rows][columns] or dynamically allocate memory for flexibility. Initialization can be done directly with values like int grid[2][3] = {{1,2,3}, {4,5,6}}, or you can assign values individually after declaration. Understanding 2D array structure helps organize complex data efficiently in memory.

// Method 1: Declare then assign
int matrix[2][3];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;

// Method 2: Initialize with nested braces
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};

// Method 3: Initialize without inner braces
int table[2][3] = {1, 2, 3, 4, 5, 6};

// Method 4: Partial initialization
int scores[3][2] = {{100, 95}, {88}}; // Rest are 0

Visual Representation:

grid[2][3] looks like:

Row 0: [1] [2] [3]
Row 1: [4] [5] [6]

🔹 Accessing 2D Array Elements

Access individual elements in 2D arrays using two indices: the row index and column index. The syntax is array[row][column], where indices typically start at zero. For example, grid[0][0] accesses the top-left element, grid[1][2] accesses the element in the second row and third column. You can both read and modify elements using this notation, making it easy to manipulate data within the two-dimensional structure.

#include <stdio.h>

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    // Access specific elements
    printf("Top-left corner: %d\n", matrix[0][0]);     // 1
    printf("Center element: %d\n", matrix[1][1]);      // 5
    printf("Bottom-right: %d\n", matrix[2][2]);        // 9
    
    // Modify an element
    matrix[1][1] = 50;
    printf("Modified center: %d\n", matrix[1][1]);     // 50
    
    return 0;
}

Output:

Top-left corner: 1

Center element: 5

Bottom-right: 9

Modified center: 50

🔹 Nested Loops with 2D Arrays

Nested loops are essential for processing all elements in 2D arrays systematically. The outer loop typically iterates through rows using for(int i=0; i, while the inner loop iterates through columns with for(int j=0; j. This double-nested approach ensures you visit each element exactly once, enabling operations like printing, summing, or transforming all values in the array efficiently and organized.

#include <stdio.h>

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    printf("Matrix elements:\n");
    
    // Print matrix in table format
    for(int i = 0; i < 3; i++) {        // Rows
        for(int j = 0; j < 3; j++) {    // Columns
            printf("%d ", matrix[i][j]);
        }
        printf("\n");  // New line after each row
    }
    
    // Calculate sum of all elements
    int sum = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            sum += matrix[i][j];
        }
    }
    printf("Sum of all elements: %d\n", sum);
    
    return 0;
}

Output:

Matrix elements:
1 2 3 
4 5 6 
7 8 9 
Sum of all elements: 45

🔹 Matrix Operations

Matrix operations on 2D arrays include addition, multiplication, transposition, and determinant calculations. These operations are fundamental in linear algebra and computer graphics applications. Common operations involve iterating through array elements with nested loops, performing calculations, and storing results. Understanding matrix operations enables solving complex mathematical problems, image processing tasks, and scientific computations effectively within your C programs.

Matrix Addition:

void addMatrices(int A[2][2], int B[2][2], int C[2][2]) {
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            C[i][j] = A[i][j] + B[i][j];
        }
    }
}

Finding Maximum in Matrix:

int findMax(int matrix[3][3]) {
    int max = matrix[0][0];
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            if(matrix[i][j] > max) {
                max = matrix[i][j];
            }
        }
    }
    return max;
}

🔹 3D Arrays

3D arrays extend the concept of 2D arrays by adding a third dimension, creating a cube-like structure. Declare them using int array[depth][rows][columns], conceptualizing them as multiple stacked 2D arrays. Access elements with three indices: array[d][i][j]. 3D arrays are useful for representing volumetric data, game grids with depth, or any scenario requiring three-dimensional organization. Triple-nested loops traverse all elements systematically.

#include <stdio.h>

int main() {
    // 3D array: 2 layers, 2 rows, 3 columns
    int cube[2][2][3] = {
        {{1, 2, 3}, {4, 5, 6}},      // First layer
        {{7, 8, 9}, {10, 11, 12}}    // Second layer
    };
    
    printf("3D Array elements:\n");
    
    for(int layer = 0; layer < 2; layer++) {
        printf("Layer %d:\n", layer);
        for(int row = 0; row < 2; row++) {
            for(int col = 0; col < 3; col++) {
                printf("%d ", cube[layer][row][col]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    return 0;
}

Output:

3D Array elements:
Layer 0:
1 2 3 
4 5 6 

Layer 1:
7 8 9 
10 11 12

🧠 Test Your Knowledge

How do you access the element in the 2nd row, 3rd column of a 2D array named 'arr'?