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:
- We declare two variables rowsandcolsto store the dimensions of the 2D array.
- The user inputs the number of rows and columns, which are stored using scanf().
- A 2D array arris declared dynamically using the entered dimensions.
- Two nested loops iterate through each element, reading input values using scanf().
- 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 62. 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:
- We declare rowsandcolsto store the dimensions of the 2D array.
- The user inputs the dimensions, and they are stored using scanf().
- A 2D array arris declared dynamically based on user input.
- We initialize a variable sumto 0 to store the total sum.
- Two nested loops read the array elements using scanf()and add them tosum.
- 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: 10Conclusion
In this tutorial, we explored how to read a 2D array in C with practical examples:
- Reading and Displaying a 2D Array: We used nested loops to input and print a 2D array in matrix format.
- Summing Elements of a 2D Array: We read an array and computed the sum of all its elements.
