Class 4 — Thursday, September 3

Predict-then-run on one character, then a loop ladder you climb at your own speed.
main is shorter here than in the book

From here on this course writes

int main() {

where [DIS] writes int main(void), and it leaves off the return 0; at the end. Both of the book’s versions are still correct and you will keep seeing them — in the book, in older code, and in anything a chatbot writes you. Ours is shorter and means the same thing:

  • return 0; has been optional since 1999. Reaching the closing brace of main returns 0 by itself. That is what A1’s optional echo $? question was pointing at.
  • () and (void) both say “no arguments” in the C the lab machines compile. They did not always, which is why the book is careful; gcc -Wall is silent on either.

Copy whichever you like. Nothing is graded on it. It is here so that the difference between the book’s programs and ours does not read as a mistake in one of them.

1 Predict-then-run

Write your prediction down before you run it — in notes.txt or in your Brightspace Journal, whichever is already open. Not in your head: a prediction you did not write down is one you get to be right about afterwards.

Download it rather than typing it: bad.c. The whole point of this one is a single character, and typing it out is the easiest way to fix the bug by accident before you have seen it.

#include <stdio.h>

int main() {
    int x = 3;
    if (x = 5) {
        printf("x is five\n");
    }
    printf("x is now %d\n", x);
}

Predict two things, separately:

  1. Does x is five print?
  2. What number does the second line print?

Then compile it with gcc -Wall bad.c -o bad and run it. Read everything gcc printed, including nothing at all.

2 The loop ladder

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.

When you are done, let the instructor know.

2.1 Rung 1 — countdown.c

Count down from 10 to 1, one number per line, then print Liftoff!.

#include <stdio.h>

int main() {
    int start = 10;

    // your work goes here
}

Use a while loop. Done when your screen shows eleven lines: 10, 9, …, 1, Liftoff!.

Make the file yourself — that is half of what this rung is for. If you cannot get one made and you have already put your hand up, take countdown.c and keep going; being stuck on nano is not a reason to miss the loop.

2.2 Rung 2 — break it

Change start to 3, recompile, run. Then change it to 0, recompile, run.

Done when you can say out loud what 0 printed and whether that is what you wanted. Then write it down — it is the start of your notes.txt for the assignment that goes out at the end of class.

2.3 Rung 3 — evens.c

A new file. Print every number from 1 to limit, with int limit = 20; at the top, marking the even ones:

1
2 even
3
4 even

Use a for loop with an if inside it. % is how you ask whether a number is even; you met it in A1.

Done when your output has 20 lines and exactly 10 of them say even.

2.4 Rung 4 — table.c

The multiplication table from 1 to size, with int size = 9; at the top.

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27

One for loop inside another. printf("%3d", ...) makes each number three characters wide, which is what lines the columns up. The newline goes in the outer loop, not the inner one — that is the whole exercise.

Done when you have nine rows of nine numbers and the columns are straight.

2.5 Rung 5 — if you got here

Rewrite Rung 1 as a for loop that does exactly the same thing. Keep both versions. Which one reads better to you, and would your answer change if the countdown started at a number you had to compute first?

2.6 Rung 6 — the book’s own exercises

If you got through Rung 5, go to Dive Into Systems: Exercises and work the Conditionals and Loops set. They run in the browser and tell you straight away whether you are right, which is the fastest feedback available to you anywhere in this course.

Nothing here is collected and none of it is graded. It is the best thing to do with the rest of the period, and it is also the best thing to do this weekend if the loops in A2 are not yet coming out on the first try.

3 Before you leave: upload what you have

Everything you wrote today is worth keeping, and it is the start of the assignment going out at the end of class.

Your home folder persists, but only on the machine you are sitting at — a different seat next time is a different home folder. So put every .c file you made today on Brightspace before you go. It keeps the latest version, so uploading a half-finished countdown.c costs you nothing and replacing it on Tuesday is one click.