BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Two-Dimensional Array (2D Array) in C: Matrix & Row-Major | Unit 4 | BKNMU BCA

by BCA School •

BKNMU Junagadh  |  BCA Sem 1  |  Unit 4



Two-Dimensional Array (2D Array) in C: Matrix Concept & Processing

Complete Guide to Tabular Data Representation, Row-Major Memory Mapping, Nested Loops, and Matrix Operations

BKNMU Junagadh BCA Sem 1 Unit 4 2D Array Matrix Processing Row-Major Order
In simple words: A Two-Dimensional Array (2D Array) organizes homogeneous elements into a tabular format composed of horizontal rows and vertical columns (commonly known as a Matrix). While a 1D array needs one subscript, a 2D array requires two subscripts: arr[row_index][col_index]. This guide covers declaration, memory mapping, nested-loop input/output, and matrix manipulation for BKNMU BCA Semester 1 (Unit 4).
📖 Core Properties of a 2D Array Structure

A two-dimensional array is defined by four core structural behaviors:

Two Subscripts

1. Row & Column Access

Elements are identified using two brackets: arr[i][j], where i denotes the row offset and j represents the column offset.

Row-Major Order

2. Memory Layout

RAM is strictly linear; C maps 2D tables linearly using Row-Major Order, placing all elements of row 0 first, followed by row 1, and so on.

Nested Loops

3. Dual-Loop Traversal

Traversal requires nested for loops: the outer loop controls row navigation, while the inner loop steps across columns.

Matrix Models

4. Tabular Real-World Use

Serves as the foundation for mathematical matrices, grade tables, coordinates, grid-based boards, and multi-subject student records.

1️⃣ Master Table: Syntax, Memory & Initialization Forms

A complete structural reference of 2D array declaration styles and initializations:

Pattern Type Syntax Pattern Memory & Data Behavior
Standard Declaration int matrix[ROWS][COLS]; Allocates ROWS × COLS × sizeof(int) contiguous bytes containing garbage values.
Grouped Initialization int a[2][2] = {{1, 2}, {3, 4}}; Row-wise grouping using nested braces. Clean and highly readable for exam solutions.
Linear Initialization int a[2][2] = {1, 2, 3, 4}; Populates elements consecutively across row 0 then row 1 automatically.
Omitted Row Size int a[][2] = {{1, 2}, {3, 4}}; Row bound can be omitted if columns are declared. Column bound is mandatory.
ℹ️ Total Elements & Total Bytes Calculation:
  • Total Elements: Total Elements = ROWS × COLS
  • Total Memory Bytes: Total Bytes = ROWS × COLS × sizeof(Data_Type)
  • Example: For int mat[3][4]; in 4-byte GCC: 3 × 4 = 12 elements, occupying 12 × 4 = 48 Bytes.
💻 Practical Code Example: Matrix Input and Tabular Display

The following program demonstrates reading values for a $3 \times 3$ matrix and displaying it in proper tabular format:

#include <stdio.h>

int main() {
    int matrix[3][3];
    int r, c;

    // 1. Reading Matrix Elements using Nested Loops
    printf("Enter elements for 3x3 matrix:\n");
    for (r = 0; r < 3; r++) {
        for (c = 0; c < 3; c++) {
            printf("Element [%d][%d]: ", r, c);
            scanf("%d", &matrix[r][c]);
        }
    }

    // 2. Displaying Elements in Tabular (Matrix) Grid
    printf("\n--- Stored 3x3 Matrix ---\n");
    for (r = 0; r < 3; r++) {
        for (c = 0; c < 3; c++) {
            printf("%d\t", matrix[r][c]);
        }
        printf("\n"); // Line break after completing each row
    }

    return 0;
}
🔄 Step-by-Step Flow: Row-Major Memory Mapping
1

Base Address Allocation

The array name matrix holds the starting address of cell [0][0]. Hardware memory allocation remains strictly contiguous and 1-dimensional.

2

Row-Major Address Calculation

To access matrix[i][j], compiler calculates address as: Address = Base_Addr + ((i × COLS) + j) × sizeof(type).

3

Nested Loop Traversal Execution

The outer loop fixes row index i, while inner loop sweeps column index j from 0 to COLS - 1, providing sequential row-wise execution.

💡 Critical Exam Rule: Mandatory Column Bound: When declaring and initializing a 2D array simultaneously, specifying the first dimension (rows) is optional, but specifying the second dimension (columns) is mandatory (e.g. int a[][3] is valid; int a[3][] is an error). The compiler requires the column size to calculate row offsets.
⚠️ Forgetting Line Break in Matrix Printing: When displaying a 2D array in matrix form, always put printf("\n"); inside the outer loop right after the inner loop finishes. Omitting it will print all matrix numbers on a single continuous line.
📝 Quick Revision — Key Exam Points
  • Definition: An array of 1D arrays; organizes data into rows and columns.
  • Subscripts: Uses two indices: arr[row][column].
  • Index Range: Rows: 0 to ROWS - 1; Columns: 0 to COLS - 1.
  • Memory Order: Stored sequentially using Row-Major Order.
  • Traversal: Uses nested for loops (outer for rows, inner for columns).