Midterm Part 1 — the problems

Four programs, fifty minutes, written on a lab machine and attached to the Midterm Part 1 quiz. Open book, no AI.

Fifty minutes. Everything on Midterm Part 1 applies: open book and open notes, no AI, no collaboration, and you hand in through the Midterm Part 1 quiz on Brightspace — under Quizzes, not Assignments. Attach every file to its one question, the Makefile included. Attach as you go, and press Submit once, at the end: you get one attempt, so submitting ends your exam. A program that does not compile is still worth attaching.

Match the output shown exactly, wording and all — a first pass on a submission is scripted.

1. reverse.c — a number, backwards

Read one non-negative integer with scanf. Print its digits in reverse order, as a number.

No array and no string. A loop and two variables is the whole program.

For the input 90210 it prints:

reversed 1209

A number has no leading zero, so that is right.

Done when it prints that for 90210, reversed 7 for 7, and reversed 0 for 0.

2. stamps.c — a function that returns a value

Write int stamps(int grams): how many stamps a letter of that weight needs. 1 up to 50 g, 2 up to 100 g, 3 up to 500 g, and 0 for anything heavier. Then a main holding these five asserts and the line after them:

assert(stamps(20) == 1);
assert(stamps(50) == 1);
assert(stamps(51) == 2);
assert(stamps(500) == 3);
assert(stamps(501) == 0);
printf("all examples passed\n");

Done when it prints all examples passed.

3. readings — one program in three files, and a Makefile

  • readings.h — an include guard, and prototypes for highest and count_below. Nothing else.

  • readings.c — the two definitions, and no main. int highest(int arr[], int n) is the largest number in the array. int count_below(int arr[], int n, int limit) is how many of them are less than limit.

  • main.c — main and nothing else. An array holding the five readings -3 -12 -7 -8 -1, and two lines:

    highest -1
    below -5: 3
  • Makefile — builds a program called readings from the two .c files, and has a clean rule. Type it by hand, tab and all. Any Makefile that builds the program is a correct answer — with .o files or without.

Done when make builds it, ./readings prints those two lines, and make a second time in a row says there is nothing to do.

4. stars.c — a word with its vowels covered

Read one word with scanf("%99s", word) into a char word[100], replace every lowercase vowel — a, e, i, o, u — with *, and print the word. Change the array itself, and walk to the '\0' yourself — no strlen.

word: seventeen
s*v*nt**n

Done when it prints that for seventeen, **d** for audio, and rhythm unchanged for rhythm.