Declaring and indexing an array
Also called array, index, initializer list.
Indexes run from 0 to SIZE − 1. Elements an initializer list doesn't reach are 0; int z[4] = {}; is all zeros.
const int SIZE = 5;
int a[SIZE] = {1, 2, 3}; // 1 2 3 0 0
a[0] // first element
a[SIZE - 1] // last element
Careful: A local array with no initializer holds leftovers. The size must be a constant: int a[n]; with a typed n is not standard C++.
Taught in Declaring and indexing an array
Counting elements with sizeof
Also called sizeof(a) / sizeof(a[0]), array length.
Bytes in the whole array divided by bytes in one element gives the count.
int a[5];
sizeof(a) // 20 bytes
sizeof(a) / sizeof(a[0]) // 5 elements
Careful: Inside a function the parameter is an address: sizeof gives 8, with the warning -Wsizeof-array-argument. Pass the count instead.
Taught in Declaring and indexing an array
Walking an array
Also called loop over an array, out of bounds, buffer overrun.
Start at 0 and test i < SIZE: every element once, never index SIZE.
for (int i = 0; i < SIZE; i++) {
sum += a[i];
}
Careful: i <= SIZE reads a[SIZE], past the end. Nothing checks indexes, so this is undefined behavior, not an error message.
Taught in Walking an array, and running off the end
Copying and comparing arrays
Also called element-by-element copy, invalid array assignment, Warray-compare.
An array's name stands for the address of its first element. Copy and compare element by element.
| Line | What happens |
|---|
b = a; | compile error: invalid array assignment |
a == b | compares addresses: false for two arrays |
cout << a; | prints an address |
b[i] = a[i]; in a loop | copies every element |
Taught in Arrays are not values
Arrays as parameters
Also called array parameter, const parameter, pass an array.
The function receives the address of the first element, so writes through a[i] change the caller's array. const forbids writes; pass the count as n.
double average(const double a[],
int n) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
return sum / n;
}
Careful: No copy is made. A write to a const parameter is a compile error beginning assignment of read-only location.
Taught in Arrays and functions
Linear search
Also called find an element, not found, return -1.
Return the index at the first match; return -1 only after the loop has checked every element.
for (int i = 0; i < n; i++) {
if (a[i] == t) {
return i;
}
}
return -1;
Careful: An else return -1; inside the loop gives up at the first mismatch. A missing value costs n comparisons.
Taught in Searching and the largest value
Largest or smallest (best so far)
Also called maximum, minimum, index of the largest.
at is the index of the largest so far; a[at] is its value. Use < for the smallest. With > a tie keeps the first.
int at = 0;
for (int i = 1; i < n; i++) {
if (a[i] > a[at]) {
at = i;
}
}
Careful: Never start the best value at 0: with all-negative (or, for a minimum, all-positive) values it reports a number not in the array.
Taught in Searching and the largest value
Two-dimensional arrays
Also called 2D array, row and column, row-major order, grid.
Row first, then column, both from 0. Walk with nested loops, rows outside and columns inside. Rows are stored one after another (row-major order).
int g[2][3] = {{1, 2, 3},
{4, 5, 6}};
g[1][0] // row 1, column 0: 4
Careful: For double t[3][4], t[3][2] and t[0][4] are out of bounds: rows run 0 to 2, columns 0 to 3.
Taught in Two-dimensional arrays
Swapping two values
Also called swap, temporary variable.
Save one value before the first assignment overwrites it.
int t = a;
a = b;
b = t; // or swap(a, b);
Careful: a = b; b = a; leaves both holding the old b.
Taught in Selection sort
Selection sort
Also called sorting, sort an array.
Each pass selects the smallest unsorted element and swaps it into place: one swap per pass. It makes (n − 1) + (n − 2) + … + 1 comparisons, 10 for 5 elements.
for (int i = 0; i < n - 1; i++) {
int m = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[m]) {
m = j;
}
}
swap(a[i], a[m]);
}
Careful: Start the inner loop at i + 1, after the sorted part. The work grows with the square of n.
Taught in Selection sort