A Comprehensive Guide
A 2D array (two-dimensional array) is an array of arrays. It represents a tabular structure with rows and columns, similar to a matrix or spreadsheet. Each element in a 2D array is accessed using two indices: the row index and the column index.
Column 0 Column 1 Column 2
┌─────────┬─────────┬─────────┐
│ arr[0][0]│ arr[0][1]│ arr[0][2]│ ← Row 0
├─────────┼─────────┼─────────┤
│ arr[1][0]│ arr[1][1]│ arr[1][2]│ ← Row 1
├─────────┼─────────┼─────────┤
│ arr[2][0]│ arr[2][1]│ arr[2][2]│ ← Row 2
└─────────┴─────────┴─────────┘
data_type array_name[rows][columns];
int matrix[3][4]; // 3 rows, 4 columns
float grades[5][3]; // 5 rows, 3 columns
char board[8][8]; // 8x8 chess board
int matrix[2][3] = {
{1, 2, 3}, // Row 0
{4, 5, 6} // Row 1
};
int matrix[2][3] = {
{1, 2}, // Row 0: third element is 0
{4, 5, 6} // Row 1
};
int matrix[][3] = { // Rows automatically calculated
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
2D arrays are stored in row-major order in memory. This means elements are stored row by row in contiguous memory locations.
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
Memory Address: 1000 1004 1008 1012 1016 1020
Values: 1 2 3 4 5 6
Indices: [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
Elements are accessed using row and column indices (both starting from 0):
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("%d", matrix[0][0]); // Output: 1
printf("%d", matrix[1][2]); // Output: 6
printf("%d", matrix[2][1]); // Output: 8
matrix[1][1] = 50; // Modify element
#include <stdio.h>
int main() {
int rows = 3, cols = 3;
int matrix[rows][cols];
printf("Enter %d elements:\n", rows * cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
return 0;
}
void displayMatrix(int rows, int cols, int matrix[rows][cols]) {
printf("\nMatrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int rows, int cols, int A[rows][cols],
int B[rows][cols], int result[rows][cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
void transpose(int rows, int cols, int matrix[rows][cols],
int result[cols][rows]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
}
int findMax(int rows, int cols, int matrix[rows][cols]) {
int max = matrix[0][0];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
return max;
}
void processMatrix(int matrix[3][4]) {
// Function body
}
void processMatrix(int rows, int cols, int matrix[rows][cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void processMatrix(int *matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", *(matrix + i * cols + j));
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
// Allocate memory for rows
int **matrix = (int **)malloc(rows * sizeof(int *));
// Allocate memory for columns in each row
for(int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Initialize
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = i * cols + j + 1;
}
}
// Free memory
for(int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
int main() {
int rows = 3, cols = 4;
// Allocate single block of memory
int *matrix = (int *)malloc(rows * cols * sizeof(int));
// Access elements: matrix[i][j] = *(matrix + i * cols + j)
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
*(matrix + i * cols + j) = i * cols + j + 1;
}
}
free(matrix);
return 0;
}
#include <stdio.h>
#define STUDENTS 3
#define SUBJECTS 4
int main() {
float grades[STUDENTS][SUBJECTS];
float averages[STUDENTS] = {0};
// Input grades
printf("Enter grades for %d students (%d subjects each):\n",
STUDENTS, SUBJECTS);
for(int i = 0; i < STUDENTS; i++) {
printf("\nStudent %d:\n", i + 1);
for(int j = 0; j < SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%f", &grades[i][j]);
averages[i] += grades[i][j];
}
averages[i] /= SUBJECTS;
}
// Display results
printf("\nStudent\tAverage Grade\n");
printf("-------\t-------------\n");
for(int i = 0; i < STUDENTS; i++) {
printf("%d\t%.2f\n", i + 1, averages[i]);
}
return 0;
}
#include <stdio.h>
#define ROWS_A 2
#define COLS_A 3
#define COLS_B 2
void multiplyMatrices(int A[ROWS_A][COLS_A], int B[COLS_A][COLS_B],
int result[ROWS_A][COLS_B]) {
for(int i = 0; i < ROWS_A; i++) {
for(int j = 0; j < COLS_B; j++) {
result[i][j] = 0;
for(int k = 0; k < COLS_A; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
void displayMatrix(int rows, int cols, int matrix[rows][cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[ROWS_A][COLS_A] = {{1, 2, 3}, {4, 5, 6}};
int B[COLS_A][COLS_B] = {{7, 8}, {9, 10}, {11, 12}};
int result[ROWS_A][COLS_B];
multiplyMatrices(A, B, result);
printf("Matrix A:\n");
displayMatrix(ROWS_A, COLS_A, A);
printf("\nMatrix B:\n");
displayMatrix(COLS_A, COLS_B, B);
printf("\nResult (A × B):\n");
displayMatrix(ROWS_A, COLS_B, result);
return 0;
}
Matrix A:
1 2 3
4 5 6
Matrix B:
7 8
9 10
11 12
Result (A × B):
58 64
139 154