CMSC104, Summer 2004
Programming Project 4
Age Statistics
Out: Thursday, July 15, 2004
Due: Saturday, July 24, 2004 before midnight
NOTE: The extra credit parts are now 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 Task
You have just recently attended an Eminem concert. You noticed
that there was a quite diverse group of people attending the
concert. Out of curiosity 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.
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
You should use the following function prototype:
void GenerateAgeGroupTable(int ageList[], int numberOfPeople);
This prototype 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.
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.
When you run your program, use the following command:
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/d/b/dblock/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
Eminem Concert Age Statistics Report
There were 17 people that attended the concert.
Their ages were:
7 12 12
35 20 20
18 33 33
21 19 19
17 18 18
19 8
Breakdown by Age Group:
15 and under: 4
16 to 20: 6
21 to 30: 1
31 to 40: 3
40 and above: 3
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'.
submit cs104 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.
submitls cs104 Proj4