Skip to content

CMSC 14300: Practice Set 2 - Loops, switch, Functions & Makefiles

Practice for the material in Week 1.

Work in a fresh directory and compile everything with warnings on:

mkdir -p ~/cmsc14300/lec02-pset && cd ~/cmsc14300/lec02-pset
clang -Wall -Wextra -std=c17 problem1.c -o problem1

If your compiler prints a warning, treat it as something to fix - warnings are the compiler trying to help you.


Problem 1 - Pick the right loop

Read an integer n and print a countdown from n down to 1, then Liftoff!.

  • If n is 0 or negative, print only Liftoff! (no countdown).

Enter n: 3
3
2
1
Liftoff!
Enter n: 0
Liftoff!


Problem 2 - Average until a sentinel

Keep reading integers until the user enters -1. Then print how many numbers were entered (not counting the -1) and their average as a double. If the very first number is -1, print No numbers entered.

Enter numbers (-1 to stop): 10 20 30 -1
Count: 3, Average: 20.00
Enter numbers (-1 to stop): -1
No numbers entered.


Problem 3 - Grade letter with switch

Write a function char letter_grade(int score) that returns a letter for a 0-100 score: 90+ -> A, 80-89 -> B, 70-79 -> C, 60-69 -> D, else F. In main, read a score and print the letter.

Enter score (0-100): 87
Grade: B

Problem 4 - Functions: a number toolkit

Write and test these three functions (prototypes at the top, definitions below main):

int sum_digits(int n);   /* sum of the decimal digits, e.g. 1234 -> 10 */
int count_digits(int n); /* how many digits, e.g. 1234 -> 4          */
int is_palindrome(int n);/* 1 if n reads the same reversed, else 0    */

Assume n >= 0. In main, read an integer and print all three results.

Enter a number: 1234
Digits: 4
Digit sum: 10
Palindrome: 0
Enter a number: 1221
Digits: 4
Digit sum: 6
Palindrome: 1


Problem 5 - Multi-file build + Makefile

Package Problem 4's functions as a reusable module, mirroring the calculator from Lecture 2.

  1. digits.h - prototypes for sum_digits, count_digits, is_palindrome, wrapped in an include guard (IFNDEF).
  2. digits.c - the three definitions, including its own header.
  3. main.c - #include <stdio.h> and #include "digits.h", plus main.
  4. Build it by hand first:
    clang -c digits.c
    clang -c main.c
    clang main.o digits.o -o digits
    
  5. Then write a Makefile (with CC, CFLAGS, a clean target, and TAB indented recipes) so make builds it and make clean removes the artifacts.

  6. Check your understanding: after a successful make, edit only digits.c and run make again. Which files get recompiled? Which don't? Why?


Self-check

You're ready for Quiz 1 material from this week if you can, without looking it up:

  • Explain when while runs zero times but do/while runs once, and why.
  • Say what break does in a switch, and give a use for deliberate fall-through.
  • Name the difference between a function prototype, definition, and call, and explain why C sometimes needs a prototype.
  • Describe the two steps clang takes to turn several .c files into one program (compile -> link), and what an "undefined reference" error means.