NBC 103 • Unit 316 min readHigh Exam Frequency

Two-Dimensional Arrays (2D) & Matrix Operations

Unit 3: Arrays, Pointers & String ManipulationProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Spreadsheet Grid & Apartment Floor-Room System

2D Array ek Excel spreadsheet ya multi-floor apartment building jaisa hai! Pehla index floor number (Row) batata hai aur doosra index flat number (Column). Lekin computer ki RAM 2D nahi hoti, wo ek seedhi 1D line hoti hai! Isliye C Row-Major Order use karta hai: pehle floor 0 ke saare flats RAM me aate hain, fir floor 1 ke flats, fir floor 2 ke flats!

Academic Lecture Notes & Solved Study Pages

Unit 3 • Core Concepts, Step-by-Step Proofs & Notebook Solutions

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. 2D Array Declaration & Memory Architecture

Declaration Syntax: `data_type matrix[ROWS][COLS];` (e.g. `int mat[3][4];` allocates 3 rows and 4 columns = 12 integers = 48 bytes).
Initialization:

`int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};`

Omitting Dimensions: The first dimension (rows) can be omitted, but the COLUMN dimension is STRICTLY MANDATORY so the compiler knows how many elements exist per row: `int mat[][3] = {1, 2, 3, 4, 5, 6};` is legal; `int mat[2][]` is an error!
NOTEBOOK PAGE 2 OF 3

2. Row-Major Order vs Column-Major Order

Row-Major Order (Standard in C, C++, Python):

Elements are stored row by row. Row 0 elements are laid down first, then Row 1, etc.

Memory Address Formula:

$\text{Address}(A[i][j]) = \text{Base Address} + (i \times \text{COLS} + j) \times \text{sizeof(element)}$

Column-Major Order (Standard in FORTRAN, MATLAB, R):

Elements are stored column by column.

Memory Address Formula:

$\text{Address}(A[i][j]) = \text{Base Address} + (j \times \text{ROWS} + i) \times \text{sizeof(element)}$

NOTEBOOK PAGE 3 OF 3

3. Matrix Multiplication Algorithm & Conditions

Mathematical Precondition:

Two matrices A ($R_1 \times C_1$) and B ($R_2 \times C_2$) can be multiplied if and only if $C_1 == R_2$ (Columns of Matrix A equal Rows of Matrix B).

Resulting Matrix C will have dimensions: $R_1 \times C_2$.
Formula: $C[i][j] = \sum_{k=0}^{C_1 - 1} (A[i][k] \times B[k][j])$
Time Complexity: 3 nested loops results in $O(R_1 \times C_2 \times C_1) = O(N^3)$ computational complexity.
Row-Major Linear RAM Layout for 2x3 Matrix: mat[2][3]
ElementRow (i)Column (j)Linear Index (i*COLS + j)Physical Address (Base = 0x5000)
mat[0][0]000*3 + 0 = 00x5000
mat[0][1]010*3 + 1 = 10x5004
mat[0][2]020*3 + 2 = 20x5008
mat[1][0]101*3 + 0 = 30x500C
mat[1][1]111*3 + 1 = 40x5010
mat[1][2]121*3 + 2 = 50x5014

Interactive Tested Code Example

#include <stdio.h>

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

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

    // Matrix Multiplication Algorithm
    for (int i = 0; i < R1; i++) {
        for (int j = 0; j < C2; j++) {
            for (int k = 0; k < C1; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }

    printf("=== MATRIX MULTIPLICATION RESULT (2x2) ===\n");
    for (int i = 0; i < R1; i++) {
        printf("| ");
        for (int j = 0; j < C2; j++) {
            printf("%-4d ", C[i][j]);
        }
        printf("|\n");
    }
    return 0;
}
💡 Note:Computes matrix product of two 2x2 matrices using triple nested loops. Multiplies row elements of A by column elements of B and accumulates sum in C[i][j].

🎯 University Exam Scoring Blueprint

  • In university exams, always derive the Row-Major formula: Address(A[i][j]) = Base + (i * COLS + j) * size.
  • Remember that when passing a 2D array to a function, the column size MUST be specified in formal parameters: void func(int arr[][3]).
  • State the matrix multiplication condition: Columns of First = Rows of Second.

Top Viva Questions on Two-Dimensional Arrays (2D) & Matrix Operations

2 Questions
1

Why must the column size be specified when declaring 2D arrays as function parameters: void func(int arr[][3])?

2

What is the Transpose of a matrix?