CS21 Homework 9
Due Sunday, November 2 by 11:30pm


Introduction

In this assignment you will practice using pointers. You will need to dynamically allocate an array and you will need to use the pass by reference method. This program is based on programming exercises 13.3 and 13.6 from our textbook The Art and Science of C. You should work on your own for this assignment.


Tabulating Number Frequencies

Your program will ask the user to enter a list of numbers and will then tabulate and display the frequencies of the numbers. The difference between this problem and a more traditional histogram problem lies in the fact that you don't know the range of input values in advance. In one case the numbers may lie between 0 and 100, but in another case they may lie between 600 and 625.

Because of this constraint, you cannot declare an array to count the frequencies until after the numbers have been entered. You should solve this problem by first calculating the range of the data (the minimum and maximum) and then dynamically allocating an array that contains the appropriate number of elements to count the frequencies. More detail on how to do this is provided below.

In the first sample run, the input data is between 0 and 100.

This program calculates the frequency of numbers entered.
Enter a series of integers (-1 to quit).
 ? 79
 ? 0
 ? 100
 ? 80
 ? 86
 ? 91
 ? 90
 ? 86
 ? 80
 ? 79
 ? 80
 ? -1
Frequencies
  0: 1
 79: 2
 80: 3
 86: 2
 90: 1
 91: 1
100: 1

In the second sample run, the input data is between 600 and 625.

This program calculates the frequency of numbers entered.
Enter a series of integers (-1 to quit).
 ? 600
 ? 622
 ? 625
 ? 621
 ? 601
 ? 606
 ? 622
 ? 601
 ? 622
 ? -1
Frequencies
600: 1
601: 2
606: 1
621: 1
622: 3
625: 1


Program Requirements


Enhancement

Do not attempt this portion of the program until the required portion has been thoroughly tested and is working properly. This portion is optional, but if you have the time this will give you some additional practice to help you prepare for the upcoming exam. I will go over the solution to this problem in detail next week.

Now that we know how to dynamically allocate memory, we should replace the old GetIntegerArray with a new function that will try to avoid wasting space by allocating memory to try to better match the user's needs. Write a function that has the following prototype:

int *GetDynamicIntegerArray(int *effectiveSize, int sentinel);
This function passes the effectiveSize by reference and returns a pointer to a dynamically allocated array. This function should work as follows:

Below is a sample run of the enhanced version of the program. I have added a display to show when the array is being enlarged to handle additional data.

This program calculates the frequency of numbers entered.
Enter a series of integers (-1 to quit).
 ? 90
 ? 91
 ? 95
 ? 91
 ? 92
 ? 89
Enlarging array size to 10
 ? 100
 ? 99
 ? 93
 ? 91
 ? 92
Enlarging array size to 20
 ? 94
 ? -1
Frequencies
 89: 1
 90: 1
 91: 3
 92: 2
 93: 1
 94: 1
 95: 1
 99: 1
100: 1
In this example, the actual array size was 20 and the effective size was 12. So we are wasting very little space.

Handing in your solution

Use cs21handin to turn in your program.