Two-Dimensional Array (2D Array) in C: Matrix & Row-Major | Unit 4 | BKNMU BCA
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
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).
A two-dimensional array is defined by four core structural behaviors:
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.
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.
3. Dual-Loop Traversal
Traversal requires nested for loops: the outer loop controls row navigation, while the inner loop steps across columns.
4. Tabular Real-World Use
Serves as the foundation for mathematical matrices, grade tables, coordinates, grid-based boards, and multi-subject student records.
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 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, occupying12 × 4 = 48 Bytes.
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;
}
Base Address Allocation
The array name matrix holds the starting address of cell [0][0]. Hardware memory allocation remains strictly contiguous and 1-dimensional.
Row-Major Address Calculation
To access matrix[i][j], compiler calculates address as: Address = Base_Addr + ((i × COLS) + j) × sizeof(type).
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.
int a[][3] is valid; int a[3][] is an error). The compiler requires the column size to calculate row offsets.
printf("\n"); inside the outer loop right after the inner loop finishes. Omitting it will print all matrix numbers on a single continuous line.
- Definition: An array of 1D arrays; organizes data into rows and columns.
- Subscripts: Uses two indices:
arr[row][column]. - Index Range: Rows:
0toROWS - 1; Columns:0toCOLS - 1. - Memory Order: Stored sequentially using Row-Major Order.
- Traversal: Uses nested
forloops (outer for rows, inner for columns).