Skip to content

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:

clang -Wall -Wextra -std=c17 myprog.c -o myprog

Set up

Make a directory for today's work and cd into it:

mkdir -p ~/cmsc14300/lec02
cd ~/cmsc14300/lec02

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.
Sum 1..n; enter n: 5
for=15 while=15 do-while=15

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.

Multiplication table for: 7
 1 x 7 = 7
 2 x 7 = 14
...
10 x 7 = 70

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.

1) Say hello
2) Say goodbye
3) Quit
Choose: 1
Hello!

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.

Enter a letter: e
'e' is a vowel

Part C - Functions

Exercise 6 - Celsius -> Fahrenheit, as a function

Refactor Lecture 1's temperature converter: write a function

double c_to_f(double c);

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.

Enter temperature in Celsius: 100
100.0 C = 212.0 F

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 default for an unrecognized operator.
Enter: <num> <op> <num>  (op is + - * /): 6 * 7
6 * 7 = 42

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", plus main.

Build it by hand: compile each .c to a .o, then link the objects:

clang -c mathops.c
clang -c main.c
clang main.o mathops.o -o calc
./calc
  • For fun, try linking main.o alone (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 = clang and CFLAGS = -Wall -Wextra -std=c17,
  • have a calc target that links the two object files,
  • have a rule for each .o (list mathops.h as a prerequisite),
  • have a clean target that removes calc and the .o files.

Recipe lines must start with a real TAB, not spaces.

Then:

  • Run make, then ./calc.
  • Edit only mathops.c, run make again, and watch which files get recompiled - and which don't. Why?
  • Run make clean.

Stretch - take it further

  1. Add a modulo operator (%) to the calculator (guard the zero divisor).
  2. Wrap main in a loop so the calculator keeps reading expressions until the user enters q as the operator.
  3. Add a run: calc target to the Makefile so make run builds 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.