C Multidimensional Arrays

Introduction

In the previous chapter, we explored one-dimensional arrays in C. In this chapter, we will focus on multidimensional arrays. Multidimensional arrays are arrays of arrays and can have two or more dimensions. The most commonly used multidimensional array is the two-dimensional array, which is often used to represent matrices or tables.

What is a Multidimensional Array?

A multidimensional array is an array of arrays. In a two-dimensional array, the elements are arranged in rows and columns, forming a matrix. Each element in a multidimensional array can be accessed using multiple indices, one for each dimension.

Syntax

The basic syntax for declaring a two-dimensional array in C is as follows:

data_type array_name[rows][columns];
  • data_type: The type of elements stored in the array (e.g., int, float, char).
  • array_name: The name of the array.
  • rows: The number of rows in the array.
  • columns: The number of columns in the array.

Example: Declaring and Initializing a Two-Dimensional Array

Let’s look at a simple example to understand how to declare and initialize a two-dimensional array.

Example: Declaring and Initializing an Integer Matrix

#include <stdio.h>

int main() {
    // Declaring and initializing a 2D array
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Accessing and printing 2D array elements
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("Element at matrix[%d][%d]: %d\n", i, j, matrix[i][j]);
        }
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element at matrix[0][0]: 1
Element at matrix[0][1]: 2
Element at matrix[0][2]: 3
Element at matrix[1][0]: 4
Element at matrix[1][1]: 5
Element at matrix[1][2]: 6

In this example, we declared and initialized a two-dimensional array named matrix with 2 rows and 3 columns. We then used nested loops to access and print each element in the array.

Accessing Array Elements

Array elements in a two-dimensional array are accessed using their row and column indices. The first element of an array is at index [0][0], the second element is at index [0][1], and so on.

Example: Accessing Array Elements

#include <stdio.h>

int main() {
    int matrix[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };

    // Accessing and printing specific elements
    printf("Element at matrix[0][0]: %d\n", matrix[0][0]);
    printf("Element at matrix[1][2]: %d\n", matrix[1][2]);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element at matrix[0][0]: 10
Element at matrix[1][2]: 60

Modifying Array Elements

Array elements in a two-dimensional array can be modified by assigning new values to them using their row and column indices.

Example: Modifying Array Elements

#include <stdio.h>

int main() {
    int matrix[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };

    // Modifying specific elements
    matrix[0][0] = 100;
    matrix[1][2] = 600;

    // Accessing and printing modified elements
    printf("Element at matrix[0][0]: %d\n", matrix[0][0]);
    printf("Element at matrix[1][2]: %d\n", matrix[1][2]);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element at matrix[0][0]: 100
Element at matrix[1][2]: 600

Array Initialization

Two-dimensional arrays can be initialized at the time of declaration. If the array size is not specified for one or more dimensions, the compiler determines the size based on the number of elements in the initialization list.

Example: Array Initialization

#include <stdio.h>

int main() {
    // Array initialization without specifying size
    int matrix[][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Determining the size of the array
    int rows = sizeof(matrix) / sizeof(matrix[0]);
    int columns = sizeof(matrix[0]) / sizeof(matrix[0][0]);

    // Accessing and printing array elements
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            printf("Element at matrix[%d][%d]: %d\n", i, j, matrix[i][j]);
        }
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element at matrix[0][0]: 1
Element at matrix[0][1]: 2
Element at matrix[0][2]: 3
Element at matrix[1][0]: 4
Element at matrix[1][1]: 5
Element at matrix[1][2]: 6

Simple C Programs to Demonstrate Two-Dimensional Arrays

Program 1: Addition of Two Matrices

Example:

#include <stdio.h>

int main() {
    int matrix1[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int matrix2[2][3] = {
        {7, 8, 9},
        {10, 11, 12}
    };
    int sum[2][3];

    // Adding two matrices
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    // Printing the sum matrix
    printf("Sum of the two matrices:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Sum of the two matrices:
8 10 12
14 16 18

Program 2: Transposing a Matrix

Example:

#include <stdio.h>

int main() {
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int transpose[3][2];

    // Transposing the matrix
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Printing the transposed matrix
    printf("Transposed matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Transposed matrix:
1 4
2 5
3 6

Program 3: Multiplying Two Matrices

Example:

#include <stdio.h>

int main() {
    int matrix1[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int matrix2[3][2] = {
        {7, 8},
        {9, 10},
        {11, 12}
    };
    int product[2][2] = {0};

    // Multiplying two matrices
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 3; k++) {
                product[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    // Printing the product matrix
    printf("Product of the two matrices:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", product[i][j]);
        }
        printf("\n");
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Product of the two matrices:
58 64
139 154

In this example, we multiply two matrices matrix1 and matrix2 to get the resulting product matrix. We use three nested loops: the outer two loops iterate over the rows and columns of the resulting matrix, and the innermost loop performs the multiplication and addition required for matrix multiplication.

Array of Arrays

C also allows arrays of arrays, which can be more than two dimensions. However, the syntax and usage become more complex with higher dimensions. Let’s consider an example of a three-dimensional array.

Example: Declaring and Initializing a Three-Dimensional Array

#include <stdio.h>

int main() {
    // Declaring and initializing a 3D array
    int array[2][2][3] = {
        {
            {1, 2, 3},
            {4, 5, 6}
        },
        {
            {7, 8, 9},
            {10, 11, 12}
        }
    };

    // Accessing and printing 3D array elements
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 3; k++) {
                printf("Element at array[%d][%d][%d]: %d\n", i, j, k, array[i][j][k]);
            }
        }
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element at array[0][0][0]: 1
Element at array[0][0][1]: 2
Element at array[0][0][2]: 3
Element at array[0][1][0]: 4
Element at array[0][1][1]: 5
Element at array[0][1][2]: 6
Element at array[1][0][0]: 7
Element at array[1][0][1]: 8
Element at array[1][0][2]: 9
Element at array[1][1][0]: 10
Element at array[1][1][1]: 11
Element at array[1][1][2]: 12

In this example, we declared and initialized a three-dimensional array named array with dimensions 2x2x3. We then used nested loops to access and print each element in the array.

Conclusion

Multidimensional arrays are a powerful feature in C that allows you to store and manage data in more than one dimension. They are particularly useful for representing matrices, tables, and other complex data structures. By understanding how to declare, initialize, and manipulate multidimensional arrays, you can write more efficient and organized code for a wide range of applications. The concepts of two-dimensional arrays and higher-dimensional arrays provide a solid foundation for handling complex data in C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top