C 2D & Multi-Dimensional Arrays: Row-Major RAM Mapping & Matrix Mathematics
Welcome to Phase 7 (Chapter 15): C 2D & Multi-Dimensional Arrays, Row-Major RAM Mapping & Matrix Mathematics Masterclass! When software systems model mathematical matrices, tabular spreadsheets, graphic coordinate maps, game boards (such as Chess, Go, or Tic-Tac-Toe), or multidimensional physics tensors, single-dimensional arrays are insufficient. Multi-Dimensional Arrays provide the architectural abstraction to organize data across rows and columns. In this comprehensive guide, you will master how physical computer hardware flattens multi-dimensional grids into linear 1D Row-Major RAM memory bytes, implement core linear algebra algorithms (Matrix Addition and Matrix Transposition), and compare character arrays with null-terminated C strings.
Programmer conceptualizes a 2D array as a Grid table with Rows and Columns (e.g. $2 \times 3$ matrix). Kaani physical computer RAM is strictly a single, continuous, linear 1D sequence of byte addresses!
C language compilers organize multi-dimensional arrays in RAM using Row-Major Order: Row 0 is placed in memory first, followed immediately by Row 1, then Row 2, without any gaps.
Conceptual 2D Grid: Physical 1D Linear RAM Memory Sequence:
Row 0: [ 10 ] [ 20 ] [ 30 ] โโโโบ [ 10 ][ 20 ][ 30 ] [ 40 ][ 50 ][ 60 ]
Row 1: [ 40 ] [ 50 ] [ 60 ] โโโโโโโโโฌโโโโโโโโ โโโโโโโโโฌโโโโโโโโ
Row 0 Bytes Row 1 Bytes
RAM Address: 0x3000 0x3004 0x3008 0x300C 0x3010 0x3014
Stored Value: 10 20 30 40 50 60
๐ The Mathematical 2D Address Calculation Formula:
$$\text{Address of } matrix[i][j] = \text{Base Address} + \Big( (i \times \text{Total Columns}) + j \Big) \times \text{sizeof(element)}$$
โข i * Total Columns skips all the previous full rows in memory.
โข + j moves to the target column offset within the current row.
โข Multiplying by sizeof(element) converts the element count into exact physical RAM byte offsets!
๐ 1. Matrix Addition ($C[i][j] = A[i][j] + B[i][j]$)
Rendu matrices ni add cheyyalante, vatiki exact same dimensions $(M \times N)$ undali. Outer loop rows ni, inner loop columns ni iterate chesthu corresponding element values ni add chesthamu.
๐ 2. Matrix Transposition ($T[j][i] = M[i][j]$)
Matrix Transpose ante Rows ni Columns ga, Columns ni Rows ga convert cheyyadam. Original matrix dimension $(M \times N)$ ayithe, transposed matrix dimension $(N \times M)$ ga maruthundhi.
#include <stdio.h>
#define ROWS 2
#define COLS 3
int main(void) {
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];
// 1. Matrix Addition
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
Sum[i][j] = A[i][j] + B[i][j];
}
}
// 2. Matrix Transposition of A (2x3 -> 3x2)
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
Transpose[j][i] = A[i][j];
}
}
printf("--- Matrix Sum (A + B) ---\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) printf("%3d ", Sum[i][j]);
printf("\n");
}
printf("\n--- Transpose of Matrix A (3x2) ---\n");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) printf("%3d ", Transpose[i][j]);
printf("\n");
}
return 0;
}
| Attribute | Raw Character Array (char arr[]) | Null-Terminated C String (char str[]) |
|---|---|---|
Null Terminator ('\0') | โ NOT guaranteed unless manually placed. | โ Compulsory automatically appended at end. |
| Standard I/O Compatibility | Cannot be safely printed with %s. | Fully compatible with printf("%s") & string.h. |
| Memory Size | char ch[2] = {'A', 'B'}; $
ightarrow$ Takes 2 Bytes. | char str[3] = "AB"; $
ightarrow$ Takes 3 Bytes ('A', 'B', '\0'). |
Run this 2D identity matrix generator in our live GCC compiler:
#include <stdio.h>
int main(void) {
int n = 3;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", (i == j) ? 1 : 0);
}
printf("\n");
}
return 0;
}