Skip to main content

2D Arrays and Matrices in C ๐Ÿงฎ

Mentor's Note: Think of a 1D array as a single row of houses. A 2D array, on the other hand, is like a multi-story apartment building or a spreadsheet grid. To locate a specific apartment, you need two pieces of information: the floor number (Row) and the door number (Column). ๐Ÿ’ก

๐Ÿ“š Educational Content: Matrix operations like addition, transpose, and multiplication are classic topics for board exams (GSEB/CBSE) and college practical examinations (BCA Sem 1, B.Tech CS).

What You'll Learn

By the end of this tutorial, you'll know:

  • The structure of two-dimensional (2D) arrays in C.
  • How 2D arrays are stored linearly in memory (Row-Major Order).
  • How to use nested loops to input and output grid data.
  • How to implement Matrix Addition and Transpose.
  • How to write the nested loop structure for Matrix Multiplication.

๐ŸŒŸ The Scenario: The Classroom Seating Chartโ€‹

Imagine a classroom with 3 rows of desks, where each row contains 4 columns.

  • If the teacher wants to call on a student, they specify: "Row 1, Desk 2".
  • This grid layout is represented below:
Column 0Column 1Column 2Column 3
Student [0][0]Student [0][1]Student [0][2]Student [0][3]
Student [1][0]Student [1][1]Student [1][2]Student [1][3]
Student [2][0]Student [2][1]Student [2][2]Student [2][3]

In C programming:

  • The entire classroom grid is a 2D Array.
  • The row indices range from 0 to 2.
  • The column indices range from 0 to 3.
  • student[2][1] represents the student sitting in Row 2, Column 1.

๐Ÿ“– Concept Explanationโ€‹

A 2D array is an array of arrays. It is mathematically viewed as a table consisting of rows and columns.

1. Declaration & Initializationโ€‹

To declare a 2D array, you specify the data type, the name, and the dimensions in two separate sets of square brackets:

int matrix[3][4]; // A matrix with 3 rows and 4 columns

You can initialize it in several ways:

// Method 1: Using nested braces (Highly readable)
int arr[2][3] = {
{1, 2, 3}, // Row 0
{4, 5, 6} // Row 1
};

// Method 2: Flattened initialization
int arr[2][3] = {1, 2, 3, 4, 5, 6}; // Automatically grouped by columns
Compiler Rule

When declaring and initializing a 2D array, the row dimension is optional, but the column dimension is MANDATORY.

int arr[][3] = {{1, 2, 3}, {4, 5, 6}}; // โœ… Valid
int arr[2][] = {{1, 2, 3}, {4, 5, 6}}; // โŒ Invalid

2. Under the Hood: Row-Major Layoutโ€‹

Although we visualize 2D arrays as grids, the computer's RAM is strictly linear. C stores 2D arrays in Row-Major Orderโ€”meaning all elements of Row 0 are stored first, followed by all elements of Row 1, and so on.


๐Ÿง  Algorithm & Step-by-Step Logicโ€‹

Matrix Multiplication (Row ร— Column)โ€‹

To multiply matrix $A$ (size $R_1 \times C_1$) by matrix $B$ (size $R_2 \times C_2$), the columns of $A$ must equal the rows of $B$ ($C_1 = R_2$). The resulting matrix $C$ will have dimensions $R_1 \times C_2$.

For each cell $C[i][j]$: C[i][j] = Sum( A[i][k] * B[k][j] ) for k = 0 to C_1 - 1

  1. Start ๐Ÿ
  2. Loop through each row $i$ of Matrix $A$ from $0$ to $R_1-1$.
  3. Loop through each column $j$ of Matrix $B$ from $0$ to $C_2-1$.
  4. Set $C[i][j] = 0$.
  5. Loop $k$ from $0$ to $C_1-1$.
  6. Calculate $C[i][j] = C[i][j] + A[i][k] \times B[k][j]$.
  7. End ๐Ÿ

๐Ÿ’ป Implementationsโ€‹

We will write two complete implementations for matrix math.

1. Matrix Addition & Transposeโ€‹

#include <stdio.h>

#define ROWS 2
#define COLS 3

int main() {
int A[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}};
int B[ROWS][COLS] = {{7, 8, 9}, {1, 2, 3}};
int sum[ROWS][COLS];
int transpose[COLS][ROWS];

// Matrix Addition using Nested Loops
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}

// Transpose of Matrix A (Rows become Columns)
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
transpose[j][i] = A[i][j];
}
}

// Output the Sum Matrix
printf("Sum of Matrix A and B:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

// Output the Transposed Matrix (size COLS x ROWS)
printf("Transpose of Matrix A:\n");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

// Output:
// Sum of Matrix A and B:
// 8 10 12
// 5 7 9
// Transpose of Matrix A:
// 1 4
// 2 5
// 3 6

2. Matrix Multiplicationโ€‹

#include <stdio.h>

#define R1 2
#define C1 3
#define R2 3
#define C2 2

int main() {
int A[R1][C1] = {{1, 2, 3}, {4, 5, 6}};
int B[R2][C2] = {{7, 8}, {9, 1}, {2, 3}};
int result[R1][C2];

// Matrix Multiplication logic using 3 Nested Loops
for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
result[i][j] = 0; // Initialize element to 0
for (int k = 0; k < C1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}

// Printing Result Matrix (size R1 x C2)
printf("Resultant Matrix:\n");
for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

// Output:
// Resultant Matrix:
// 31 19
// 85 55

๐Ÿ“Š Sample Dry Runโ€‹

Let's trace the calculation of result[0][0] in Matrix Multiplication:

  • $A[0]$ is [1, 2, 3]
  • Column 0 of $B$ is [7, 9, 2]
Stepindices $i, j, k$OperationCumulative ResultDescription
1$i=0, j=0$result[0][0] = 00Initialize target element to zero.
2$k=0$result[0][0] += A[0][0] * B[0][0] $\implies 1 \times 7$7First term added.
3$k=1$result[0][0] += A[0][1] * B[1][0] $\implies 2 \times 9$25Second term added ($7 + 18$).
4$k=2$result[0][0] += A[0][2] * B[2][0] $\implies 3 \times 2$31Third term added ($25 + 6$).

๐Ÿ“‰ Complexity Analysisโ€‹

Time Complexity โฑ๏ธโ€‹

  • Addition & Transpose: $O(R \times C)$ - Must visit every cell in the grid once.
  • Multiplication: $O(R_1 \times C_1 \times C_2)$ - Due to the three nested loops. For square matrices of size $N$, this runs in $O(N^3)$ time.

Space Complexity ๐Ÿ’พโ€‹

  • Auxiliary Space: $O(1)$ - No dynamic memory is allocated; computations are written directly to pre-allocated arrays.
  • Total Space: $O(R \times C)$ - Space required to store the matrices.

๐ŸŽจ Visual Logic & Diagramsโ€‹

Row-Major Memory Mappingโ€‹

If we declare int arr[2][3], here is how it is laid out in RAM:

Visual Grid:
+---------+---------+---------+
| [0][0] | [0][1] | [0][2] | <-- Row 0
+---------+---------+---------+
| [1][0] | [1][1] | [1][2] | <-- Row 1
+---------+---------+---------+

Linear RAM Storage Layout:
+--------+--------+--------+--------+--------+--------+
| [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] |
+--------+--------+--------+--------+--------+--------+

๐Ÿ“š Best Practices & Common Mistakesโ€‹

โœ… Best Practicesโ€‹

  • Specify Column Dimension in Functions: When passing a 2D array to a function, you must declare it with the column size.
    void process(int arr[][3], int rows); // Correct
  • Reset Cumulative Sum: Always initialize or reset the accumulator variable to 0 inside your matrix multiplication loops before calculating the inner product.

โŒ Common Mistakes โš ๏ธโ€‹

  • Swapping Row and Column Indices: Typing arr[j][i] instead of arr[i][j] when your loops are configured for i as rows and j as columns. This leads to garbage output or segmentation faults.
  • Incorrect Dimension Matching for Multiplication: Multiplying matrices without ensuring that the number of columns in the first matrix matches the number of rows in the second.
  • Missing Brackets in Initialization: Declaring int arr[2][2] = {1, 2, 3, 4}; is compiler-legal but poor style. Use nested curly braces to specify rows explicitly.

๐ŸŽฏ Practice Problemsโ€‹

Easy Level ๐ŸŸขโ€‹

  1. Write a program to display the sum of diagonal elements of a square matrix.
  2. Input a 2D array and count the number of zero elements (sparsity check).

Medium Level ๐ŸŸกโ€‹

  1. Write a function that checks if a square matrix is symmetric (equal to its transpose).
  2. Write a program to scale (multiply) every element of a matrix by a user-entered integer.

Hard Level ๐Ÿ”ดโ€‹

  1. Implement a program to find the row with the maximum sum in a 2D array.
  2. Design a program that calculates the determinant of a $3 \times 3$ matrix.

โ“ Frequently Asked Questionsโ€‹

Q: Why does C require the column size when passing a 2D array to a function?

The compiler needs the column size to calculate the memory offset. Because 2D arrays are stored linearly, to locate arr[i][j], the compiler uses the formula: Address = Base Address + (i * Number of Columns + j) * size of type If the compiler does not know the number of columns, it cannot calculate this offset.

Q: What is the difference between Row-Major and Column-Major order?

In Row-Major order (used by C/C++), elements are stored row by row. In Column-Major order (used by Fortran/MATLAB), elements are stored column by column.

Q: Can we have 3D or higher dimensional arrays in C?

Yes. C allows multi-dimensional arrays of any dimension (e.g., int cube[3][3][3]). You access them using multiple subscripts, but memory is still flattened linearly under the hood.


โœ… Summaryโ€‹

In this tutorial, you've learned:

  • โœ… 2D arrays represent tabular grid data consisting of rows and columns.
  • โœ… Elements are accessed using two subscripts: arr[row][col].
  • โœ… Memory layout in C is Row-Major Order (flattened row by row).
  • โœ… Traversal requires nested loops (outer for rows, inner for columns).
  • โœ… Functions receiving 2D arrays must specify the column dimension.

๐Ÿ’ก Interview Tips & Board Focus ๐Ÿ‘”โ€‹

  • Board Exam Alert: The matrix multiplication program is a high-yield question in both GSEB and CBSE theory papers. Practice writing the three nested loops by heart.
  • Interview Question: "Explain how the memory address of arr[2][3] is calculated in int arr[5][5]." Answer: "Using the formula: Base Address + (2 * 5 + 3) * sizeof(int)."
  • Key Terms: Row-Major, Matrix Multiplication, Matrix Transpose, Subscripts.

๐Ÿ“š Further Readingโ€‹

Continue your learning path:

Go deeper:

๐Ÿ“ Visit Us

๐Ÿซ VD Computer Tuition Surat

VD Computer Tuition
๐Ÿ“ Address
2/66 Faram Street, Rustompura
Surat โ€“ 395002, Gujarat, India
๐Ÿ“ž Phone / WhatsApp
+91 84604 41384
๐ŸŒ Website

Computer Classes & Tuition โ€” Areas We Serve in Surat

Adajanโ€ขAlthanโ€ขAmroliโ€ขAthwaโ€ขAthwalinesโ€ขBhagalโ€ขBhatarโ€ขBhestanโ€ขCanal Roadโ€ขChowkโ€ขCitylightโ€ขDumasโ€ขGaurav Pathโ€ขGhod Dod Roadโ€ขHaziraโ€ขJahangirpuraโ€ขKamrejโ€ขKapodraโ€ขKatargamโ€ขLimbayatโ€ขMagdallaโ€ขMajura Gateโ€ขMota Varachhaโ€ขNanpuraโ€ขNew Citylightโ€ขOlpadโ€ขPalโ€ขPandesaraโ€ขParle Pointโ€ขPiplodโ€ขPunaโ€ขRanderโ€ขRing Roadโ€ขRustampuraโ€ขSachinโ€ขSalabatpuraโ€ขSarthanaโ€ขSosyo Circleโ€ขUdhnaโ€ขVarachhaโ€ขVed Roadโ€ขVesuโ€ขVIP Road
๐Ÿ“ž Call Sir๐Ÿ’ฌ WhatsApp Sir