I am really struggling with how to write functions. If someone could show me how to solve this that would be awesome.
Royal Oak University is undertaking a study of the age distribution of students in various
programs of study. For a specific course, the ages of all registered students have been read from
a file and stored into an array. This array has then been sorted from minimum age to maximum
age. The sorted ages are integer values and the maximum size of this array is 60.
Write the function that accepts as input arguments this sorted age array, and the actual number of
records. The function updates the output arguments which are: an integer array containing each
of the distinct ages (in ascending order) of students; and a corresponding integer array that
contains the number of students of each age. The function will also return the total number of
distinct ages.
As an example, if the input sorted age array is: 17 17 18 19 19 19 22
The function output argument arrays should hold:
17 2
18 1
19 3
22 1
The function would return 4 as the number of distinct ages.
The function prototype is as follows:
int calcDistribution(const int sortedAges[], const int& actualRec,
int distinctAges[], int distribution[]);
Remember that you are only writing the code for the function, NOT for main or any other function.
Note: Your function may not call any other user-defined functions.
Note: This is a calculation function. There should be no cin or cout statements in your code.
(5) a) Complete the contract-style documentation by stating the constraints on the input arguments
and by stating what specific results your function promises.
(20) b) Write the implementation in syntactically correct C++ code for the function definition
int calcDistribution( constint sortedAges[], constint& actualRec,
int distinctAges[], int distribution[] )
{
int count = 0;
int *first = sortedAges;
int *last = sortedAges + actualRec;
while ( first != last )
{
int *start = first;
int value = *first;
for ( ; first != last && *first == value; ++ first );
distinctAges[count] = value;
distribution[count] = first - start;
++count;
}
return ( count );
}