CMSC104 Spring 2014
Programming Project 3
Body Mass Index
Out: Wednesday, May 7
Due: Friday, May 16, before 11:59 p.m.
The Objective
This project is designed to exercise all your C programming knowledge, with emphasis on file I/O, arrays, and functions.
The Background
The Body Mass Index (BMI) is a standard measure of relative weight and is used to classify individuals as underweight, normal, overweight, or obese. The BMI is a simple function of height (inches) and weight (pounds):
bmi = ( weight / height2 ) * 730
A 1993 growth survey in Hong Kong recorded the heights and weights of 25,000 children under the age of 18. The file proj3.dat contains 200 of the 25,000 height and weight records; the first line of the file indicates the number of records (200).
You can download proj3.dat to your Linux account using the following command:
linux3[10]% wget http://userpages.umbc.edu/~cmarron/proj3.dat
Your program must read the data file; compute the BMI for each of the 200 height and weight records; compute some basic statistics for heigh, weight, and BMI; write the statistics to an output file; write the height, weight, and BMI records to the output file.
The Task
Your program must accept the name of the input and output files on the command line, read the data from the input file (proj3.dat), compute the bmi values and statistics, and write the statistics and data to the output file.
- The first command line argument must be the input file name and the second command line argument must be the output file name.
- The program must read the data from the input file into two float arrays. A third float array will be used to store the computed BMI.
- The the program must compute the mean and median for each of the three variables (height, weight, and BMI). You may use any of the functions from grade_stats.c (see Lecture 20, sample code).
- The program must write the statistics and data to the specified output file.
Your program must include the functions with the following prototypes:
- int read_data( char[], float [], float[] ); - This function reads the input file (1st argument) and stores the height and weight data in two float arrays (2nd and 3rd argument). The function returns the number of data lines in the input file. If the attempt to open the input file fails, it must print an error message and return 0.
- float compute_bmi( float, float ); - This function computes the BMI from the height (1st argument) and weight (2nd argument). The function returns the BMI.
More Details
- You must use the above function prototypes EXACTLY as they have been given to you. You may not change them in any way.
- The program must check that the user has specified two command line arguments - the input and output files. If not, it should print an error message and return 1.
- The program must check the return value of the fopen() function. If an error occurs when opening a file, the program must print an error message and exit.
- You must use a constant to define the maximum number of data lines that your program can handle and you must use this constant in the declaration of your arrays.
- The main function must check the return value of read_data(). If it is 0 or negative, the program should print an error message and return 1.
- If an error occurs when opening the output file, the program must print an error message and return 1.
Hints
The program grade_stats.c (Lecture 20, sample code) is a good example of how to write this program -- some of the fuctions can even be copied verbatim (mean(), median(), and sort()). The function read_data() is very similar to read_scores() in grade_stats.c. Homework 5 shows you how to read the data size from the first line of the input file and the use of a defined constant to specify the maximum number of data lines.
Sample Output
linux3[1]% gcc -ansi -Wall proj3.c linux3[2]% ./a.out proj3.dat proj3.report linux3[3]% cat proj3.report BMI Calculator Report Height mean = 67.95 median = 67.93 Weight mean = 127.22 median = 127.88 BMI mean = 20.10 median = 20.01 Height Weight BMI 65.78 112.99 19.06 71.52 136.49 19.48 69.40 153.03 23.19 68.22 142.34 22.33 67.79 144.30 22.92 68.70 123.30 19.07 69.80 141.49 21.20 70.01 136.46 20.32 67.90 112.37 17.79 66.78 120.67 19.75 [...190 lines omitted...] linux3[4]% ./a.out file_does_not_exist proj3.report Error - unable to open input file. linux3[5]% ./a.out only_one_file Error - incorrect command line arguments.
Submitting the Program
Here is a sample submission command:
linux3[4]% submit cmsc104_cmarron proj3 proj3.c
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show the file that you
submitted in a format similar to the Unix ls
command.
linux3[5]% submitls cmsc104_cmarron proj3