Yesterday I had to complete a programming exercise as part of a research internship I will be starting Nov. 2nd. I had to create a simple C++ program which would take the mean, median, and mode of a set of numbers of unknown length. I was free to implement it any which way, with the exception of using arithmetic libraries to do my mean, median, and mode computations.
I chose to retrieve data from a file into a Vector of integers. The vector is then passed to the 3 functions to perform their corresponding operation. The result is returned and printed.
The mode function sorts the vector and iterates through it keeping a count of how many times the current value appears. Since the array is in sorted order, we know that if the next value is greater than the current, then we must be done counting the current, so it can be added to a temporary vector. If a new value is found, which has a greater count than the current highest, then the vector is cleared and the new value is added. If the new value has a count equal to that of the current high count, it is added to the temporary vector. The temporary vector is returned at the end.
Function Descriptions:
The median function check to see if the vector has an even number of items. If it does, then the median is computed as the average of the two medians of the vector in sorted order. Otherwise, the median is the result of the middle value. The return type for this function is float because of the need to represent decimals in cases when the average must be taken.
The mean function iterates through the vector taking the sum of all the values, then divides them by the total number of items in the vector. The return type is a double for large numbers which require greater precision than a float can provide.
Header File:
#include <iostream> #include <vector> #include <iterator> #include <algorithm> #include <fstream> using namespace std; template <typename T> vector<int> mode(vector<T> &data); template <typename T> float median(vector<T> &data); template <typename T> double mean(vector<T> &data);
Main:
#include "mean_median_mode.h"
int main(int argc, char *argv[]) {
// Function declaration
vector<int> data;
// Load a small set of variables into the vector
int input_int;
ifstream infile(argv[1]);
if(!infile) {
if(argv[1] != NULL) {
// There was an error opening the file
cout << "There was an error opening "
<< argv[1]
<< " for reading."
<< endl;
}
else {
cout << "Syntax error. mean_median_mode <input file name>" << endl;
}
}
else {
while (infile >> input_int) {
data.push_back(input_int);
}
sort(data.begin(), data.end());
cout << "Data Set: ";
int j;
for (j = 0; j < data.size(); j++) {
printf("%i ", data[j]);
}
cout << endl;
printf("Mean: %f Median: %f, Mode: ", mean(data), median(data));
vector<int> tmp = mode(data);
int i;
for(i = 0; i < tmp.size(); i++) {
printf("%i ", tmp[i]);
}
printf("\n");
}
return 0;
}
template <typename T> vector<int> mode(vector<T> &data) {
vector<int> tmp_vector;
if(data.size() > 0) {
sort(data.begin(), data.end());
vector<int>::iterator i;
i = data.begin();
int highest_mode = 0;
int highest_mode_count = 0;
int current_mode = 0;
int current_count = 0;
// Iterate through the vector
while(i != data.end()) {
int tmp = *i;
if(current_count == 0) {
current_mode = tmp;
}
if(tmp == current_mode) {
current_count++;
}
else if(tmp > current_mode) {
// Check if the current mode is greater than the highest
if(current_count > highest_mode_count) {
// Make the current mode the highest
highest_mode = current_mode;
highest_mode_count = current_count;
// Clear the vector
tmp_vector.clear();
// Add the highest value to the vector
tmp_vector.push_back(highest_mode);
// Set current to tmp
current_mode = tmp;
current_count = 1;
}
// In case multiple modes
else if(current_count == highest_mode_count) {
// Set the highest mode to current
highest_mode = current_mode;
highest_mode_count = current_count;
// Add the current mode to the vector
tmp_vector.push_back(current_mode);
current_mode = tmp;
current_count = 1;
}
else {
// Set tmp to current
current_mode = tmp;
current_count = 1;
}
}
else {
// Shouldn't need to do anything if tmp < current_mode
}
i++;
}
}
return tmp_vector;
}
template <typename T> float median(vector<T> &data) {
if(data.size() > 0) {
// Sort the data
sort(data.begin(), data.end());
// Handle the even cases
if((data.size() % 2) == 0) {
// To get the median value sum the 2 medians and get the average
float sum = 0;
sum += data[data.size()/2];
sum += data[(data.size()/2) - 1];
return (sum/2);
}
// Handle the odd cases
else {
return data[data.size()/2];
}
}
return 0;
}
template <typename T> double mean(vector<T> &data) {
if(data.size() > 0) {
vector<int>::iterator i;
double sum = 0;
// Iterate through all of the elements in the vector and sum them
for(i = data.begin(); i != data.end(); i++) {
sum += *i;
}
return sum/data.size();
}
return 0;
}
Makefile:
all: mean_median_mode mean_median_mode: mean_median_mode.h mean_median_mode.cpp g++ -o mean_median_mode mean_median_mode.cpp clean: rm mean_median_mode

Nice. I’m going to keep using this same exercise, so if future test takers are smart, they’ll find this solution via Google.
For what it’s worth, your solution is probably the best C++ version anyone has done, because no one else thought to use vectors.
Thanks Curtis. I suppose this makes it super easy for them though.
Hi there just wanted to give you a quick heads up. The words in your post seem to be running off the screen in Opera. I’m not sure if this is a format issue or something to do with internet browser compatibility but I thought I’d post to let you know. The design look great though! Hope you get the problem fixed soon. Kudos
Hello! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no data backup. Do you have any solutions to prevent hackers?
Hello, is your website having any difficulties recently? I had to refresh the web page about 7 times till the 503 error went away and I was able to look at this post!