CS 215 – Fundamentals of Programming II
Spring 2008 – Homework 2

10 points


Out: January 16, 2008
Due: January 23, 2008


The purpose of this homework is to continue practicing using the development environment expected in this course. Therefore, please use the UNIX environment and the g++ compiler for this homework, and please type the program in by hand (i.e., do not cut and paste from the web page).


Write a program that accepts exactly one command-line argument that is the name of an input file in file hwk2.cpp. The program must do proper error checking of the number of command line arguments and the file open. The input file will contain a random number of integers. The program is to count the number of integers in the input file and compute the average of the integers in the input file. For example, if the input file contains the following data:


1
2
3
4
5
6


the program should output:


There are 6 numbers in the file
The average of the numbers in the file is 3.5


as the result. If the file is empty (so the count is 0) the output should be:


There are 0 numbers in the file
The average cannot be computed


Also create a (separate) makefile in file Makefile.hwk2 for this program that creates executable file hwk2. Although this program has only one file, separate the compile and link phases in the makefile as shown in the handout Very Basic make.


Electronically submit the source code file (hwk2.cpp) and the makefile (Makefile.hwk2) in a tarfile as explained in the handout Submission Instructions for CS 215. Please note that the automated submission system requires that all files must be named as specified above including the executable program and also requires that the output of the program be exactly as expected including whitespace. Also submit hardcopy printouts of these files.

01/15/08 1 of 1