I'm working on a call monitoring app at the office and along with the other data that is to be processed, my manager wants the app to calculate and print onscreen the statistical mode for the number of phone calls made by each department. The mode is the most common number, so if a dept. has 5 employees, the manager might enter in the following numbers for daily phone totals: 20, 20, 30, 35, 40.(in that set, the mode would be 20, since it is the most frequent number)
The code I develop has to account for more than one mode, for example: 20, 20, 30, 30, 40 (20 and 30 are both modes).
This is the code segment I have so far, it compiles but returns garbage. I'm using dynamic arrays to store the data, since the number of employees varies from dept. to dept. Any assistance would be appreciated!
Code starts here:
============================================
The code I develop has to account for more than one mode, for example: 20, 20, 30, 30, 40 (20 and 30 are both modes).
This is the code segment I have so far, it compiles but returns garbage. I'm using dynamic arrays to store the data, since the number of employees varies from dept. to dept. Any assistance would be appreciated!
Code starts here:
============================================
Code:
//calculates and displays the mode
void calcMode(float array[], int elems)
{
// declare variables
int x, y, i; //loop counters
int sum = 0; //stores the count for each value
int *freq; //dynamic array to store the sums
int hiFreq = 0; //stores the highest number of occurrences
freq = new int [elems];
// determine the frequency of each value in array
for ( x = 0; x < elems; x++ )
{
for ( y = 0; y < elems; y++ )
{
if ( array[x] == array[y] )
{
sum++;
freq[x] = sum;
}
}
}
cout << "Frequency is: " << freq[x] << endl; //MISTAKE: the frequency is not correct
if (hiFreq < freq[x] )
hiFreq = freq[x];
// display the mode
cout << "\nThe mode was counted: " << hiFreq << " times." <<endl //MISTAKE: always returns 0
<< "The mode(s) are: " << endl;
//MISTAKE: no numbers are displayed when the following code is run
for ( i = 0; i < elems; i++ ){
if ( hiFreq == freq[i] )
cout << array[i] << endl;
}
}