Your task is to create a program that will analyze the ages of guests for an event. As input, you will get an unknown number of age values until the user enters 0 as the age. At that point the input will stop and the output statistics will be printed.
Grouping requirements:
- Everyone below 18 is a minor
- Ages 18 and beyond will be put grouped with a step of 5 years (e.g. 18 – 22, 23 – 27 etc.)
- Everyone whose age is above the previously defined groups will be grouped as elders.
Data structure
The counters for each group will be created as an array, where each array member is for one of the predefined groups. The size of the array is defined with the macro GROUPS .
- Index GROUPS - 1 is for elders
- Index GROUPS - 2 is for minors
- Indexes from 0 to
GROUPS - 3 are used for the rest of the age groups.
I.e. index 0 is for ages 18 – 22; index 1 is for 23 – 27 etc.
Program requirements:
- Grouping settings must be easily configurable by only adjusting macros.
- Group count
- Group stepping
- Upper bound for minor group
- The rest of the code must automatically adjust itself if any of the macros were altered.
Required functions
For this task, you should create at least 3 functions
- To validate that the user input is within allowed and reasonable bounds
- To find the group index that the age belongs to. Group index must be returned by this function and stored into idx variable
- To print the results
You can also make a 4th function to make the program even nicer by moving the infinite loop from main into a separate function handling the processing.
Program structure
We are giving you the generic structure for the program with the macros already in place. You need to use them in the final code.
The places where you should call your functions (the ones listed by “required functions” section) are highlighted as comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <stdio.h> /* Number of age groups in total */ #define GROUPS 11 /* Array position for minors group */ #define GROUP_MINOR (GROUPS - 2) /* Array position for elders group */ #define GROUP_ELDER (GROUPS - 1) /* Group size*/ #define GROUP_STEP 5 /* Upper bound for minors */ #define MINOR_UPPER_BOUND 17 /* Calculates lower bound for elders */ #define ELDER_LOWER_BOUND (MINOR_UPPER_BOUND + (GROUP_STEP * (GROUPS - 2))) int main(void) { int ageGroups[GROUPS] = {0}; int idx; /* Infinite loop for input*/ while (1) { /* Grab user input (call your function!) */ /* Check for break condition (age 0)*/ /* Determine the index (call your function!) */ /* idx = YourFunctionName()*/ /* Add to the age group counter */ ageGroups[idx]++; } /* Pint the results (Call your function!) */ return 0; } |
Algorithm
We have modeled most of the algorithm for you that you need to implement. Some of the calculations we have left for you to figure out – e.g. how to find the end points for age ranges in the output and a little bit of the array indexing as well.
Sample
Here is a sample of the program and how it behaves. You can also see a debugging output – it is useful to sanity check yourself. You can print out even more data just to be sure. In this case it helps to identify which counter (array slot) was increased. In the final version you do not need to include the counter, but for development it is highly recommended.
NB! When testing that your code works, try to also change the GROUPS and GROUP_STEP macros and see if everything still calculates correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
This program categorizes participants of the event into age brackets! To stop entry and show statistics, enter 0 or a negative number at any point! Enter participants age: 1 DEBUG: idx = 9 Enter participants age: 15 DEBUG: idx = 9 Enter participants age: 17 DEBUG: idx = 9 Enter participants age: 18 DEBUG: idx = 0 Enter participants age: 22 DEBUG: idx = 0 Enter participants age: 23 DEBUG: idx = 1 Enter participants age: 45 DEBUG: idx = 5 Enter participants age: 62 DEBUG: idx = 8 Enter participants age: 63 DEBUG: idx = 10 Enter participants age: 99 DEBUG: idx = 10 Enter participants age: 0 Minors: 3 Ages 18 - 22: 2 Ages 23 - 27: 1 Ages 28 - 32: 0 Ages 33 - 37: 0 Ages 38 - 42: 0 Ages 43 - 47: 1 Ages 48 - 52: 0 Ages 53 - 57: 0 Ages 58 - 62: 1 Elders: 2 |