A simple 2-Dimensional Arrays in C
Introduction
In C programming, arrays can be extended into multiple dimensions. While a 1-dimensional array represents a list of elements, a 2-dimensional array can represent data in rows and columns, much like a table or a grid. This guide introduces 2-dimensional arrays, explains how they work, and includes examples to solidify your understanding.
What is a 2-Dimensional Array?
A 2-dimensional (2D) array in C is an array of arrays. You can imagine it like a table with rows and columns, where each cell stores a value. For example, a 3×3 array would have 3 rows and 3 columns, totaling 9 elements. This can be useful when working with data that naturally forms a grid, like a matrix, game board, or any table of data.
Declaring a 2-Dimensional Array
To declare a 2-dimensional array, you need to specify two sizes: the number of rows and the number of columns. Here’s the syntax:
data_type array_name[rows][columns];
For example:
int matrix[3][3];
This line declares a 3×3 2D array called matrix
, which can store 9 integers (3 rows × 3 columns).
Example Program: Initializing and Printing a 2D Array
Let’s walk through a program that:
- Declares a 2D array.
- Initializes it with values.
- Prints each value in a grid format.
Code
#include <stdio.h>
int main() {
// Declaring and initializing a 2D array with 3 rows and 3 columns
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Printing the 2D array in matrix form
printf("The 3x3 matrix is:\n");
for(int i = 0; i < 3; i++) { // Loop through each row
for(int j = 0; j < 3; j++) { // Loop through each column in the current row
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Step-by-Step Explanation
Let’s go over the code line by line.
- Including the Standard Input-Output Library
#include <stdio.h>
This line includes the standard input-output library, which allows us to use functions like printf
for displaying output.
- Starting the main function
int main() {
The main
function is where the program starts executing.
- Declaring and Initializing the 2D Array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
- Here, we declare a 2D array called
matrix
with 3 rows and 3 columns. - We initialize the array with values: the first row contains
1, 2, 3
, the second row contains4, 5, 6
, and the third row contains7, 8, 9
. - This creates a 3×3 grid as shown below:
1 2 3 4 5 6 7 8 9
- Printing a Message
printf("The 3x3 matrix is:\n");
This line prints a message to indicate that the following output is the 3×3 matrix.
- Using Nested Loops to Access Each Element
for(int i = 0; i < 3; i++) { // Loop through each row
for(int j = 0; j < 3; j++) { // Loop through each column in the current row
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next line after each row
}
- Outer Loop: The first
for
loop withi
goes from 0 to 2, representing the rows.i
is the row index. - Inner Loop: The second
for
loop withj
goes from 0 to 2, representing the columns within each row.j
is the column index. matrix[i][j]
accesses each element in the array. For example:matrix[0][0]
refers to the element in the first row, first column, which is1
.matrix[1][2]
refers to the element in the second row, third column, which is6
.
- The
printf("\n");
line at the end of the outer loop moves to the next line after each row, creating a grid format.
- Ending the Program
return 0;
}
This line indicates that the program ended successfully.
Example Output
The program prints:
The 3x3 matrix is:
1 2 3
4 5 6
7 8 9
Each row of the array is printed in a new line, creating a matrix format.
Example 2: Taking User Input in a 2D Array
In this example, we’ll modify the program to take user input to fill the 2D array and then display it.
Code
#include <stdio.h>
int main() {
int matrix[3][3];
// Taking input from the user
printf("Enter 9 numbers to fill the 3x3 matrix:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("Enter element for row %d, column %d: ", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
}
// Printing the 2D array in matrix form
printf("The 3x3 matrix is:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Explanation
- The program first prompts the user to enter 9 numbers to fill the 3×3 matrix.
- Each
scanf
statement captures a number and stores it in the corresponding position inmatrix
. - After collecting the input, the nested loop structure prints the 2D array in a matrix form.
Example Output
Enter 9 numbers to fill the 3x3 matrix:
Enter element for row 1, column 1: 1
Enter element for row 1, column 2: 2
Enter element for row 1, column 3: 3
Enter element for row 2, column 1: 4
Enter element for row 2, column 2: 5
Enter element for row 2, column 3: 6
Enter element for row 3, column 1: 7
Enter element for row 3, column 2: 8
Enter element for row 3, column 3: 9
The 3x3 matrix is:
1 2 3
4 5 6
7 8 9
Conclusion
2-dimensional arrays allow you to organize and access data in a grid format. By using rows and columns, you can easily represent matrices and tables in C. Practice with different sizes of arrays and try using nested loops to perform operations on each element.
Happy Coding!