SI 204 Spring 2017 / HWs


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 the 204sub command.
  • This is an electronic homework and you do not need to print out anything to turn in during class.

Assignment

  1. Required reading: Sections 4 (Recursion) and 5 (The structure of multi-file programs) from the Unit 5 notes,
  2. Here is a program with a #include statement for a header file that you will complete:
    #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 can download that file from this link.)

    You may not change that program! Instead, you need to make a header file countdown.h and a definitions file countdown.c that contain (respectively) the prototype and definition for the countdown function.

    The countdown function should have the following prototype:

    void countdown(int n);
    that prints out the numbers from n down to 1 and then prints out the message "hooray!". For example, calling countdown(6); should result in
    6
    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!