Homework: age grouping

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.

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.