Homework 18: Multi-file programs
Name: _____________________________________________ Alpha: ___________________
Describe help received: ________________________________________________________
- Due before class on Wednesday, March 1
- This homework contains code to be submitted electronically.
Put your code in a folder called
hw18
and submit using the204sub
command. - This is an electronic homework and you do not need to print out anything to turn in during class.
Assignment
- Required reading: Sections 4 (Recursion) and 5 (The structure of multi-file programs) from the Unit 5 notes,
-
Here is a program with a
#include
statement for a header file that you will complete:
(You can download that file from this link.)#include <stdio.h> #include "countdown.h" int main() { int n; do { printf("Enter n: "); fflush(stdout); } while (scanf(" %d", &n) != 1); // this calls the function YOU have to write! countdown(n); return 0; }
You may not change that program! Instead, you need to make a header file
countdown.h
and a definitions filecountdown.c
that contain (respectively) the prototype and definition for thecountdown
function.The
countdown
function should have the following prototype:
that prints out the numbers fromvoid countdown(int n);
n
down to 1 and then prints out the message "hooray!". For example, callingcountdown(6);
should result in6 5 4 3 2 1 hooray!
You are strongly encouraged to write the
countdown
function recursively! Here is a complete example of compiling and running your program:roche@ubuntu$
gcc countdown.c main.c -o main
roche@ubuntu$
./main
Enter n:
3
3
2
1
hooray!