2-D Array in C
A 2-Dimensional (2-D) array in C is an array of arrays, essentially representing a table or matrix. It stores elements in rows and columns, making it useful for storing tabular data like a grid or matrix.
Definition
A 2-D array is an array of arrays, where each element is accessed by two indices: one for the row and one for the column. The size of a 2-D array is determined by the number of rows and columns.
Syntax:
data_type array_name[row_size][column_size];
Example 1: Declaring and Initializing a 2-D Array
{%raw%}
#include <stdio.h>
int main()
{
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Declare and initialize a 2-D array
// Print elements of the array in matrix format
printf("Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]); // Print elements in a row
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
{%endraw%}
Output
Matrix:
1 2 3
4 5 6
Accessing Array Elements
In a 2-D array, each element is accessed by two indices: the row index and the column index. The row index represents the row, and the column index represents the element's position within that row.
Example 2: Accessing Elements Using Nested Loops
{%raw%}
#include <stdio.h>
int main()
{
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Printing the matrix
printf("Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%3d ", matrix[i][j]); // Print each element with proper spacing
}
printf("\n"); // Move to the next row
}
return 0;
}
{%endraw%}
Output
Matrix:
1 2 3
4 5 6
7 8 9
Example 3: Modifying Array Elements
{%raw%}
#include <stdio.h>
int main()
{
int matrix[2][2] = {{10, 20}, {30, 40}};
// Modify an element in the array
matrix[0][1] = 99;
// Print the modified matrix
printf("Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next row
}
return 0;
}
{%endraw%}
Output
Matrix:
10 99
30 40
Example 3: Modifying Array Elements
{%raw%}
#include <stdio.h>
int main()
{
int rows, cols, i, j;
// Input the number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare matrices
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
// Input the first matrix
printf("Enter elements of the first matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input the second matrix
printf("Enter elements of the second matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Calculate the sum of the matrices
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display the resulting sum matrix
printf("The resulting matrix after addition is:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
{%endraw%}
Output
Matrix:
10 99
30 40
Example 4: addition of two 2-dimensional arrays
{%raw%}
#include <stdio.h>
int main()
{
int rows, cols;
// Input the dimensions of the matrices
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare matrices
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
// Input elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Calculate the sum of the matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display the result
printf("Resultant Matrix (Sum):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
{%endraw%}
Output
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
Enter element [1][1]: 1
Enter element [1][2]: 2
Enter element [2][1]: 3
Enter element [2][2]: 4
Enter elements of the second matrix:
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][1]: 7
Enter element [2][2]: 8
Resultant Matrix (Sum):
6 8
10 12
Example 5: multiplication of two 2-dimensional arrays
{%raw%}
#include <stdio.h>
int main()
{
int rows1, cols1, rows2, cols2;
// Input dimensions of the first matrix
printf("Enter the number of rows for the first matrix: ");
scanf("%d", &rows1);
printf("Enter the number of columns for the first matrix: ");
scanf("%d", &cols1);
// Input dimensions of the second matrix
printf("Enter the number of rows for the second matrix: ");
scanf("%d", &rows2);
printf("Enter the number of columns for the second matrix: ");
scanf("%d", &cols2);
// Check if multiplication is possible
if (cols1 != rows2) {
printf("Matrix multiplication is not possible. The number of columns in the first matrix must equal the number of rows in the second matrix.\n");
return 1;
}
// Declare matrices
int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1][cols2];
// Initialize the result matrix with zeros
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}
// Input elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Perform matrix multiplication
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Display the result matrix
printf("Resultant Matrix (Multiplication):\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
{%endraw%}
Output
Enter the number of rows: 2
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 3
Enter the number of rows for the second matrix: 3
Enter the number of columns for the second matrix: 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant Matrix (Multiplication):
58 64
139 154
Example 6:Program for Transposing a Matrix
{%raw%}
#include <stdio.h>
int main()
{
int r, c, a[100][100], transpose[100][100], i, j;
// Input rows and columns
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
// Input elements of the matrix
printf("nEnter elements of the matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
// Transpose the matrix
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
// Print the original matrix
printf("nOriginal Matrix:\n");
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
// Print the transposed matrix
printf("nTransposed Matrix:\n");
for (i = 0; i < c; ++i) {
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
{%endraw%}
Output
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
Enter elements of the matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 6
Enter element a23: 7
Original Matrix:
1 2 3
4 6 7
Transposed Matrix:
1 4
2 6
3 7
- Key Points:
- Indexing:
- 2D arrays are indexed with two indices: [row][column].
- Example: arr[1][2] accesses the element at the second row, third column.
- Initialization:
- You can initialize a 2D array statically when declaring it.
- Example: int arr[2][3] = {% raw %}{{1, 2, 3}, {4, 5, 6}}{% endraw %};
- Memory Representation:
- 2D arrays are stored in row-major order in memory (i.e., all elements of one row are stored contiguously).
- Dynamic 2D Arrays:
- You can create 2D arrays dynamically using pointers, but this requires dynamic memory allocation with malloc() or calloc().