UMBC CMSC 104 Spring 2001 CSEE | 104 | current 104

UMBC CMSC 104 -- Spring 2001

Homework 8

Objectives

This assignment is very similar to Project 6. But this time, you will be reading the student ages into an array and then producing statistics, rather than processing the ages one at a time. You will also be dividing your program into functions. The code for the main function will be supplied (see below). You will need to write the code for the remaining eight functions, their prototypes, and any necessary preprocessor directives (note that this program has two defined constants, AGE and MAX).

Assignment

You have been assigned the task of producing statistics based on the ages of all current CMSC 104 students. Given the ages, you are to determine each of the following pieces of information:

Name the program hw8.c. Use #define preprocessor directives to define the constant AGE as 19 and the constant MAX as 50. A data file containing the ages for you to use as input to your program will be provided. (These ages are obviously not from a survey of all CMSC 104 students because there are too few ages in the file.) You may assume that there is at least one age and no more than 50 ages in the file. The ages, which can be assumed to be positive, non-zero numbers, are in random order. The last value in the file is -1. This is the sentinel value that signals the program to stop reading ages.

To use the data file as input to your program, you will use UNIX redirection. By using redirection, you can have UNIX fill the stdin buffer from a data file rather than from the keyboard. The scanf statement that you use in your program will look exactly the same as it would if you were getting your input from the keyboard. Since you will be getting the values from a file instead of from a user typing at the keyboard, you will not need to prompt the user.

When you run your program, use the following command:

a.out < hw8.dat This is how UNIX redirection is done. It is saying to run your executable file using the file hw8.dat as input. The file, hw8.dat, can be seen by following this link. Within your gl account, bring up Netscape and go to this link. Then under the File menu, choose the Save As option. This will save a copy of the hw8.dat file into your directory so that you can use it as input. DO NOT just cut and paste to capture the text and put it into a file because you may capture some unwanted extra spaces. (Note that this procedure is easiest when done using one of the SGI Workstations rather than a PC.)

Here is an example of what a data file might look like:

18 39 23 18 19 15 28 -1 Note that there may be several students of the same age.

Here is the output that would result from using this sample data file:

CMSC 104 Student Age Analysis There are 7 students enrolled in CMSC 104. The youngest student is 15 years old. The oldest student is 39 years old. The average age is 22 years. The number of students under the age of 19 is 3. The number of students 19 years of age or older is 4.

Your program's output should follow this EXACT format.

Main Function

Here is the main function EXACTLY as it should appear in your program. Do NOT alter it IN ANY WAY. (You can simply "cut and paste" the function from this page into your text editor.)

main() { int ageArray[MAX]; /* Array to hold student ages */ int numStudents; /* Total number of students */ int numOver; /* Number of students over AGE */ int numUnder; /* Number of students under or same as AGE */ int youngest; /* Age of the youngest student */ int oldest; /* Age of the oldest student */ int sumOfAges; /* Sum of all ages */ int averageAge; /* Average of all ages */ /* Read the ages into the array and compute the number of students */ numStudents = readData(ageArray); /* Compute the sum of all of the ages in the array */ sumOfAges = computeSum(ageArray, numStudents); /* Find the age of the youngest student */ youngest = findYoungest(ageArray, numStudents); /* Find the age of the oldest student */ oldest = findOldest(ageArray, numStudents); /* Compute the number of students under AGE years of age */ numUnder = computeNumUnder(ageArray, numStudents); /* Compute the number of students >= AGE years of age */ numOver = computeNumOver(ageArray, numStudents); /* Compute the average age */ averageAge = computeAverage(sumOfAges, numStudents); /* Print the final statistics */ printStats(numStudents, youngest, oldest, averageAge, numUnder, numOver); }

Coding Standards and Indentation

Make sure that you follow the "C Coding Standards" and "Indentation Styles" given on the Projects web page. Your program will be graded not only on whether or not it produces the correct results, but also on whether or not you follow these standards and styles.

Make sure that you follow the "C Coding Standards" and "Indentation Styles". Your program will be graded not only on whether or not it produces the correct results, but also on whether or not you follow these standards and styles.

Algorithm

Include the algorithm for you program within the header of your program. You will not receive full credit if you don't include your algorithm.



6:44 PM 1/23/01