πŸŒ™

2D Array in C

A 2D array is an array of arrays, meaning it stores data in a row-column format like a table or matrix. Each element in the array is accessed using two indices:

First index β†’ Row number
Second index β†’ Column number

Syntax:

data_type array_name[rows][columns];
Example 1: Accessing stored values.
#include <stdio.h>

int main() {
    int matrix[3][3]= {
        {12,24,55},
        {3,7,45},
        {76,89,23}
    };
    int i, j;

    // Displaying the matrix
    printf("\nThe matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

The matrix is:

12	24	55	
3	7	45	
76	89	23
Example 2: storing and accessing.
#include <stdio.h>

int main() {
    int matrix[3][3]; // 3 rows, 3 columns
    int i, j;

    // Taking input
    printf("Enter elements for the matrix:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    // Displaying the matrix
    printf("\nThe matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter elements for the matrix:
Element [0][0]: 12
Element [0][1]: 24
Element [0][2]: 55
Element [1][0]: 3
Element [1][1]: 7
Element [1][2]: 45
Element [2][0]: 76
Element [2][1]: 89
Element [2][2]: 23

The matrix is:
12	24	55	
3	7	45	
76	89	23	
Example 3: Printing stars in Matrix
#include <stdio.h>

int main() {
   char stars[3][3];
    int i, j;
    
    // Displaying the matrix
    printf("\nThe matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("*\t", stars[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

The matrix is:
*	*	*	
*	*	*	
*	*	*
Example 4: Real World Example

A 2D array can be used to represent seats in a theater where rows and columns represent seats. Users can choose a seat, and we mark it as booked.

#include <stdio.h>

#define ROWS 3
#define COLS 4

int main() {
    int seats[ROWS][COLS] = {0}; // 0 means available, 1 means booked
    int row, col;

    // Display initial seating arrangement
    printf("Available seats (0 = Available, 1 = Booked):\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", seats[i][j]);
        }
        printf("\n");
    }

    // User selects a seat
    printf("\nEnter the row and column number to book (0-based index): ");
    scanf("%d %d", &row, &col);

    // Check if the seat is available
    if (row >= 0 && row < ROWS && col >= 0 && col < COLS) {
        if (seats[row][col] == 0) {
            seats[row][col] = 1;
            printf("Seat booked successfully!\n");
        } else {
            printf("Sorry, the seat is already booked!\n");
        }
    } else {
        printf("Invalid seat selection!\n");
    }

    // Display updated seating arrangement
    printf("\nUpdated seats:\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", seats[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Case 1: Booking an Available Seat

Available seats (0 = Available, 1 = Booked):
0 0 0 0
0 0 0 0
0 0 0 0

Enter the row and column number to book (0-based index): 1 2
Seat booked successfully!

Updated seats:
0 0 0 0
0 0 1 0
0 0 0 0

Case 2: Trying to Book an Already Booked Seat

Available seats (0 = Available, 1 = Booked):
0 0 0 0
0 0 1 0
0 0 0 0

Enter the row and column number to book (0-based index): 1 2
Sorry, the seat is already booked!

Updated seats:
0 0 0 0
0 0 1 0
0 0 0 0

❌ The system prevents double booking.

Case 3: Entering an Invalid Seat Number

Available seats (0 = Available, 1 = Booked):
0 0 0 0
0 0 1 0
0 0 0 0

Enter the row and column number to book (0-based index): 5 3
Invalid seat selection!

Updated seats:
0 0 0 0
0 0 1 0
0 0 0 0

Explanation:

1. We initialize a 2D array where 0 represents available seats.

2. We display the seat arrangement so the user knows what’s available.

3. User inputs the row & column number of the seat they want.

4. If the seat is available (0), we change it to 1 (booked).

5. If already booked, we inform the user.