CS301 Fall 1998

Homework Assignment #1

Out: Tuesday September 8
Due: Thursday, September 17

This homework will help you refresh your memory in using UNIX and programming in C, since the course will depend on such skills. Organizing your UNIX account in meaningfully-named directories is a useful practice that pays off when working on a long project like this one. This is what I suggest:

In your home account on poobah create a directory and name it CS301. Inside here you will save your course-related work this semester. Inside the CS301 directory create another directory and name it HW1. For each of the problems below create directories within HW1 to contain your solutions - one per problem. Choose good names for these directories.

For each of the problems below write a C program that solves it. Write a header file and an implementation file. Also, have a diver program (named something like driver.c) that includes the header file and demonstrates that your implementation is correct. Comment your code carefully by explaining how the file is to be compiled and run, including what input (if any) it may need. Try to also include a makefile and a README file, if you feel that the latter is needed. Finally, try to use argc and argv[] to control the arguments your program is called.

The "titles" I have put at each problem explain the programming skill I emphasize in them. If you are not familiar with any of the algorithms that you have to implement, check with an algorithms book.

  1. Basic C: Produce an executable named primetest that checks whether some given number X is prime or not. If it is, the program just terminates while, if it is not, prints out two of its factors.
  2. Recursion: Produce an executable named fibo that prints all the fibonacci numbers up to N, where N is given as a parameter (i.e., you call the program as fibo 1000). Use recursion in your solution.
  3. Handling input file: Produce an executable named totallines that takes as input some ascii file and reports the number of lines in the file.
  4. Handling output file: Produce an executable named listing that takes as input some ascii file (e.g., containing a program) and produces another file with the extension ".lst" in which each line of the input file is preceded by its line number.
  5. Memory allocation: Produce an executable named median that takes as input a file containing integers, one per line, and prints out their median. You will need to sort these numbers, storing them in an array. Allocate only as much memory as you need using malloc().

Happy programming!