How to Read a 2D Array in C

In C, we can read a 2D array using nested loops. A 2D array consists of rows and columns, and we use a loop to iterate through each row and another loop to access each column within that row.

In this tutorial, we will provide a step-by-step guidance on how to read a 2D array in C with multiple examples.


Examples to Read a 2D Array

1. Reading and Displaying a 2D Array

In this example, we will read a 2D array from user input and display it in matrix form.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows, cols;

    // Read the dimensions of the 2D array
    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    int arr[rows][cols];

    // Reading elements of the array
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    // Displaying the array
    printf("The entered 2D array is:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare two variables rows and cols to store the dimensions of the 2D array.
  2. The user inputs the number of rows and columns, which are stored using scanf().
  3. A 2D array arr is declared dynamically using the entered dimensions.
  4. Two nested loops iterate through each element, reading input values using scanf().
  5. Another set of nested loops prints the elements in matrix format.

Output:

Enter number of rows and columns: 2 3
Enter the elements of the array:
1 2 3
4 5 6
The entered 2D array is:
1 2 3
4 5 6

2. Reading a 2D Array and Summing Elements

In this example, we will read a 2D array from user input and calculate the sum of all its elements.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows, cols, sum = 0;

    // Read the dimensions of the array
    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);

    int arr[rows][cols];

    // Reading elements of the array
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &arr[i][j]);
            sum += arr[i][j];  // Adding each element to sum
        }
    }

    // Displaying the sum
    printf("The sum of all elements is: %d\n", sum);

    return 0;
}

Explanation:

  1. We declare rows and cols to store the dimensions of the 2D array.
  2. The user inputs the dimensions, and they are stored using scanf().
  3. A 2D array arr is declared dynamically based on user input.
  4. We initialize a variable sum to 0 to store the total sum.
  5. Two nested loops read the array elements using scanf() and add them to sum.
  6. After reading all elements, the total sum is printed using printf().

Output:

Enter number of rows and columns: 2 2
Enter the elements of the array:
1 2
3 4
The sum of all elements is: 10

Conclusion

In this tutorial, we explored how to read a 2D array in C with practical examples:

  1. Reading and Displaying a 2D Array: We used nested loops to input and print a 2D array in matrix format.
  2. Summing Elements of a 2D Array: We read an array and computed the sum of all its elements.