CS U111 · Lectures 7–9 · drill

Functions, arrays & strings: predict the output

Six short programs per sheet, freshly generated each time. Write exactly what each one prints, then reveal: the real output, a step-by-step trace, and the one rule the program was testing. Reading code like this — not writing it — is what dry-run and debugging questions test. A sheet takes about fifteen minutes.

How to use it

Every program is complete except for the #include lines at the top (<stdio.h>, plus <string.h> and <ctype.h> for strings). The array and string programs show only what sits inside main. The scanf items show the typed input under the code. Trace on paper first, then type the output into the box exactly. If the program prints several lines, press Enter between them, or type ⏎. Extra spaces don't count against you. Reveal marks each answer. Open See the method to get the trace and the rule. If your trace is right but the answer is marked wrong, it's a typing slip. If your trace is wrong, reread that topic in the functions lesson, the arrays lesson or the strings notes.

Where these come from

Every family comes from a slide in Lecture 7 (functions), Lecture 8 (arrays) or Lecture 9 (strings): pass by value, return versus printf, local versus global variables, early return, linear search with break, min/max trackers, bubble and selection passes, 2D traversal and transpose; '\0', strlen and sizeof, strcmp/strcpy/strcat, character loops and scanf with %s and %[^\n]. Nothing here goes beyond those three lectures. There are no pointers and no passing arrays to functions, because the course hasn't taught those yet.

The rules the families test

F1. C passes arguments by value. The function works on a copy, so the caller's variable changes only if the caller assigns the returned value back to it (a = change(a);). It doesn't matter whether the parameter has the same name as the caller's variable.

F2. A printf inside a function runs when the function is called. The returned value only appears on screen if the caller prints it. In printf("%d\n", f(x) + 1), f runs completely, including its own printf, before the outer printf prints anything.

F3. A global variable is shared by every function. If a function declares a local variable with the same name, that local hides the global inside that function. Changes to the local are lost when the function ends.

F4. return ends the function immediately, even from inside a loop. Nothing after it in the function runs.

F5. Nested calls are evaluated from the inside out, and each function hands its value to the one around it. Integer division / drops the fraction, rounding toward zero (so -7 / 2 is -3).

A1. The condition decides which elements count. Check whether it tests the value a[i] or the index i. sum / n with two ints drops the fraction; (double) sum / n keeps it.

A2. The tracker starts at a[0] with position 0. With a tie, > keeps the first position where the value appears and >= moves to the last.

A3. break stops the search at the first match. Without it, the loop checks every element and found ends up holding the last match. If there is no match, found keeps its starting value, −1.

A4. Each bubble pass carries the largest remaining value to the end. Each selection pass swaps the smallest remaining value into the front slot. Stopping after k passes leaves the array partly sorted.

A5. m[r][c] means row r, column c, both counted from 0. The transpose swaps the indices: t[j][i] = m[i][j], so t[x][y] equals m[y][x].

S1–S2. sizeof is the array's fixed size in bytes. strlen counts characters up to the first '\0'. A '\0' written into the middle hides the rest of the string from %s and strlen, but the bytes are still there.

S3–S4. strcmp returns 0 for equal text and otherwise a number whose sign gives the order, so if (strcmp(a, b)) means "different". == compares addresses, never text. strcpy replaces, strcat appends, and neither checks the size.

S5–S7. A char is a small number: %c shows the character and %d its code. c - '0' gives a digit's value, and upper and lower case are 32 apart. Loop until '\0', or use < strlen; <= runs once too many.

S8. %s skips leading spaces and reads one word. %[^\n] reads the rest of the line, spaces included. After %d, the leftover Enter makes %[^\n] read nothing unless the format starts with a space. More in the strings notes.