C Program to Print a 2D Array

Introduction

A 2D array is an array of arrays, which can be used to represent a matrix or a table of values. This guide will show you how to write a C program to input and print a 2D array.

Problem Statement

Create a C program that:

  • Takes the number of rows and columns as input from the user.
  • Takes the elements of the 2D array as input.
  • Prints the 2D array in matrix form.

Example:

  • Input: Rows = 2, Columns = 3, Elements = [1, 2, 3, 4, 5, 6]
  • Output:
    1 2 3
    4 5 6
    

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the number of rows and columns, and the 2D array itself.
  4. Input the Dimensions of the Array: Use scanf to take input from the user for the number of rows and columns.
  5. Input the Elements of the 2D Array: Use nested loops to take input from the user for the elements of the 2D array.
  6. Print the 2D Array: Use nested loops to print the 2D array in matrix form.

C Program

#include <stdio.h>

/**
 * C Program to Print a 2D Array
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the number of rows, columns, and the 2D array
    int rows, cols;

    // Step 2: Prompt the user to enter the number of rows and columns
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    // Step 3: Declare the 2D array with the given dimensions
    int arr[rows][cols];

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

    // Step 5: Print the 2D array
    printf("The 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");  // Move to the next row
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables rows and cols are declared to store the number of rows and columns of the 2D array. The 2D array arr is declared with dimensions [rows][cols] based on user input.

Step 2: Input the Dimensions of the Array

  • The program prompts the user to enter the number of rows and columns using printf. The scanf function reads the input and stores it in the variables rows and cols.

Step 3: Declare the 2D Array

  • The program declares the 2D array arr with the dimensions [rows][cols] provided by the user.

Step 4: Input the Elements of the 2D Array

  • The program uses nested for loops to take input for each element of the 2D array. The outer loop iterates over the rows, and the inner loop iterates over the columns. The scanf function reads the input and stores it in the corresponding position in the array.

Step 5: Print the 2D Array

  • The program uses another set of nested for loops to print the 2D array in matrix form. The outer loop iterates over the rows, and the inner loop iterates over the columns. The printf function is used to display each element, followed by a space. After each row is printed, the program moves to the next line using printf("\n");.

Step 6: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements of the array:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
The 2D array is:
1 2 3 
4 5 6

Another Example:

Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of the array:
Element at [0][0]: 7
Element at [0][1]: 8
Element at [1][0]: 9
Element at [1][1]: 10
Element at [2][0]: 11
Element at [2][1]: 12
The 2D array is:
7 8 
9 10 
11 12

Conclusion

This C program demonstrates how to input and print a 2D array. It covers basic concepts such as arrays, loops, and handling user input, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top