Assignment 2 — Loops, conditionals, and three broken programs
Six small programs that drill if, for and while — one of them reading its input with scanf — then three broken ones the compiler is perfectly happy with, and your account of why it is happy with them.
Due Sep 9, 2026
This is a practice assignment. There is nothing clever in it. Six of the eight steps are short programs of the kind you have already written once in A1, and the point is repetition: if, for and while should be things your fingers do without your thinking about them by the end of the week.
You already know most of this material. In Java and in Python you have written every loop below. C spells them almost exactly the way Java does, and [DIS] 16.3 (or 1.3 if Python is your language) is mostly there to confirm that. Two things are genuinely new: reading input with scanf, which is [DIS] 16.2 and shows up in step 5, and step 7, which is where the assignment actually spends its weight.
You will hand in seven files: countdown.c, evens.c, table.c, sum.c, digits.c, grade.c, and notes.txt. Keep them in one directory and work there all week.
Steps 1 and 2 are the floor — they are what Tuesday’s class assumes you have on screen. Step 7 is where most of the credit is.
Steps 3–6 are the practice. They are worth doing and they are worth the most if you are the person who finds loops slow, but a submission with 1, 2 and 7 done properly beats one with all eight rushed.
The lab machines in Ben Franklin 103 are the guaranteed environment. Compile everything there before you submit, even if you wrote it elsewhere.
Submit early and submit again. Brightspace keeps the latest version, so upload what you have on Friday and replace it on Tuesday.
AI: the course AI policy applies in full. No AI-generated code, and no asking a chatbot to solve these before the deadline. The optional problems at the end say when that changes.
The shapes, for reference
Keep this next to you. It is also on the In Class page for Thursday, which is where we work through the first two together — and which explains why the programs here open with int main() and no return 0; where the book writes int main(void) and ends with one. Either is fine in anything you hand in.
if (x > 10) {
printf("big\n");
} else if (x > 5) {
printf("medium\n");
} else {
printf("small\n");
}
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
while (n > 0) {
n = n / 10;
}Step 5 asks the person running it for its number instead of having one built in. That shape is:
int value;
printf("Enter a number: ");
scanf("%d", &value);The & is not decoration and not optional. [DIS] 16.2 — Input/Output (printf and scanf) is the reading that says what it is for — read that before you write step 5, not after.
One number at the top, same rule as A1. Every program below except step 5 declares its input as a variable on the first line of main and works from that. If changing that one number does not change the whole output correctly, the program is not finished.
Part A — the six programs
countdown.c— awhileloop. Declareint start = 10;and count down to 1, one number per line, then printLiftoff!.Then set
startto3, recompile, and check. Then set it to0and check again.0is the interesting one. Put one line innotes.txtsaying what it printed and whether that is what you wanted.evens.c— aforloop with anifinside it. Declareint limit = 20;and print every number from 1 tolimit, marking the even ones:1 2 even 3 4 even%is how you ask whether a number is even; you met it in A1.table.c— oneforloop inside another. Print the multiplication table from 1 tosize, withint 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 27Use
printf("%3d", ...)to get the columns to line up — the3is how many characters wide to make the number. Getting the newline in the right place is most of this exercise; it goes in the outer loop, not the inner one.sum.c— an accumulator, and a way to check it. Withint n = 100;at the top, add up every number from 1 tonin a loop and print the total.Then print
n * (n + 1) / 2on a second line. Those two numbers should agree.Try
n = 1,n = 0andn = 2. Innotes.txt, say whether they agreed every time, and if one of them did not, which.digits.c— awhileloop that shrinks a number, and your firstscanf. Ask for a number withscanf, then count how many digits it has and print the count. The tool is integer division: dividing by 10 repeatedly until you reach 0, counting as you go.Do not turn the number into a string. This is arithmetic.
Then run it three times and type
90210,7and0. One of those is very likely wrong on your first attempt — say which innotes.txt, and whether you fixed it.Notice what reading the number bought you: three tests and no recompile between them. That is the whole argument for
scanf, and it is why every program above is more annoying to test than this one.grade.c— anif/else ifchain. Withint score = 85;at the top, print the letter grade: 90 and above is A, 80–89 is B, 70–79 is C, 60–69 is D, below 60 is F.Write it as one
if/else if/elsechain, not six separateifstatements.Then test it on
90,89,60and59— the boundaries, not the middles — and put the four lines innotes.txt. If a boundary comes out wrong, that is the normal outcome and fixing it is the exercise.
Part B — three things the compiler allows
This is the part that matters, and it is the part to leave time for.
Compile all three, run all three, and explain all three. Each of these is a complete, legal C program.
gcccompiles every one of them. Two of them would not be accepted by a Java compiler at all.Put each into a scratch file, compile it with
gcc -Wall, run it, and write your answers innotes.txt.- The one-character difference.
#include <stdio.h> int main() { int x = 3; if (x = 5) { printf("x is five\n"); } printf("x is now %d\n", x); }What did it print? Read the
ifline character by character and say what it actually does — there are two things, not one. Would Java have compiled this? Did-Wallsay anything?- The stray semicolon.
#include <stdio.h> int main() { for (int i = 0; i < 5; i++); { printf("hello\n"); } }How many times did
helloappear, and how many times did you expect? Where does the loop body actually end?- The missing braces.
#include <stdio.h> int main() { int x = 1; if (x > 10) printf("big\n"); printf("and bigger\n"); }What printed, and why? If you write Python, say in one sentence why this one is more dangerous for you than for someone who writes Java.
Then, in three or four sentences at the end of
notes.txt: all three of these compile. A1 step 4 was about programs that do not compile. Which of the two kinds is worse to have in a program you are shipping, and why? There is no answer I am looking for; there is an argument I am looking for.Submit all seven files on Brightspace. Compile every
.cfile one last time on a lab machine first. A file that does not compile earns nothing for the parts that would have worked.
Grading
Full credit for six programs that compile and run, and a notes.txt that answers the questions in 1, 4, 5, 6 and 7.
The weight is not spread evenly:
- Step 7 is worth more than steps 1–6 put together. It is the only part of this assignment you could not have written in Java.
- Steps 1 and 2 are the floor. Tuesday’s class starts from them.
- Steps 3–6 are graded on running correctly, and on your notes being honest about the boundary cases.
n = 0in step 4, and typing0into step 5, are supposed to catch you out.
As in A1, written answers are graded on being honest about what you saw, not on being right. “It printed nothing and I do not know why” is worth more than a confident description of something that did not happen.
Presentation
One of you will be asked on Thursday, September 10 to put your files on the projector and walk the class through them — most likely step 7, and what -Wall did or did not say. You will not be told in advance. There are ten of us and about twelve assignments over the term, so plan on presenting about once — twice at the most. See the syllabus on how presentation is graded.
What else to do (optional part)
Ungraded. No points, and skipping them costs nothing.
In
countdown.c, change thewhileto aforloop that does exactly the same thing. Which reads better to you here, and would your answer change if the countdown started at a number you had to compute?Write a loop that never ends, run it, and stop it with
Ctrl-C. Now you have met the reason that key was on the A1 command card.In
evens.c, replaceif (n % 2 == 0)withif (!(n % 2)). Same output? Say in one line what!did, and which version you would rather read six months from now.Give
countdown.cascanftoo, so it asks where to count down from. Then run it and type something that is not a number at all — your name, or nothing but Enter. What did it count down from, and what does that tell you about whatscanfdoes when it fails?Work the Conditionals and Loops and Input/Output sets in Dive Into Systems: Exercises. They run in the browser and mark themselves, so they are the fastest way to find out whether the loops are actually sticking. If step 1 or step 2 fought you, do this before anything else on this list.
After the deadline has passed — and only then, per the AI policy — paste step 7a into a chatbot and ask it whether the program is correct. Does it spot the
=? Write one line on whether it explained the problem or merely reported it.