Class 14 — Thursday, October 8
First, the Opening Journal: its prompt is in the announcements on Brightspace.
1 An array, without brackets
On Tuesday, a + 1 printed the same address as &a[1]. It is not one byte past a but one element past, and an int is four bytes here. So a + i is the address of a[i], and *(a + i) is a[i] itself. The brackets are shorthand for exactly that.
That gives a second way to walk an array: a pointer that starts at a and moves on one element at a time with p++, until it reaches a + n:
a + n is the address just past the last element. Comparing with it is fine, but *(a + n) reads past the end of the array, which is undefined behavior: C reads whatever is there.
2 An array, in pairs
In pairs; swap who types at every Done when. Every function walks its array with a pointer, with no [ or ] inside it. Every function gets the design recipe’s one-line comment, and every test at least one assert of your own.
Keep entr running in a second terminal, so that every save recompiles and runs your file. The command is on the command line page.
2.1 fill
Write void fill(int * a, int n, int value), which sets every one of the n elements to value:
#define SIZE 5
int main() {
int a[SIZE];
fill(a, SIZE, 7);
for (int i = 0; i < SIZE; i++) {
assert(a[i] == 7);
}
}Done when the asserts pass.
2.2 flip
Write void flip(int * a, int n), which reverses the array in place. Start one pointer at the first element and one at the last, swap what they point to with the swap from Class 12, and move them towards each other while the first is still before the second.
#define SIZE 5
int main() {
int a[SIZE] = {1, 2, 3, 4, 5};
flip(a, SIZE);
assert(a[0] == 5);
assert(a[2] == 3);
assert(a[4] == 1);
}Done when the asserts pass. Then change SIZE to 4 — one edit — fix the values and the asserts, and check again: an even length has no middle element.
2.3 find
From here on, for pairs who are done with 2.1 and 2.2.
Write int * find(int * a, int n, int target), which returns the address of the first element equal to target, or NULL when there is none:
#define SIZE 5
int main() {
int a[SIZE] = {4, 8, 15, 16, 23};
int * p = find(a, SIZE, 15);
assert(p != NULL);
*p = 150;
assert(a[2] == 150);
assert(find(a, SIZE, 99) == NULL);
}Done when the asserts pass.
2.4 rotate
Write void rotate(int * a, int n), which moves every element one place to the right, and the last one round to the front. Walk a pointer backwards, from the last element down, with p--:
#define SIZE 5
int main() {
int a[SIZE] = {1, 2, 3, 4, 5};
rotate(a, SIZE);
assert(a[0] == 5);
assert(a[1] == 1);
assert(a[4] == 4);
}Done when the asserts pass, and so does your own check that SIZE rotations bring the array back to where it started.
3 A string, without brackets
A string is an array of char with a '\0' after its last letter. The loop below is section 1’s walk, with a different test.
4 A string, in pairs
In pairs again, with entr still running, and still no [ or ] inside any function.
4.1 length
Write int length(char * s), which returns how many characters come before the '\0':
Done when the asserts pass.
4.2 count
Write int count(char * s, char c), which returns how many times c appears in s:
int main() {
assert(count("banana", 'a') == 3);
assert(count("banana", 'z') == 0);
assert(count("", 'a') == 0);
}Done when the asserts pass.
4.3 upper
From here on, for pairs who are done with 4.1 and 4.2.
Write void upper(char * s), which changes every lowercase letter in s to its capital, in place. A letter c is lowercase when it is between 'a' and 'z', and c - 'a' + 'A' is its capital:
int main() {
char word[] = "Hello, C!";
upper(word);
assert(word[0] == 'H');
assert(word[1] == 'E');
assert(word[4] == 'O');
assert(word[5] == ',');
assert(word[7] == 'C');
}Done when the asserts pass.
4.4 find_char
Write char * find_char(char * s, char c), which returns the address of the first c in s, or NULL when there is none: 2.3’s find, for a string.
int main() {
char name[] = "pointer";
char * p = find_char(name, 'n');
assert(p != NULL);
*p = 'N';
assert(name[3] == 'N');
assert(find_char(name, 'z') == NULL);
}Done when the asserts pass.
5 Before you leave
- A6 goes out today, due Wednesday, October 14.
- No class on Tuesday: it is fall break. The next class is Thursday 10/15, and the reading is due before it.
- The Closing Journal: its prompt is in the announcements on Brightspace.