# Working out the mode of a set of values

• 02-05-2002
Auz
Working out the mode of a set of values
Can anyone please give me any assistance in how to work out the mode of a set of values entered by the user.

Many Thanks

:D
• 02-05-2002
Auz
Mode as in the value that occurs most often in a set of data.

i.e. 1,2,3,4,5,6,2,1,1, Mode = 1
• 02-05-2002
Auz
many Thanks
• 02-05-2002
Auz

Here is my program, can someone please put the relevant coding (to work out the mode) into the correct place on my program for me.

Sorry if this is being cheeky !

#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>

//Function declarations
void BubbleSort (float[], int); // Sorts the array.
void goswap (float&, float&); // Swaps two adjacent numbers in the array.

//Main Program
void main ()
{
const int ArrayS = 50; // Determines the array size, i.e. to 50.
int i; // A loop control variable.
int count;
float max, min; // Variables which hold results obtained from the
float range, mean, median; // Entered values
int MedianArray; // Variable to assist in the calculation of the median.
float sum; // Sum of the data.
float Stat[ArrayS]; // The array that the numbers are entered into.
clrscr();
do
{
cout <<"Enter how many values you would like to use and press Return (Max of " << ArrayS << "): ";
cin >> count;
if (count<=ArrayS&&count>0)
break;
cout << "The amount of values must be less than " << ArrayS << " numbers." << endl << endl;
} while (count>=ArrayS||count<=0);
for (i=0; i<count; i++)
{
cout <<" Enter value number " << (i+1) << " :- ";
cin >> Stat[i];
}
BubbleSort (Stat, count);
max = Stat[count-1];
min = Stat[0];
range = max - min;
sum = 0; // initialise sum
for (i=0; i<count; i++)
{
sum+= Stat[i];
}
mean = sum/count; // calculates the mean value
if (fmod(count, 2) == 1)
{
MedianArray = (count-1)/2;
median = (Stat[MedianArray]);
}
else
{
MedianArray = count/2;
median = ((Stat[MedianArray] + Stat[MedianArray-1])/2); // calculates the median value
}
cout << endl;
cout << "********************************************* * " << endl;
cout << " YOU ENTERED " << count << " VALUES." << endl;
cout << " " << endl;
cout << " The minimum value was: " << min << endl;
cout << " The maximum value was: " << max << endl;
cout << " The mean value was: " << mean << endl;
cout << " The median value was: " << median << endl;
cout << " The range of the values was: " << range << endl;
getchar();
return;
}

// Sorting Functions
void BubbleSort (float array_to_sort[], int size_of_array)
{
enum boolean {FALSE, TRUE} sorted;
do
{
sorted = TRUE;
for (int i=0; i<size_of_array-1; i++)
{
if (array_to_sort[i] > array_to_sort[i+1])
{
goswap(array_to_sort[i], array_to_sort[i+1]);
sorted = FALSE;
}
}
} while (sorted==FALSE);
return;
}// End Function BubbleSort.

void goswap (float& a, float& b)
{
float temp;
temp = a;
a = b;
b = temp;
return;
}

If you copy and paste this coding into a work document or a C++ blank template you will see the correct layout !