Class 3 — Tuesday, September 1

Two rounds of predict-then-run on integer arithmetic, then the first three lines of seconds.c.

1 Predict, then run — round 1

Write down what you think each line prints before you run anything.

printf("%d\n",  7 / 2);
printf("%d\n",  7 % 2);
printf("%f\n",  1 / 2 * 2.0);

Add them to hello.c, below the line already there. Recompile with gcc hello.c -o hello, then run ./hello.

Circle every line where your prediction and the output disagree. Those are the only ones worth talking about.

The middle line is the one A1 Part B cannot be done without.

2 Predict, then run — round 2

Same rule: write your prediction down first. These two are shorter than they look, and for each one there are two things to predict — what it prints, and whether it compiles at all.

int x = 2.7;
printf("%d\n", x);
int n = 1;
{
    int n = 2;
    printf("%d\n", n);
}
printf("%d\n", n);

Put each one inside main, compile, and run it. Read everything gcc prints, including nothing at all — whether the compiler stayed quiet is part of the result here, not a detail.

3 seconds.c — the first three lines

This is the start of A1 Part B. We do the first piece together; the rest is yours.

Make a new file, seconds.c, and give it exactly one number to work from:

#include <stdio.h>

int main(void) {
    int total = 10000;

    // your work goes here

    return 0;
}

Get it to print only the whole hours, like this:

10000 seconds is 2 h

Then stop and compare with the person next to you. If you both got 2, you are ready for the minutes — which is homework.

One number lives at the top. If you find yourself typing 10000 a second time anywhere, that is the thing the assignment is asking you not to do.