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
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the number of rows and columns, and the 2D array itself.
- Input the Dimensions of the Array: Use
scanfto take input from the user for the number of rows and columns. - Input the Elements of the 2D Array: Use nested loops to take input from the user for the elements of the 2D array.
- 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
rowsandcolsare declared to store the number of rows and columns of the 2D array. The 2D arrayarris 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. Thescanffunction reads the input and stores it in the variablesrowsandcols.
Step 3: Declare the 2D Array
- The program declares the 2D array
arrwith the dimensions[rows][cols]provided by the user.
Step 4: Input the Elements of the 2D Array
- The program uses nested
forloops 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. Thescanffunction 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
forloops to print the 2D array in matrix form. The outer loop iterates over the rows, and the inner loop iterates over the columns. Theprintffunction is used to display each element, followed by a space. After each row is printed, the program moves to the next line usingprintf("\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.