Chapter 2: Arrays and Structures
Introduction
Learning Outcomes
Define the array ADT and explain the contiguous memory layout of a C array.
Find the address of an element of a 2-, 3- or n-dimensional array in row- or column-major order.
Store triangular and band matrices in a one-dimensional array with O(1) access.
Build growable arrays and 2D arrays with malloc/realloc/free; explain the amortized cost.
Use structs, unions and strings (char arrays) correctly and safely.
Count the steps of matrix addition and multiplication and show the O(n³) result.
Array ADT · Horowitz 2.1
An array: a set of ⟨index, value⟩ pairs
Abstractly, an array is a mapping: each defined index is associated with a value. “Consecutive memory cells” is the most common implementation of this ADT, not its definition.
| Operation | Meaning |
|---|---|
Create(j, list) | an empty array of j dimensions with index set list |
Retrieve(A, i) | return A[i] if i is a valid index, else an error |
Store(A, i, x) | if i is valid, return a new array where i maps to x |
Given the base address α and element size s, A[i] is at α + i·s: one multiply and one add, O(1).
The size is fixed up front; inserting or deleting in the middle shifts the elements after it: O(n).
Array ADT · C implementation
Declaring arrays in C, and addresses
int list[5], *plist[5]; /* list : 5 ints */ /* plist: 5 int pointers */ list[i] /* address: α + i * sizeof(int) */ sizeof list /* 5 * sizeof(int) bytes */
The compiler keeps the address of list[0] (α); the address of list[i] is computed at run time. Indices are not checked: writing list[5] is undefined behavior.
Addresses assuming sizeof(int) = 4
Array ADT · Arrays and pointers
Array name ≈ address of the first element
| Expression | Equivalent |
|---|---|
list | &list[0] |
list + i | &list[i] (i·sizeof(int) bytes further) |
*(list + i) | list[i] |
p = list; p[i] | the same cell as list[i] |
Pointer arithmetic counts elements, not bytes: p + 1 points to the next int.
list = p; does not compile; an array name is a constant address, not a variable.
For a local array, sizeof list gives the whole array; for a parameter, the pointer size.
void f(int a[]) ≡ void f(int *a): the array is not copied, its address is passed; the size goes in a separate parameter.
Array ADT · Run the code step by step
Pointer arithmetic: *(p + i)
arr is allocated on the stack as 4 consecutive ints; call the address of the first one α.
The call does not copy the array: only the value &arr[0] (α) is passed to p.
0 < 4 → keep looping. p + 0 = α + 0·4 = α+0.
*(p + 0) reads the value at that address: 10 (≡ p[0]).
1 < 4 → keep looping. p + 1 = α + 1·4 = α+4.
*(p + 1) reads the value at that address: 20 (≡ p[1]).
2 < 4 → keep looping. p + 2 = α + 2·4 = α+8.
*(p + 2) reads the value at that address: 30 (≡ p[2]).
3 < 4 → keep looping. p + 3 = α + 3·4 = α+12.
*(p + 3) reads the value at that address: 40 (≡ p[3]).
4 < 4 is false → the loop ends; print_array returns.
The address grows by 4 bytes per line. (Real %p output is a hexadecimal address; shown here as α.)
void print_array(int *p, int n) { int i; for (i = 0; i < n; i++) printf("%p %d\n", (void *)(p + i), *(p + i)); } int main(void) { int arr[] = {10, 20, 30, 40}; print_array(arr, 4); return 0; }
Dynamic Arrays · malloc / free
An array whose size is known only at run time
int n, i, *a; scanf("%d", &n); a = malloc(n * sizeof *a); /* n ints */ if (a == NULL) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } for (i = 0; i < n; i++) a[i] = i * i; /* like an array */ free(a); /* give it back */ a = NULL; /* no dangling pointer */
A local array disappears when the function returns; a malloc block lives until it is freed.
sizeof *a idiomGives the right size even if the type changes; avoids mixing it up with sizeof(int*).
calloc(n, s)Allocates a zeroed block; realloc grows or shrinks a block.
Dynamic Arrays · Run the code step by step
A growing array: double the capacity
An empty dynamic array: {NULL, 0, 0}. We append 7, 3, 9, 4, 6 in order.
0 == 0 → full, must grow.
newcap = 1. realloc returns a 1-cell block; the 0 old elements are copied (realloc from NULL = malloc). The result goes into t first, so the old block survives a failure.
t != NULL: d->data = t, cap = 1.
data[0] = 7, then size++ → 1. (Postfix: use first, then increment.)
1 == 1 → full, must grow.
newcap = 2. realloc returns a 2-cell block; 1 old element is copied (1 copy so far). The result goes into t first, so the old block survives a failure.
t != NULL: d->data = t, cap = 2.
data[1] = 3, then size++ → 2. (Postfix: use first, then increment.)
2 == 2 → full, must grow.
newcap = 4. realloc returns a 4-cell block; 2 old elements are copied (3 copies so far). The result goes into t first, so the old block survives a failure.
t != NULL: d->data = t, cap = 4.
data[2] = 9, then size++ → 3. (Postfix: use first, then increment.)
3 == 4 is false → there is room; store directly.
data[3] = 4, then size++ → 4. (Postfix: use first, then increment.)
4 == 4 → full, must grow.
newcap = 8. realloc returns an 8-cell block; 4 old elements are copied (7 copies so far). The result goes into t first, so the old block survives a failure.
t != NULL: d->data = t, cap = 8.
data[4] = 6, then size++ → 5. Five appends cost only 7 copies in total.
typedef struct { int *data; int size, cap; } DynArray; int append(DynArray *d, int x) { if (d->size == d->cap) { /* full */ int newcap = d->cap ? 2 * d->cap : 1; int *t = realloc(d->data, newcap * sizeof *t); if (t == NULL) return 0; /* old block intact */ d->data = t; d->cap = newcap; } d->data[d->size++] = x; return 1; }
Dynamic Arrays · Analysis
Doubling: amortized O(1) per append
| n appends | doubling: copies | copies / n | grow by 1: copies | copies / n |
|---|---|---|---|---|
| 8 | 7 | 0.88 | 28 | 3.5 |
| 64 | 63 | 0.98 | 2016 | 31.5 |
| 1024 | 1023 | 1.00 | 523776 | 511.5 |
With doubling the copies are 1 + 2 + 4 + … + 2k < 2n → O(n) in total (total cost < 3n including the n writes), so O(1) per append (amortized). Growing by 1 (or any constant c) each time costs n(n−1)/2 = O(n²) copies.
The append that triggers growth is expensive; amortized analysis guarantees the average over a long sequence.
Capacity is at most 2·size; the price is at most 50% empty cells.
realloc trapIf a = realloc(a, …) fails, it returns NULL and the old block is lost (a leak). Assign to a temporary first.
Dynamic Arrays · Run the code step by step
A dynamic 2D array: an array of pointers + rows
First allocate an array of pointers with 3 cells (sizeof *x = sizeof(int *)); its contents are still indeterminate.
x != NULL: allocation succeeded, go on.
i = 0 < 3 → allocate row 0.
x[0] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.
i = 1 < 3 → allocate row 1.
x[1] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.
i = 2 < 3 → allocate row 2.
x[2] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.
3 < 3 is false → the loop ends.
Return x; x[i][j] ≡ *(*(x + i) + j). The rows need not be contiguous. To release it, free each x[i] first, then x.
int **make2d(int rows, int cols) { int i, **x = malloc(rows * sizeof *x); if (x == NULL) return NULL; for (i = 0; i < rows; i++) { x[i] = malloc(cols * sizeof **x); if (x[i] == NULL) { /* undo */ while (--i >= 0) free(x[i]); free(x); return NULL; } } return x; }
Multidimensional Arrays
Memory is one-dimensional, arrays are not
An n-dimensional array declared as A[upper0][upper1]…[uppern−1] has ∏ upperi elements. E.g. A[10][10][10] → 1000 elements.
Memory, however, is a one-dimensional array of bytes; the compiler must lay the elements out in some order:
- Row major: all of row 0 first, then row 1… (the default in C, C++, Python/NumPy)
- Column major: column 0 first… (Fortran, MATLAB)
int x[3][4]; is really an array of 3 elements, each an array of 4; x[1] points to the start of row 1.
Multidimensional Arrays
int x[3][4] can be laid out in two ways
Row major: before x[i][j] come i full rows (i·upper1 elements) and j elements → &x[i][j] = α + (i·upper1 + j)·s. C uses this.
Column major: first j full columns (j·upper0 elements), then i elements → &x[i][j] = α + (j·upper0 + i)·s. The same element lands elsewhere: x12 → 6 in row major, 7 in column major.
The formula α + i·upper1 + j counts elements (cells); multiply by s = sizeof(element) for a byte address.
Multidimensional Arrays · Run the code step by step
Computing a row-major address
We want the address of x[1][2]: α = 100, row length upper1 = 4, s = 4 bytes.
Before it: 1 full row (1·4 = 4 elements) and 2 elements in its row → k = 6 (blue cells).
Return 100 + 6·4 = 124.
printf prints 124. In column-major order k = j·3 + i = 7 → 128.
We want the address of x[2][3]: α = 100, row length upper1 = 4, s = 4 bytes.
Before it: 2 full rows (2·4 = 8 elements) and 3 elements in its row → k = 11 (blue cells).
Return 100 + 11·4 = 144.
printf prints 144. The last element: 100 + 11·4 = 144.
/* int x[3][4]: row-major byte address */ long address(long alpha, int i, int j, int u1, int s) { long k = (long)i * u1 + j; /* elements before */ return alpha + k * s; } int main(void) { printf("%ld\n", address(100, 1, 2, 4, 4)); printf("%ld\n", address(100, 2, 3, 4, 4)); return 0; }
Multidimensional Arrays
G[2][2][3]: plane · row · column
| element | address (in elements) |
|---|---|
[0][0][0] | α |
[i][0][0] | α + i·u1·u2 |
[i][j][0] | α + i·u1·u2 + j·u2 |
[i][j][k] | α + i·u1·u2 + j·u2 + k |
The last expression has 3 multiplications + 3 additions. Multiplication costs more than addition, so the compiler factors it:
α + (i·u1 + j)·u2 + k
→ 2 multiplications + 3 additions. Example: [1][0][2] → (1·2 + 0)·3 + 2 = 8.
Multidimensional Arrays
The address formula for n dimensions
For A[u0][u1]…[un−1]:
| element | address |
|---|---|
| A[0][0]…[0] | α |
| A[i0][0]…[0] | α + i0·u1u2…un−1 |
| A[i0][i1]…[0] | … + i1·u2…un−1 |
| A[i0]…[in−1] | … + in−1 |
&A[i0]…[in−1] = α + Σj=0n−1 ij·aj
aj = ∏k=j+1n−1 uk (0 ≤ j < n−1), an−1 = 1
Multidimensional Arrays · Run the code step by step
Relative position in n dimensions: Horner’s method
We look for G[1][0][2]. t = idx[0] = 1: plane number 1.
j = 1 < 3 → one dimension further in.
t = 1·2 + 0 = 2: the “global row” number counted across planes (plane 1, row 0).
j = 2 < 3 → one dimension further in.
t = 2·3 + 2 = 8: 8 elements come before → cell 8.
3 < 3 is false → the loop ends.
Return 8. 2 multiplications in total (n−1). Check: 1·2·3 + 0·3 + 2 = 8. dim[0] was never used!
/* idx: indices, dim: sizes, n: dimensions */ int offset(const int idx[], const int dim[], int n) { int j, t = idx[0]; for (j = 1; j < n; j++) t = t * dim[j] + idx[j]; /* Horner */ return t; /* address = alpha + t*s */ }
Triangular and Band Matrices
Keeping half a matrix in a one-dimensional array
The diagonal splits a square matrix in two; sometimes one half is enough (distances between cities, atomic weights in the periodic table…). The data lives in a one-dimensional array LOW; the user gets an interface that behaves like a two-dimensional matrix.
Row i holds i+1 elements → n(n+1)/2 in total. For aij, count the elements of earlier rows plus its place in the row:
k = i(i+1)/2 + j (j ≤ i)
Triangular and Band Matrices · Run the code step by step
Reading a lower triangle: row start k
4·5/2 = 10 ≤ 100: it fits in LOW. Input: 3 1 4 1 5 9 2 6 5 3.
k = 1·0/2 = 0: 0 elements stored before row 0.
low[0+0] = low[0] ← 3 (a00).
k = 2·1/2 = 1: 1 element stored before row 1.
low[1+0] = low[1] ← 1 (a10).
low[1+1] = low[2] ← 4 (a11).
k = 3·2/2 = 3: 3 elements stored before row 2.
low[3+0] = low[3] ← 1 (a20).
low[3+1] = low[4] ← 5 (a21).
low[3+2] = low[5] ← 9 (a22).
k = 4·3/2 = 6: 6 elements stored before row 3.
low[6+0] = low[6] ← 2 (a30).
low[6+1] = low[7] ← 6 (a31).
low[6+2] = low[8] ← 5 (a32).
low[6+3] = low[9] ← 3 (a33).
10 values read; the 6 zeros of the upper triangle were never stored.
#define MAX_SIZE 100 void read_lower(int low[], int n) { int i, j, k; if (n * (n + 1) / 2 > MAX_SIZE) { printf("Array too small!\n"); exit(1); } for (i = 0; i < n; i++) { k = (i + 1) * i / 2; /* before row i */ for (j = 0; j <= i; j++) /* j <= i */ scanf("%d", &low[k + j]); } }
Triangular and Band Matrices · Run the code step by step
Access: an index or an error code?
We ask for a21.
Bounds check: the indices are in 0…3.
2 < 1? No → in the lower triangle.
k = 3·2/2 + 1 = 4 → LOW[4] = a21.
printf prints 4.
We ask for a13.
Bounds check: the indices are in 0…3.
1 < 3? Yes → upper triangle (the zero region): −1.
printf prints -1. The error codes {−1, −2} and the valid results {0, 1, 2, …} never overlap, so the caller can tell them apart.
We ask for a40.
Bounds check: i = 4 ≥ n → invalid!
Print an error message and return −2.
printf prints -2.
int lower_index(int i, int j, int n) { if (i < 0 || i >= n || j < 0 || j >= n) { printf("Invalid index\n"); return -2; } else if (i < j) return -1; /* upper: 0 */ else return (i + 1) * i / 2 + j; /* valid */ } /* main: */ printf("%d\n", lower_index(2, 1, 4)); printf("%d\n", lower_index(1, 3, 4)); printf("%d\n", lower_index(4, 0, 4));
Triangular and Band Matrices · EXERCISE
EXERCISE solution: four kinds of triangles and symmetric matrices
| stored region | elements in row i | k (row major) | |
|---|---|---|---|
| j ≤ i (lower left) | i + 1 | i(i+1)/2 + j | |
| j ≥ i (upper right) | n − i | i·n − i(i−1)/2 + (j − i) | |
| i + j ≤ n−1 (upper left) | n − i | i·n − i(i−1)/2 + j | |
| i + j ≥ n−1 (lower right) | i + 1 | i(i+1)/2 + j − (n−1−i) |
k = j(j+1)/2 + i. The formulas were verified by a script for n = 1…8.Triangular and Band Matrices
Band matrices: store only the diagonal band
Elements outside the band are unused or all equal to a constant c. n: side (4), a: number of lower bands including the diagonal (3), b: number of upper bands including the diagonal (2).
elements = n(a+b−1) − a(a−1)/2 − b(b−1)/2 = 12
Triangular and Band Matrices
Band number j − i and the START array
- j − i is constant along a band: for d20 and d31, 0−2 = 1−3 = −2. The main diagonal is 0; negatives are lower bands, positives upper.
- Valid bands lie in the closed range [1 − a, b − 1]; outside is the constant region.
- START holds the position in
band[]of each band’s first element: a + b − 1 entries. C has no negative indices, so we shift by band number + a − 1. - Position within the band: j in a lower band, i in an upper band (each band starts in row 0 or column 0).
Triangular and Band Matrices · Run the code step by step
Building a band matrix (lower bands)
4·4 − 3 − 1 = 12 ≤ MAX_SIZE. Input arrives band by band: 6 10 · 3 7 11 · 1 4 8 12 · 2 5 9.
Band i = -2 (lower): 2 elements.
start[-2+2] = start[0] = 0: the band begins at band[0].
band[0+0] ← 6 (d20).
band[0+1] ← 10 (d31).
total += 2 → 2: where the next band begins.
Band i = -1 (lower): 3 elements.
start[-1+2] = start[1] = 2: the band begins at band[2].
band[2+0] ← 3 (d10).
band[2+1] ← 7 (d21).
band[2+2] ← 11 (d32).
total += 3 → 5: where the next band begins.
void build_band(int band[], int start[], int n, int a, int b) { int i, k, total = 0; /* earlier bands */ if (n*(a+b-1) - a*(a-1)/2 - b*(b-1)/2 > MAX_SIZE) { printf("Out of memory!\n"); exit(-1); } for (i = 1 - a; i <= b - 1; i++) { /* band by band */ start[i + a - 1] = total; /* band start */ for (k = 0; k <= n - abs(i) - 1; k++) scanf("%d", &band[start[i + a - 1] + k]); total += n - abs(i); } }
Triangular and Band Matrices · Run the code step by step
Building a band matrix (continued: diagonal and upper)
Band i = 0 (main diagonal): 4 elements.
start[0+2] = start[2] = 5: the band begins at band[5].
band[5+0] ← 1 (d00).
band[5+1] ← 4 (d11).
band[5+2] ← 8 (d22).
band[5+3] ← 12 (d33).
total += 4 → 9: where the next band begins.
Band i = 1 (upper): 3 elements.
start[1+2] = start[3] = 9: the band begins at band[9].
band[9+0] ← 2 (d01).
band[9+1] ← 5 (d12).
band[9+2] ← 9 (d23).
total += 3 → 12: where the next band begins.
void build_band(int band[], int start[], int n, int a, int b) { int i, k, total = 0; /* earlier bands */ if (n*(a+b-1) - a*(a-1)/2 - b*(b-1)/2 > MAX_SIZE) { printf("Out of memory!\n"); exit(-1); } for (i = 1 - a; i <= b - 1; i++) { /* band by band */ start[i + a - 1] = total; /* band start */ for (k = 0; k <= n - abs(i) - 1; k++) scanf("%d", &band[start[i + a - 1] + k]); total += n - abs(i); } }
Triangular and Band Matrices · Run the code step by step
O(1) access in a band matrix
We ask for d31; the indices are valid. Band number j − i = -2.
i − j = 2 < a = 3? Yes → a lower band.
start[-2+2] + j = 0 + 1 = 1 → band[1] = 10.
We ask for d12; the indices are valid. Band number j − i = 1.
1 ≥ 1 → upper triangle.
1 < b = 2 → a valid upper band: start[1+2] + i = 9 + 1 = 10.
We ask for d03; the indices are valid. Band number j − i = 3.
3 ≥ 1 → upper triangle.
3 ≥ b = 2 → outside the band, upper constant region: −1 (value c).
We ask for d30; the indices are valid. Band number j − i = -3.
i − j = 3 < a = 3? No → outside the band.
Lower constant region: −2. A constant number of operations in every case → O(1).
int band_index(int i, int j, int n, int a, int b, int start[]) { if (i < 0 || j < 0 || i >= n || j >= n) { printf("Index out of range\n"); return -3; } else if (j - i >= 1) { /* upper triangle */ if (j - i < b) /* valid band */ return start[j - i + a - 1] + i; else return -1; /* upper constant */ } else if (i - j < a) /* diagonal/lower */ return start[j - i + a - 1] + j; else return -2; /* lower constant */ }
Triangular and Band Matrices
A START array or a formula? (HOMEWORK 1)
band_index has no loop that depends on the input → O(1). Counting band starts with a loop would raise the complexity; START avoids that but costs memory and makes the program “less dynamic”.
HOMEWORK 1: Compute the band start without the START array. (A sample solution is on the right.)
/* t: band number (1-a..b-1); same as start[t+a-1] */ int band_start(int t, int n, int a) { if (t <= 0) /* lower bands */ return (t + a - 1) * n - (a * (a - 1) + t * (1 - t)) / 2; return (a - 1) * n - a * (a - 1) / 2 /* start of t=0 */ + t * n - t * (t - 1) / 2; }
n = 4, a = 3: t = −2, −1, 0, 1 → 0, 2, 5, 9 (same as START). A script tried every a, b for n ≤ 11. Access: band_start(j−i, n, a) + (j > i ? i : j).
Triangular and Band Matrices
Is if (expr1 && expr2) safe?
A common belief: “We can’t know whether the compiler scans right to left or left to right, so the second of these two equivalent forms is safer.”
/* 1 */ if (i < n && a[i] != 0) ... /* 2 */ if (i < n) if (a[i] != 0) ...
&& and || (C99 §6.5.13–14): if the left side is false, the right side is never evaluated. Both forms are equally safe; form 1 is a common and correct idiom.& (bitwise AND) does not short-circuit, and the order of evaluation of operands such as in f(x) + g(x) is unspecified.
Structures · Horowitz 2.2
struct: fields of different types in one record
typedef struct { char name[10]; int age; float salary; } human; human h1, h2; strcpy(h1.name, "Ali"); h1.age = 21; h1.salary = 3500.0f; h2 = h1; /* copied field by field */ /* if (h1 == h2) -> DOES NOT COMPILE */
h1.age is a field of a variable; p->age ≡ (*p).age (p: human *).
Structures can be assigned as a whole but not compared; write a function that compares field by field.
typedef struct {…} human; saves us from writing struct every time.
Structures · Horowitz 2.2
Structures within structures · self-referential structures
typedef struct { int day, month, year; } date; typedef struct { char name[10]; date dob; /* nested struct */ } person; /* p.dob.year = 2004; */ typedef struct list { char data; struct list *link; /* self-reference */ } list;
Self-referential structures are the basis of linked lists.
Structures · Horowitz 2.2
union: fields share the same memory
typedef struct { enum { FEMALE, MALE } sex; /* tag */ union { int children; /* if FEMALE */ int beard; /* if MALE */ } u; } sex_type; sex_type s; s.sex = MALE; s.u.beard = 0; /* read only the field the tag allows */
A union is as large as its largest field (+ alignment). Tracking which field is valid with a tag field is the programmer’s job.
Field order changes the memory layout (compiler-dependent; typical 32/64-bit values)
Structures · Run the code step by step
Walking through an array of structures
An array of structures sits in memory record after record; each record is sizeof(Student) bytes.
0 < 3 → record 0.
Add s[0].gpa = 3.2 → sum = 3.2.
1 < 3 → record 1.
Add s[1].gpa = 2.6 → sum = 5.8.
2 < 3 → record 2.
Add s[2].gpa = 3.5 → sum = 9.3.
3 < 3 is false → the loop ends.
n > 0 → return 9.3 / 3 = 3.10. For n = 0 it returns 0.0 instead of dividing by zero.
typedef struct { int id; char name[16]; double gpa; } Student; double class_avg(const Student s[], int n) { double sum = 0.0; int i; for (i = 0; i < n; i++) sum += s[i].gpa; /* field via . */ return n > 0 ? sum / n : 0.0; /* empty class */ }
const Student s[]: the array is not copied; const documents that the function will not modify the records.
Strings · Horowitz 2.6 · <string.h>
Strings in C: a char array ending in '\0'
char s[] = "dog"; /* 4 bytes: d o g \0 */ char t[10]; char *p = "dog"; /* read-only literal */ strlen(s) /* 3 : up to \0 */ sizeof s /* 4 : size of the array */ strcpy(t, s); /* is t big enough? */ snprintf(t, sizeof t, "%s!", s); /* safe */ if (strcmp(s, "dog") == 0) ... /* not ==! */
| function | task |
|---|---|
strlen | length (excluding \0) |
strcpy / strncpy | copy |
strcat | append |
strcmp | compare lexicographically |
strchr / strstr | find a char / substring |
Strings · Run the code step by step
How does strlen work?
p points to the same place as s (the first character).
*p = 'd' ≠ '\0' → continue.
p++: one character (1 byte) forward.
*p = 'a' ≠ '\0' → continue.
p++: one character (1 byte) forward.
*p = 't' ≠ '\0' → continue.
p++: one character (1 byte) forward.
*p = 'a' ≠ '\0' → continue.
p++: one character (1 byte) forward.
*p = '\0' → the loop ends.
Return p − s = 4. Finding the length scans the whole string: O(n) — calling strlen in a loop condition makes it O(n²)!
size_t length(const char *s) { const char *p = s; while (*p != '\0') /* up to the terminator */ p++; return (size_t)(p - s); /* pointer difference */ } /* length("data") */
Matrix Operations · Run the code step by step
Matrix multiplication: three nested loops
c[0][0] uses row 0 of a and column 0 of b; start at 0.
k = 0: += a[0][0]·b[0][0] = 1·5 → 5.
k = 1: += a[0][1]·b[1][0] = 2·7 → 19.
c[0][1] uses row 0 of a and column 1 of b; start at 0.
k = 0: += a[0][0]·b[0][1] = 1·6 → 6.
k = 1: += a[0][1]·b[1][1] = 2·8 → 22.
c[1][0] uses row 1 of a and column 0 of b; start at 0.
k = 0: += a[1][0]·b[0][0] = 3·5 → 15.
k = 1: += a[1][1]·b[1][0] = 4·7 → 43.
c[1][1] uses row 1 of a and column 1 of b; start at 0.
k = 0: += a[1][0]·b[0][1] = 3·6 → 18.
k = 1: += a[1][1]·b[1][1] = 4·8 → 50.
Done: [[19, 22], [43, 50]]. The innermost statement ran N³ = 8 times → O(n³).
#define N 2 void mult(int a[][N], int b[][N], int c[][N]) { int i, j, k; for (i = 0; i < N; i++) for (j = 0; j < N; j++) { c[i][j] = 0; for (k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j]; } }
Matrix Operations · homework
Homework: matrix multiplication and transpose
- Build the step count table for
mult(n×n); how many multiplications are performed? - What is the complexity of multiplying an m×n matrix by an n×p matrix? How many cells does the result have?
- Write a function that transposes an n×n matrix without an extra array (in place).
Solutions
- for i: n+1 · for j: n(n+1) · c=0: n² · for k: n²(n+1) · +=: n³ → 2n³ + 3n² + 2n + 1 = O(n³); n³ multiplications. (Verified by a script for n = 1…9.)
- n products per c[i][j]: O(m·n·p); m·p cells.
for (i = 0; i < n; i++) for (j = i + 1; j < n; j++) { t = a[i][j]; a[i][j] = a[j][i]; a[j][i] = t; }— only the upper triangle is visited (starting at j = 0 would swap every pair twice and leave the matrix unchanged). n(n−1)/2 swaps → O(n²).
Lab · C code step by step
Lab: Dynamic Array
Wrap-Up
Complexity Summary
| Operation | Time | Extra space |
|---|---|---|
| Access A[i]; n-dimensional address (n−1 multiplications) | O(1) | O(1) |
| Append to a dynamic array (doubling) | amortized O(1), worst O(n) | ≤ 2·size |
| Insert / delete in the middle | O(n) | O(1) |
| Lower triangular / band matrix access | O(1) | n(n+1)/2 / band elements |
| build_band, read_lower | O(stored elements) | START: a+b−1 |
| strlen / length | O(n) | O(1) |
| Matrix addition (m×n) | O(mn) | O(1) |
| Matrix multiplication (n×n) | O(n³) | O(1) |
Wrap-Up
Common Mistakes
a[n]); C does not check.a = realloc(a, …): losing the old block on failure.sizeof(int *) and sizeof(int); using sizeof a on a parameter.== on strings, or leaving no room for '\0'.Wrap-Up
Exercises
double d[5][8]row-major, α = 2000:&d[3][5]? Column-major?int A[4][5][6]: relative position of A[2][3][4] by Horner’s method?- 5×5 lower triangular matrix: where in LOW is a43?
- Band matrix n = 6, a = 2, b = 3: how many elements? START array?
- A dynamic array starts at capacity 1 and doubles. Total copies after 100 appends?
- How can reordering the fields shrink
struct { char a; double b; char c; }?
Answers
- Row: 2000 + (3·8 + 5)·8 = 2232. Column: 2000 + (5·5 + 3)·8 = 2224.
- (2·5 + 3)·6 + 4 = 82.
- k = 4·5/2 + 3 = 13.
- 6·4 − 1 − 3 = 20; bands −1, 0, 1, 2 → lengths 5, 6, 5, 4 → START = [0, 5, 11, 16].
- 1 + 2 + 4 + … + 64 = 127 (< 2·100).
- double first:
{ double b; char a, c; }→ typically 16 bytes (not 24).
Wrap-Up
Chapter Summary
ADT: ⟨index, value⟩; the C implementation is contiguous memory with address α + i·s.
Row major: α + (i·u1 + j)·s; in n dimensions, n−1 multiplications with Horner.
Triangle: n(n+1)/2 elements, k = i(i+1)/2 + j. Band: bands numbered j − i + START.
malloc/realloc/free; growth by doubling is amortized O(1); a 2D array = an array of pointers.
A struct groups fields, a union shares them; a string is a char array ending in '\0'.
Addition O(mn), multiplication O(n³).
Wrap-Up
References and Next Steps
- E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., Chapter 2 (arrays, structures, multidimensional arrays, strings) and 1.4.
- B. Kernighan, D. Ritchie, The C Programming Language, 2nd ed., Chapters 5–6 (pointers, arrays, structures).
- T. Cormen et al., Introduction to Algorithms, 17.4 (dynamic tables and amortized analysis).