Scroll Progress Bar

Double Dimensional Array

A double-dimensional array, also known as a two-dimensional array, is an array of arrays. It is a collection of elements arranged in rows and columns, forming a grid-like structure. Each element in a double-dimensional array is identified by two indices - one for the row and another for the column. It is often used to represent matrices and tables in C.

Usage and Syntax:

To declare a double-dimensional array in C, you specify the data type of the elements, followedby the array name, and the size of rows and columns in square brackets.

data_type array_name[row_size][column_size];
Sample Code with Explanation and Output:

#include <stdio.h>

int main() {
    // Declaration and initialization of a 3x4 integer double-dimensional array
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Accessing and printing the elements of the double-dimensional array
    printf("Matrix Elements:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}
Output:

Matrix Elements:
1       2       3       4
5       6       7       8
9       10      11      12
Explanation:
  • In the sample code, declare and initialize a 3x4 integer double-dimensional array matrix.
  • The array is initialized with the following values:
    
            {{1, 2, 3, 4}, 
            {5, 6, 7, 8}, 
            {9, 10, 11, 12}}
        
  • Use nested for loops to access and print the elements of the double-dimensional array in a grid-like format.
  • The outer loop runs three times for the rows (0 to 2), and the inner loop runs four times for the columns (0 to 3).
  • Use the indices i and j to access the elements using matrix[i][j].
  • The printf statement prints each element, and a tab character \t is used to separate the elements within a row.
  • The inner loop prints a newline \n after each row to format the output as a matrix.

What is a double-dimensional array in C?


Matrix

How do you declare a double-dimensional array in C?


Define

What is the purpose of double indexing in arrays?


Access

What is the size of a 2D array defined as 'int arr[3][4]'?


Twelve

What do you use to iterate through a 2D array row by row in C?


Nesting