CMSC 104, Sect. 0101
Project 4: Age Statistics
Out: Monday, November 24, 2008
Due: Tuesday, December 9, 2008, before 11:59 PM
NOTE: The extra credit parts are in red.
The Objective
This project is designed to give you practice working with arrays and functions. It will also give you practice passing arrays to functions.
The Background
You have just recently attended a "Led Zeppelin: the Final Final Tour" concert. You noticed that there was a quite diverse group of people attending the concert. Out of curiosity (and because you just happen to have a few hundred dollars burning a hole in your pocket), you have decided to attend another concert and pass out a survey asking the attendees to list their ages. Based on their ages, you are going to determine some statistics about the people attending the concert.The Task
You are to store the ages of the people attending the concert in an array. Here is the main function that you MUST use in your program:int main() { int ageList [MAX]; /* Array to store ages of people attending */ int youngestAge = 0; /* Age of youngest person */ int oldestAge = 0; /* Age of oldest person */ int numberOfPeople = 0; /* Total number of people */ float averageAge = 0.0; /* Average age of people attending */ /* Fill array with ages of people attending */ numberOfPeople = FillAgeList (ageList); /* Calculate the average age */ averageAge = ComputeAverageAge (ageList, numberOfPeople); /* Calculate the youngest age */ youngestAge = FindYoungestAge (ageList, numberOfPeople); /* Calculate the oldest age */ oldestAge = FindOldestAge (ageList, numberOfPeople); /* Determine and print age statistics */ ProcessAgeStats (ageList, numberOfPeople, averageAge, youngestAge, oldestAge); return 0; }
More Details
- You MUST use the main() function exactly as it is given. However,
you need to add the following code before main:
- A file header comment
- Any necessary preprocessor directives
- A #define directive to set MAX to 100
- Function prototypes
- You must also add the definitions of the functions after main(). You may
add additional functions if you wish.
- EXTRA CREDIT: In addition to the functions
for which there are calls in main(), you should write a function to
handle calculating and printing the number of people in specific age
groups. The age groups you should consider are:
15 and under 16 to 20 21 to 30 31 to 40 40 and above
Here is the function call you would use:
GenerateAgeGroupTable(ageList, numberOfPeople);
This function call is given because the function needs to be called from within the ProcessAgeStats function. Therefore, there is no call in main() from which you can build the prototype on your own.
- Your output does not have to look exactly like the sample output shown below. For EXTRA CREDIT, you should print the ages in columns of three as shown. You should note that if the age only has 1 digit, it should still line up evenly with the rest of the numbers. If you do not wish to do the extra credit, you can just list the ages in a single column.
Input to the Program
A data file containing the items for you to use as input to your program will be provided. The items are all greater than 0. The last value in the file will be 0. This is the sentinel value that signals the program to stop reading items.
To use the data file as input to your program, you will use Unix redirection.
By using redirection, you will be able to read data from a 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. But 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.
a.out < ages.dat
This is how Unix redirection is done. It is saying to run your executable
file using the file ages.dat
as input.
You will need to copy the file ages.dat
into your directory.
To do this, go to the directory where you would like to store
ages.dat
. Then, use the following command to copy
ages.dat
into the directory:
cp /afs/umbc.edu/users/p/a/park/pub/CS104/Proj4/ages.dat .Notice that the space and period at the end of the command are part of the command.
Here is an example of what the input data file could look like:
7 12 14 35 20 17 18 33 52 21 19 20 17 18 35 19 8 0
You will also find proj4.c
in the directory with
ages.dat
. You can copy it to your own directory
following the above directions.
Sample Output
linux2[34]% gcc -ansi -Wall proj4.c linux2[35]% cat ages.dat 7 12 14 35 20 17 18 33 52 21 19 20 17 18 35 19 8 0 linux2[36]% a.out < ages.dat Led Zeppelin Concert Age Statistics Report There were 17 people that completed the survey. Their ages were: 7 12 14 35 20 17 18 33 52 21 19 20 17 18 35 19 8 Breakdown by age group: 15 and under: 4 16 to 20: 8 21 to 30: 1 31 to 40: 3 40 and above: 1 The average age was: 21.5 The youngest person that attended was: 7 The oldest person that attended was: 52 linux2[37]%
Submitting the Program
Here is a sample submission command. Note that the project name starts with uppercase 'P'.
linux2[38]% submit cs104_0101 Proj4 proj4.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.
linux2[39]% submitls cs104_0101 Proj4