Chapter 2: Arrays and Structures

Data Structures and Algorithms with C
The array ADTArrays and pointersDynamic arraysMultidimensional arrays · addressingTriangular and band matricesstruct · unionStringsMatrix multiplication
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Arrays, abstract and concrete

Define the array ADT and explain the contiguous memory layout of a C array.

Computing addresses

Find the address of an element of a 2-, 3- or n-dimensional array in row- or column-major order.

Saving memory

Store triangular and band matrices in a one-dimensional array with O(1) access.

Dynamic memory

Build growable arrays and 2D arrays with malloc/realloc/free; explain the amortized cost.

Record types

Use structs, unions and strings (char arrays) correctly and safely.

Matrix operations

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.

OperationMeaning
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
Why is it fast?

Given the base address α and element size s, A[i] is at α + i·s: one multiply and one add, O(1).

The price

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.

[0]10[1]20[2]30[3]40αα+4α+8α+12

Addresses assuming sizeof(int) = 4

Indices start at 0: the valid indices of an n-element array are 0 … n−1.

Array ADT · Arrays and pointers

Array name ≈ address of the first element

ExpressionEquivalent
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.

Difference 1: no assignment

list = p; does not compile; an array name is a constant address, not a variable.

Difference 2: sizeof

For a local array, sizeof list gives the whole array; for a parameter, the pointer size.

Parameters

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)

[0]10[1]20[2]30[3]40αα+4α+8α+12

arr is allocated on the stack as 4 consecutive ints; call the address of the first one α.

[0]10[1]20[2]30[3]40αα+4α+8α+12p

The call does not copy the array: only the value &arr[0] (α) is passed to p.

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 0

0 < 4 → keep looping. p + 0 = α + 0·4 = α+0.

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 0

*(p + 0) reads the value at that address: 10 (≡ p[0]).

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 1

1 < 4 → keep looping. p + 1 = α + 1·4 = α+4.

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 1

*(p + 1) reads the value at that address: 20 (≡ p[1]).

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 2

2 < 4 → keep looping. p + 2 = α + 2·4 = α+8.

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 2

*(p + 2) reads the value at that address: 30 (≡ p[2]).

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 3

3 < 4 → keep looping. p + 3 = α + 3·4 = α+12.

[0]10[1]20[2]30[3]40αα+4α+8α+12pp + 3

*(p + 3) reads the value at that address: 40 (≡ p[3]).

[0]10[1]20[2]30[3]40αα+4α+8α+12p

4 < 4 is false → the loop ends; print_array returns.

[0]10[1]20[2]30[3]40αα+4α+8α+12

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 */
Stack vs. heap memory

A local array disappears when the function returns; a malloc block lives until it is freed.

The sizeof *a idiom

Gives 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

dataNULLsize = 0 cap = 0

An empty dynamic array: {NULL, 0, 0}. We append 7, 3, 9, 4, 6 in order.

dataNULLsize = 0 cap = 0

0 == 0 → full, must grow.

data0size = 0 cap = 1+0 copies

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.

data0size = 0 cap = 1

t != NULL: d->data = t, cap = 1.

data07size = 1 cap = 1

data[0] = 7, then size++ → 1. (Postfix: use first, then increment.)

data07size = 1 cap = 1

1 == 1 → full, must grow.

data071size = 1 cap = 2+1 copy

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.

data071size = 1 cap = 2

t != NULL: d->data = t, cap = 2.

data0713size = 2 cap = 2

data[1] = 3, then size++ → 2. (Postfix: use first, then increment.)

data0713size = 2 cap = 2

2 == 2 → full, must grow.

data071323size = 2 cap = 4+2 copies

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.

data071323size = 2 cap = 4

t != NULL: d->data = t, cap = 4.

data0713293size = 3 cap = 4

data[2] = 9, then size++ → 3. (Postfix: use first, then increment.)

data0713293size = 3 cap = 4

3 == 4 is false → there is room; store directly.

data07132934size = 4 cap = 4

data[3] = 4, then size++ → 4. (Postfix: use first, then increment.)

data07132934size = 4 cap = 4

4 == 4 → full, must grow.

data071329344567size = 4 cap = 8+4 copies

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.

data071329344567size = 4 cap = 8

t != NULL: d->data = t, cap = 8.

data0713293446567size = 5 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 appendsdoubling: copiescopies / ngrow by 1: copiescopies / n
870.88283.5
64630.98201631.5
102410231.00523776511.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.

A single append can still be O(n)

The append that triggers growth is expensive; amortized analysis guarantees the average over a long sequence.

Memory overhead

Capacity is at most 2·size; the price is at most 50% empty cells.

The realloc trap

If 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

xx[0]?x[1]?x[2]?rows = 3cols = 4

First allocate an array of pointers with 3 cells (sizeof *x = sizeof(int *)); its contents are still indeterminate.

xx[0]?x[1]?x[2]?rows = 3cols = 4

x != NULL: allocation succeeded, go on.

xx[0]?x[1]?x[2]?rows = 3cols = 4

i = 0 < 3 → allocate row 0.

xx[0]0?1?2?3?x[1]?x[2]?rows = 3cols = 4

x[0] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.

xx[0]0?1?2?3?x[1]?x[2]?rows = 3cols = 4

i = 1 < 3 → allocate row 1.

xx[0]0?1?2?3?x[1]????x[2]?rows = 3cols = 4

x[1] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.

xx[0]0?1?2?3?x[1]????x[2]?rows = 3cols = 4

i = 2 < 3 → allocate row 2.

xx[0]0?1?2?3?x[1]????x[2]????rows = 3cols = 4

x[2] points to a new block of 4 ints (sizeof **x = sizeof(int)). Not NULL.

xx[0]0?1?2?3?x[1]????x[2]????rows = 3cols = 4

3 < 3 is false → the loop ends.

xx[0]0?1?2?3?x[1]????x[2]????rows = 3cols = 4

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)
The question is always the same: how many elements come before A[i][j]? Address = α + (number before it) · element size.
In C

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

00x0001x0112x0223x0331x104x115x126x1372x208x219x2210x23110x001x012x023x034x105x116x127x138x209x2110x2211x23αα + 11·srow major: row by row

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.

00x0001x0132x0263x0391x101x114x127x13102x202x215x228x23110x001x102x203x014x115x216x027x128x229x0310x1311x23αα + 11·scolumn major: column by column

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

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address

We want the address of x[1][2]: α = 100, row length upper1 = 4, s = 4 bytes.

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 6

Before it: 1 full row (1·4 = 4 elements) and 2 elements in its row → k = 6 (blue cells).

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 6

Return 100 + 6·4 = 124.

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 6

printf prints 124. In column-major order k = j·3 + i = 7 → 128.

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address

We want the address of x[2][3]: α = 100, row length upper1 = 4, s = 4 bytes.

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 11

Before it: 2 full rows (2·4 = 8 elements) and 3 elements in its row → k = 11 (blue cells).

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 11

Return 100 + 11·4 = 144.

000010120230311011121322021222300010120230341051161271382092110221123100104108112116120124128132136140144address▲ k = 11

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

plane 0[0][0][0]+0[0][0][1]+1[0][0][2]+2[0][1][0]+3[0][1][1]+4[0][1][2]+5plane 1[1][0][0]+6[1][0][1]+7[1][0][2]+8[1][1][0]+9[1][1][1]+10[1][1][2]+11
elementaddress (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]:

elementaddress
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

The coefficients aj depend only on the dimensions, so the compiler computes them once. In Horner form n−1 multiplications suffice: t = i0; t = t·uj + ij.

Multidimensional Arrays · Run the code step by step

Relative position in n dimensions: Horner’s method

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

We look for G[1][0][2]. t = idx[0] = 1: plane number 1.

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

j = 1 < 3 → one dimension further in.

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

t = 1·2 + 0 = 2: the “global row” number counted across planes (plane 1, row 0).

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

j = 2 < 3 → one dimension further in.

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

t = 2·3 + 2 = 8: 8 elements come before → cell 8.

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

3 < 3 is false → the loop ends.

plane 0000001002010011012plane 110010110211011111200001001200230104011501261007101810291101011111112

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

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

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

001020301002030123456789LOW array (n(n+1)/2 = 10 elements)

4·5/2 = 10 ≤ 100: it fits in LOW. Input: 3 1 4 1 5 9 2 6 5 3.

001020301002030123456789LOW array (n(n+1)/2 = 10 elements)

k = 1·0/2 = 0: 0 elements stored before row 0.

00310203010020303123456789LOW array (n(n+1)/2 = 10 elements)

low[0+0] = low[0] ← 3 (a00).

00310203010020303123456789LOW array (n(n+1)/2 = 10 elements)

k = 2·1/2 = 1: 1 element stored before row 1.

0031020301100203031123456789LOW array (n(n+1)/2 = 10 elements)

low[1+0] = low[1] ← 1 (a10).

003102030114002030311243456789LOW array (n(n+1)/2 = 10 elements)

low[1+1] = low[2] ← 4 (a11).

003102030114002030311243456789LOW array (n(n+1)/2 = 10 elements)

k = 3·2/2 = 3: 3 elements stored before row 2.

00310203011400210303112431456789LOW array (n(n+1)/2 = 10 elements)

low[3+0] = low[3] ← 1 (a20).

0031020301140021503031124314556789LOW array (n(n+1)/2 = 10 elements)

low[3+1] = low[4] ← 5 (a21).

003102030114002159030311243145596789LOW array (n(n+1)/2 = 10 elements)

low[3+2] = low[5] ← 9 (a22).

003102030114002159030311243145596789LOW array (n(n+1)/2 = 10 elements)

k = 4·3/2 = 6: 6 elements stored before row 3.

00310203011400215903203112431455962789LOW array (n(n+1)/2 = 10 elements)

low[6+0] = low[6] ← 2 (a30).

0031020301140021590326031124314559627689LOW array (n(n+1)/2 = 10 elements)

low[6+1] = low[7] ← 6 (a31).

003102030114002159032650311243145596276859LOW array (n(n+1)/2 = 10 elements)

low[6+2] = low[8] ← 5 (a32).

00310203011400215903265303112431455962768593LOW array (n(n+1)/2 = 10 elements)

low[6+3] = low[9] ← 3 (a33).

00310203011400215903265303112431455962768593LOW array (n(n+1)/2 = 10 elements)

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?

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

We ask for a21.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

Bounds check: the indices are in 0…3.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

2 < 1? No → in the lower triangle.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

k = 3·2/2 + 1 = 4 → LOW[4] = a21.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

printf prints 4.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

We ask for a13.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

Bounds check: the indices are in 0…3.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

1 < 3? Yes → upper triangle (the zero region): −1.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

printf prints -1. The error codes {−1, −2} and the valid results {0, 1, 2, …} never overlap, so the caller can tell them apart.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

We ask for a40.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

Bounds check: i = 4 ≥ n → invalid!

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

Print an error message and return −2.

00a001020301a10a11002a20a21a2203a30a31a32a330a001a102a113a204a215a226a307a318a329a33LOW array (n(n+1)/2 = 10 elements)

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 regionelements in row ik (row major)
j ≤ i (lower left)i + 1i(i+1)/2 + j
j ≥ i (upper right)n − ii·n − i(i−1)/2 + (j − i)
i + j ≤ n−1 (upper left)n − ii·n − i(i−1)/2 + j
i + j ≥ n−1 (lower right)i + 1i(i+1)/2 + j − (n−1−i)
A real symmetric matrix (aij = aji): instead of returning −1 for the upper triangle, swap the indices: if i < j, 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

00d001d012c3c1d10d11d12c2d20d21d22d233cd31d32d330d201d312d103d214d325d006d117d228d339d0110d1211d23band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

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

From the instructor “Storing it is not the problem; can we access it quickly?”

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).
From the instructor “I design the mechanism by analyzing as if every resource were scarce.”

Triangular and Band Matrices · Run the code step by step

Building a band matrix (lower bands)

0012c3c1c23c01234567891011band[]band -2band -1band 0band 1-2-101start[](index = band no + a − 1)

4·4 − 3 − 1 = 12 ≤ MAX_SIZE. Input arrives band by band: 6 10 · 3 7 11 · 1 4 8 12 · 2 5 9.

0012c3c1c23c01234567891011band[]band -2band -1band 0band 1-2-101start[](index = band no + a − 1)

Band i = -2 (lower): 2 elements.

0012c3c1c23c01234567891011band[]band -2band -1band 0band 1-20-101start[](index = band no + a − 1)

start[-2+2] = start[0] = 0: the band begins at band[0].

0012c3c1c263c061234567891011band[]band -2band -1band 0band 1-20-101start[](index = band no + a − 1)

band[0+0] ← 6 (d20).

0012c3c1c263c1006110234567891011band[]band -2band -1band 0band 1-20-101start[](index = band no + a − 1)

band[0+1] ← 10 (d31).

0012c3c1c263c1006110234567891011band[]band -2band -1band 0band 1-20-101start[](index = band no + a − 1)

total += 2 → 2: where the next band begins.

0012c3c1c263c1006110234567891011band[]band -2band -1band 0band 1-20-101start[](index = band no + a − 1)

Band i = -1 (lower): 3 elements.

0012c3c1c263c1006110234567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

start[-1+2] = start[1] = 2: the band begins at band[2].

0012c3c13c263c10061102334567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

band[2+0] ← 3 (d10).

0012c3c13c2673c100611023374567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

band[2+1] ← 7 (d21).

0012c3c13c2673c1011061102337411567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

band[2+2] ← 11 (d32).

0012c3c13c2673c1011061102337411567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

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)

0012c3c13c2673c1011061102337411567891011band[]band -2band -1band 0band 1-20-1201start[](index = band no + a − 1)

Band i = 0 (main diagonal): 4 elements.

0012c3c13c2673c1011061102337411567891011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

start[0+2] = start[2] = 5: the band begins at band[5].

00112c3c13c2673c10110611023374115167891011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

band[5+0] ← 1 (d00).

00112c3c134c2673c101106110233741151647891011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

band[5+1] ← 4 (d11).

00112c3c134c26783c1011061102337411516478891011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

band[5+2] ← 8 (d22).

00112c3c134c26783c10111206110233741151647881291011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

band[5+3] ← 12 (d33).

00112c3c134c26783c10111206110233741151647881291011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

total += 4 → 9: where the next band begins.

00112c3c134c26783c10111206110233741151647881291011band[]band -2band -1band 0band 1-20-12051start[](index = band no + a − 1)

Band i = 1 (upper): 3 elements.

00112c3c134c26783c10111206110233741151647881291011band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

start[1+2] = start[3] = 9: the band begins at band[9].

001122c3c134c26783c101112061102337411516478812921011band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

band[9+0] ← 2 (d01).

001122c3c1345c26783c1011120611023374115164788129210511band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

band[9+1] ← 5 (d12).

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

band[9+2] ← 9 (d23).

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

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

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

We ask for d31; the indices are valid. Band number j − i = -2.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

i − j = 2 < a = 3? Yes → a lower band.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

start[-2+2] + j = 0 + 1 = 1 → band[1] = 10.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

We ask for d12; the indices are valid. Band number j − i = 1.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

1 ≥ 1 → upper triangle.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

1 < b = 2 → a valid upper band: start[1+2] + i = 9 + 1 = 10.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

We ask for d03; the indices are valid. Band number j − i = 3.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

3 ≥ 1 → upper triangle.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

3 ≥ b = 2 → outside the band, upper constant region: −1 (value c).

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

We ask for d30; the indices are valid. Band number j − i = -3.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

i − j = 3 < a = 3? No → outside the band.

001122c3c1345c267893c10111206110233741151647881292105119band[]band -2band -1band 0band 1-20-120519start[](index = band no + a − 1)

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”.

From the instructor “Extra memory is fine if it improves performance, within reason.”

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) ...
In fact: the C standard guarantees left-to-right evaluation and short-circuiting for && 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.
The real trap

& (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 */
Dot and arrow

h1.age is a field of a variable; p->age ≡ (*p).age (p: human *).

Assignment yes, == no

Structures can be assigned as a whole but not compared; write a function that compares field by field.

typedef

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;
abcNULLdatalink

Self-referential structures are the basis of linked lists.

A structure cannot contain a field of its own type (infinite size), but it can contain a pointer to its own type.

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.

{char c; int x; char d;} → 12cxxxxd{int x; char c; char d;} → 8xxxxcdempty box = padding byte

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

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

An array of structures sits in memory record after record; each record is sizeof(Student) bytes.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

0 < 3 → record 0.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

Add s[0].gpa = 3.2 → sum = 3.2.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

1 < 3 → record 1.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

Add s[1].gpa = 2.6 → sum = 5.8.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

2 < 3 → record 2.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

Add s[2].gpa = 3.5 → sum = 9.3.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

3 < 3 is false → the loop ends.

s[0]101"Ada"3.2idnamegpas[1]102"Can"2.6idnamegpas[2]103"Ece"3.5idnamegpa

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 ==! */
0'd'1'o'2'g'3'\0's
functiontask
strlenlength (excluding \0)
strcpy / strncpycopy
strcatappend
strcmpcompare lexicographically
strchr / strstrfind a char / substring

Strings · Run the code step by step

How does strlen work?

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 0

p points to the same place as s (the first character).

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 0

*p = 'd' ≠ '\0' → continue.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 1

p++: one character (1 byte) forward.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 1

*p = 'a' ≠ '\0' → continue.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 2

p++: one character (1 byte) forward.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 2

*p = 't' ≠ '\0' → continue.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 3

p++: one character (1 byte) forward.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 3

*p = 'a' ≠ '\0' → continue.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 4

p++: one character (1 byte) forward.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 4

*p = '\0' → the loop ends.

0'd'1'a'2't'3'a'4'\0's▲ pp − s = 4

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

a1234×b5678=c0

c[0][0] uses row 0 of a and column 0 of b; start at 0.

a1234×b5678=c5

k = 0: += a[0][0]·b[0][0] = 1·5 → 5.

a1234×b5678=c19

k = 1: += a[0][1]·b[1][0] = 2·7 → 19.

a1234×b5678=c190

c[0][1] uses row 0 of a and column 1 of b; start at 0.

a1234×b5678=c196

k = 0: += a[0][0]·b[0][1] = 1·6 → 6.

a1234×b5678=c1922

k = 1: += a[0][1]·b[1][1] = 2·8 → 22.

a1234×b5678=c19220

c[1][0] uses row 1 of a and column 0 of b; start at 0.

a1234×b5678=c192215

k = 0: += a[1][0]·b[0][0] = 3·5 → 15.

a1234×b5678=c192243

k = 1: += a[1][1]·b[1][0] = 4·7 → 43.

a1234×b5678=c1922430

c[1][1] uses row 1 of a and column 1 of b; start at 0.

a1234×b5678=c19224318

k = 0: += a[1][0]·b[0][1] = 3·6 → 18.

a1234×b5678=c19224350

k = 1: += a[1][1]·b[1][1] = 4·8 → 50.

a1234×b5678=c19224350

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

  1. Build the step count table for mult (n×n); how many multiplications are performed?
  2. What is the complexity of multiplying an m×n matrix by an n×p matrix? How many cells does the result have?
  3. Write a function that transposes an n×n matrix without an extra array (in place).
Solutions
  1. 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.)
  2. n products per c[i][j]: O(m·n·p); m·p cells.
  3. 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

OperationTimeExtra 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 middleO(n)O(1)
Lower triangular / band matrix accessO(1)n(n+1)/2 / band elements
build_band, read_lowerO(stored elements)START: a+b−1
strlen / lengthO(n)O(1)
Matrix addition (m×n)O(mn)O(1)
Matrix multiplication (n×n)O(n³)O(1)

Wrap-Up

Common Mistakes

1Writing past the end of an array (a[n]); C does not check.
2a = realloc(a, …): losing the old block on failure.
3Mixing up sizeof(int *) and sizeof(int); using sizeof a on a parameter.
4Freeing only the pointer array of a 2D dynamic array, not its rows.
5Mixing up the row- and column-major formulas; forgetting the element size (s).
6Using == on strings, or leaving no room for '\0'.

Wrap-Up

Exercises

  1. double d[5][8] row-major, α = 2000: &d[3][5]? Column-major?
  2. int A[4][5][6]: relative position of A[2][3][4] by Horner’s method?
  3. 5×5 lower triangular matrix: where in LOW is a43?
  4. Band matrix n = 6, a = 2, b = 3: how many elements? START array?
  5. A dynamic array starts at capacity 1 and doubles. Total copies after 100 appends?
  6. How can reordering the fields shrink struct { char a; double b; char c; }?
Answers
  1. Row: 2000 + (3·8 + 5)·8 = 2232. Column: 2000 + (5·5 + 3)·8 = 2224.
  2. (2·5 + 3)·6 + 4 = 82.
  3. k = 4·5/2 + 3 = 13.
  4. 6·4 − 1 − 3 = 20; bands −1, 0, 1, 2 → lengths 5, 6, 5, 4 → START = [0, 5, 11, 16].
  5. 1 + 2 + 4 + … + 64 = 127 (< 2·100).
  6. double first: { double b; char a, c; } → typically 16 bytes (not 24).

Wrap-Up

Chapter Summary

Array = mapping

ADT: ⟨index, value⟩; the C implementation is contiguous memory with address α + i·s.

Addressing

Row major: α + (i·u1 + j)·s; in n dimensions, n−1 multiplications with Horner.

Saving space

Triangle: n(n+1)/2 elements, k = i(i+1)/2 + j. Band: bands numbered j − i + START.

Dynamic memory

malloc/realloc/free; growth by doubling is amortized O(1); a 2D array = an array of pointers.

Records

A struct groups fields, a union shares them; a string is a char array ending in '\0'.

Matrices

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).

The End

Chapter 2: Arrays and Structures
Next chapter: Chapter 3: Recursion, Binary Search and Backtracking · Course page
1 / 1 Data Structures in C