Practice test for Midterm Part 2
Thursday, October 29, 12:30, in Ben Franklin 103. Forty minutes. It counts for nothing. Everything on Midterm Part 2 applies: open book, no AI, no collaboration, no device but the lab computer, and you hand in through the Practise test 2 quiz on Brightspace, under Quizzes, as the exam’s hand-in is. At the end, put your files in one zip, zip practice.zip hms.c repeat.c invert.c Makefile, attach practice.zip to the quiz’s one question, and press Submit once: you get one attempt. A program that does not compile still goes in the zip.
The room’s internet is restricted for the period, as it will be on the day: Brightspace, this site and the two books.
Hand in hms.c, repeat.c, invert.c and your Makefile. Every function gets the recipe’s one-line comment, and a function that hands back new memory says who frees it. Build with the course Makefile saved beside your files: make hms, make repeat.
hms.c — three answers from one function
Write void to_hms(int total, int * h, int * m, int * s), which splits total seconds into hours, minutes and seconds and hands each back through its pointer. With this main:
int main() {
int h;
int m;
int s;
to_hms(3725, &h, &m, &s);
assert(h == 1);
assert(m == 2);
assert(s == 5);
to_hms(59, &h, &m, &s);
assert(h == 0);
assert(m == 0);
assert(s == 59);
to_hms(7200, &h, &m, &s);
assert(h == 2);
assert(m == 0);
assert(s == 0);
printf("all examples passed\n");
}Done when ./hms prints all examples passed, with one example of your own added to main, its numbers worked out by hand first.
repeat.c — a string written over and over, into new memory
Write char * repeat(char * s, int k), which hands back a new string: s written k times, one after another. Ask malloc for exactly enough room, the '\0' included, and check its answer with assert. The caller frees the result. With this main:
int main() {
char * t = repeat("ab", 3);
assert(strcmp(t, "ababab") == 0);
free(t);
t = repeat("pixel", 1);
assert(strcmp(t, "pixel") == 0);
free(t);
t = repeat("ab", 0);
assert(strcmp(t, "") == 0);
free(t);
printf("all examples passed\n");
}Done when ./repeat prints all examples passed and the flag, -fsanitize=address, reports nothing, with one example of your own added.
invert.c — a picture, every color turned around
A picture is w by h pixels, and each pixel is three bytes, red, green and blue, each from 0 to 255: 3 * w * h bytes in one array, in rows from the top. Save the image library, image.h and image.c: read_ppm(&w, &h) reads a picture from the input into new memory and hands it back, and the caller frees it; write_ppm(px, w, h) writes one to the output. Save stripes.ppm too: red over white over blue.
Write void invert(unsigned char * px, int w, int h), which turns every byte b into 255 - b, with one pointer that walks the bytes: no [ or ]. Then a main that reads a picture, inverts it, writes it and frees it. Add a rule for invert at the bottom of your Makefile, the way Class 18 did for draw, and make invert:
invert: invert.c image.c image.h
gcc $(CFLAGS) -o invert invert.c image.cDone when ./invert < stripes.ppm > i.ppm opens in Loupe (loupe i.ppm) as cyan over black over yellow, and ./invert < stripes.ppm | ./invert | cmp - stripes.ppm prints nothing: inverted twice, the picture is back, byte for byte.