Handout: Lecture 2 In-Class Exercises¶
Work through these during lecture, at the exercise breaks. They build up in order:
first the loops, then switch, then functions, and finally a small
calculator that we package across multiple files with a Makefile.
Try each one yourself first - we'll discuss solutions in class, and they're written up in the lecture notes afterward. Compile everything with warnings on:
Set up¶
Make a directory for today's work and cd into it:
Part A - Loops (for vs while vs do/while)¶
Exercise 1 - Sum 1..n, three ways¶
Read an integer n, then compute the sum 1 + 2 + ... + n three times: once
with a for loop, once with a while loop, and once with a do/while loop.
Print all three results on one line.
- Run it with
n = 5. Do all three agree? - Now run it with
n = 0. Do they still agree? If one is different, work out why - it's the whole point of the exercise.
Exercise 2 - Input validation¶
Keep prompting Enter a positive number: until the user actually types a number
greater than 0. Then print it.
- Which of the three loops lets you write the prompt once without repeating it?
Enter a positive number: -3
Enter a positive number: 0
Enter a positive number: 7
Thanks! You entered 7
Do at Home - Multiplication table (stretch)¶
Read an integer n and print its times table from n x 1 to n x 10, one line
per row. Pick whichever loop reads most naturally - be ready to say why.
Part B - switch¶
Exercise 4 - A menu with switch¶
Print a short menu, read the user's numeric choice, and use a switch to act on
it. Use default to handle anything that isn't a valid option.
Exercise 5 - Vowel check (deliberate fall-through)¶
Read a single character and use a switch to report whether it's a vowel. Group
the vowel cases so they share one body (intentional fall-through); let default
handle everything else.
Part C - Functions¶
Exercise 6 - Celsius -> Fahrenheit, as a function¶
Refactor Lecture 1's temperature converter: write a function
put its prototype near the top, its definition below main, and call
it from main. Read a Celsius value and print the Fahrenheit equivalent to one
decimal place.
Part D - The Calculator¶
We'll grow one program across the next three exercises: first a single file, then split across files, then driven by a Makefile.
Exercise 8 - Calculator in one file (functions + switch)¶
Write four functions - add, sub, mul, divide, each int (int, int). In
main, read two integers and an operator character, then use a switch to call
the matching function and print the result.
- Read the input with
scanf("%d %c %d", &x, &op, &y);. - Guard against division by zero before dividing.
- Use
defaultfor an unrecognized operator.
Exercise 9 - Split the calculator into files¶
Take your single-file calculator and split it into three files:
mathops.h- the four function prototypes, wrapped in an include guard (#ifndef / #define / #endif).mathops.c-#include "mathops.h"and the four definitions.main.c-#include <stdio.h>and#include "mathops.h", plusmain.
Build it by hand: compile each .c to a .o, then link the objects:
- For fun, try linking
main.oalone (clang main.o -o calc) and read the error. What is the linker complaining about?
Exercise 10 - Automate with a Makefile¶
Write a Makefile for the split calculator. It should:
- define variables
CC = clangandCFLAGS = -Wall -Wextra -std=c17, - have a
calctarget that links the two object files, - have a rule for each
.o(listmathops.has a prerequisite), - have a
cleantarget that removescalcand the.ofiles.
Recipe lines must start with a real TAB, not spaces.
Then:
- Run
make, then./calc. - Edit only
mathops.c, runmakeagain, and watch which files get recompiled - and which don't. Why? - Run
make clean.
Stretch - take it further¶
- Add a modulo operator (
%) to the calculator (guard the zero divisor). - Wrap
mainin a loop so the calculator keeps reading expressions until the user entersqas the operator. - Add a
run: calctarget to theMakefilesomake runbuilds and runs.
Extra Practice¶
A small number toolkit¶
Write and test these three functions (prototypes up top, definitions below
main). Each is a short loop wrapped in a function:
int factorial(int n); /* 5 -> 120 */
int is_prime(int n); /* 7 -> 1, 8 -> 0 */
int gcd(int a, int b); /* 48, 36 -> 12 (Euclid's algorithm) */
In main, call each one and print the results.