Class 5 — Tuesday, September 8
Everything below Rung 1 on this page is the assignment. This period is the last time we are in a room together before the deadline, so use it on the assignment rather than on anything else.
If the period runs out, make sure you have got to Rung 5. It is the one that wants thinking time rather than typing time, so it is the one that goes worst late at night. The rest you can finish at a keyboard tonight.
1 Predict-then-run
Make the file yourself, call it scanf.c, and copy-paste the program below into it — do not retype it. One character is missing on purpose, and retyping is the easiest way to put it back by accident before you have seen what its absence does.
#include <stdio.h>
int main() {
int count;
printf("Type a number and press Enter:\n");
scanf("%d", count);
printf("You typed %d\n", count);
}Write your prediction in your Brightspace Journal before you run it. Not in a file on this machine, and not in your head — a prediction you did not write down is one you get to be right about afterwards.
Predict three things, separately:
- Does
gcc scanf.c -o scanfcompile it? - Does
gcc -Wall scanf.c -o scanfcompile it? - If it runs, what does it print when you type
42?
Then do all three, in that order. Read everything gcc printed, including nothing at all.
Now go back to the same Journal note and add what actually happened, and your best guess at why — underneath the prediction, not in a new note. Having the two next to each other is the point.
A guess you are not sure about is worth more here than nothing. Nobody is graded on the prediction being right, and the ones that were wrong are the ones worth reading out.
2 The ladder
These are the steps of A2 you have left, in order. Start at Rung 1 even if you think you are past it — it is short, and the rest is built on it.
Pairs, one keyboard between two people. The person not typing reads this page out loud and says what to type next, and you swap who types at every rung.
If you have been stuck for three minutes, put your hand up. Three minutes is not a long time and a hand up is not a failure — it is the rule here.
Already finished at home? Do not sit through the rungs again. Skip to If you have already finished at the bottom of this page and start there.
2.1 Rung 1 — table.c
The multiplication table from 1 to size, with int size = 9; at the top. One for loop inside another.
The whole exercise is where the newline goes. A printf("\n") inside the inner loop gives you 81 lines with one number on each. It belongs in the outer loop, after the inner one has finished a row.
Done when you have nine rows of nine numbers and the columns line up. printf("%3d", ...) is what makes them line up — the 3 is how many characters wide to make each number.
2.2 Rung 2 — digits.c
Read a number with scanf, then count its digits by dividing by 10 until you reach 0.
This is your first scanf, and the & you just spent ten minutes on is the part to get right. Arithmetic only — do not turn the number into a string.
Done when you have run it three times, typing 90210, 7 and 0, without recompiling in between. One of those three is very likely wrong the first time. Which one, and did you fix it? That goes in notes.txt.
Notice what scanf just bought you: three tests and no recompile. Every program you wrote before this one needed an edit and a rebuild to test a second value.
2.3 Rung 3 — grade.c
int score = 85; at the top, and one if / else if / else chain that prints the letter grade. Not six separate if statements.
Done when you have tested 90, 89, 60 and 59 — the boundaries, not the middles — and the four lines are in notes.txt. A boundary coming out wrong is the normal outcome here; fixing it is the exercise.
2.4 Rung 4 — break digits.c on purpose
Run digits.c again and, where it asks for a number, type a letter.
Done when you can say out loud what happened and whether the program noticed. Then explain it in notes.txt.
Nothing in the assignment asks you to fix this. It is here because it is the same shape as the program at the top of this page, and because it is worth knowing that a C program will happily carry on after being handed something it did not ask for.
2.5 Rung 5 — the three programs gcc accepts
Compile all three, run all three, explain all three. If the period is running out, this is the rung to be on — the ones above it are typing, and this one is thinking, which is the part that does not go well in a hurry.
You have seen the first one before — it is the program from Thursday. The other two you have not.
What is being asked for at the end is an argument, not an answer: which kind of failure is worse to ship — one the compiler rejects, or one it accepts and runs wrong. Three or four sentences. There is no answer being looked for.
3 If you have already finished
Then the ladder has nothing for you. Do these instead, in order.
3.1 triangle.c — table.c with one thing changed
Same nine rows, but row 1 has one number on it, row 2 has two, and row 9 has nine. Start from your table.c and change one thing.
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
The number in each spot is still row times column. What changes is how far the inner loop runs. In table.c it stops at size every time. Here it has to stop somewhere different on every row, and the row number is already sitting in a variable you can use.
Done when the first row has one number, the last has nine, and the left edge is straight down. If your left edge is ragged, the %3d is doing its job and something else is wrong.
This is not part of the assignment and nothing is collected. It is here because a nested loop whose inner bound is a constant is the easy case, and you have now done it twice.
3.2 Then the book’s exercises for this week’s reading
Both sets run in the browser and mark themselves, which is the fastest feedback available to you anywhere in this course. Nothing is collected.
- Input/Output (printf and scanf) — three questions, one of them the missing
&from the top of this page. - Conditionals and Loops — eighteen questions. Six of them are “how many times does this loop print?”, which is the fastest way to find out whether loops are actually sticking.
3.3 If you finish that too
Start Rung 5’s closing argument in writing, at the keyboard, while you are sitting in front of one: which kind of failure is worse to ship — one the compiler rejects, or one it accepts and runs wrong. It is the thing most likely to get skipped tonight, and the thing that goes worst when it is rushed.
4 Before you leave: upload
Your home folder persists, so today’s files will still be here next time you sit at this machine. Only this one, though — a different seat is a different home folder, and that is the way to lose an afternoon’s work without doing anything wrong.
So it is worth putting today’s .c files on Brightspace before you go. It keeps the latest version, so uploading a half-finished grade.c costs you nothing and replacing it tonight is one click.