I am having a little trouble with my mode function. I need to add something so that if none of the numbers occur more than once, it will print "There is no mode". I am unsure of where to add this. I tried putting it right after my count reset but it didn't help. I would really appreciate any help. I finished the assignment besides this.
// Unit 9 modularized.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include <iomanip>
usingnamespace std;
// Declare functions.
int askStudents();
int* askMovies(int getStudents);
int* sortMovies(int* askMovies);
float getAverage(int* setMovies, int getStudents);
double getMedian(int getStudents, int* setMovies);
int getMode(int getStudents, int* setMovies);
//Declare variables.
int getStudents;
int* setMovies;
int main()
{
getStudents = askStudents();
setMovies = askMovies(getStudents);
sortMovies(setMovies);
getMedian(getStudents, setMovies);
getAverage(setMovies, getStudents);
getMode(getStudents, setMovies);
return 0;
}
int askStudents()
{
cout << "How many students were surveyed? ";
cin >> getStudents;
while (getStudents < 0)
{
cout << "Please enter a non-negative number: ";
cin >> getStudents;
}
return getStudents;
}
int* askMovies(int getStudents)
{
// Creating array "setMovies" with size of getStudents using pointer.
int *setMovies;
setMovies = newint[getStudents];
// Storing the amount of movies each student watched into setMovies array.
for (int i = 0; i < getStudents; i++)
{
cout << "How many movies did each student watch: ";
cin >> setMovies[i];
while (setMovies[i] < 0)
{
cout << "Please enter a non-negative number: ";
cin >> setMovies[i];
}
}
// Printint setMovies for test
for (int i = 0; i < getStudents; i++)
cout << setMovies[i];
cout << "\n";
return setMovies;
}
int* sortMovies(int* setMovies)
{
for (int i = 0; i < getStudents; i++)
{
for (int j = i + 1; j <getStudents; j++)
{
if (setMovies[i]>setMovies[j])
{
int temp = setMovies[i];
setMovies[i] = setMovies[j];
setMovies[j] = temp;
}
}
}
for (int i = 0; i < getStudents; i++)
cout << setMovies[i];
cout << "\n";
return setMovies;
}
float getAverage(int* setMovies, int getStudents)
{
float sum = 0.0;
for (int i = 0; i < getStudents; i++)
{
sum += setMovies[i];
}
float average = sum / getStudents;
cout << "The average is: " << average << endl;
return average;
}
double getMedian(int getStudents, int* setMovies)
{
double median;
if (getStudents % 2 == 0)
{
median = (setMovies[(getStudents - 1) / 2] + setMovies[(getStudents / 2)]) / 2.0;
cout << "The median is: " << median << endl;
}
else
{
median = setMovies[(getStudents / 2)];
cout << "The median is: " << median << endl;
}
return median;
}
int getMode(int getStudents, int* setMovies)
{
int number = setMovies[0];
int count = 1;
int countTwo = 1;
int mode = number;
for (int i = 1; i < getStudents; i++)
{
if (setMovies[i] == number)
{
++count;
if (count > countTwo)
{
countTwo = count; // mode is the biggest ocurrences
mode = number;
}
}
else
{ // now this is a different number
if (count > countTwo)
{
countTwo = count; // mode is the biggest ocurrences
mode = number;
}
count = 1; // reset count for the new number
number = setMovies[i];
}
}
cout << "The mode is: " << mode << endl;
return mode;
}
There is never "no mode", @stormbot. It would lead to hopeless inconsistencies. What if there was only only one item of data? That item only occurs once ... is it the mode or not?
There may, on the other hand, be many modes. It's quite common to say something is "bimodal", for example. What do you think is "the" mode of, say,
3,3,3,9,9,9
I would say that this had modes 3 and 9; in other words, every member of the dataset is a mode. So, for consistency again, why not say that the dataset 5,5,5,5 has mode 5, rather than "no mode".
Mode is the only one of mean, median, mode that can actually be more than one value. It's also the only one which is guaranteed to take a (or several) value(s) which is/are actually in the data, as well as the only one which is defined for non-numeric data.
You may save a lot of difficulties for yourself if you used a vector<int> for your container, rather than a dynamically-allocated int[] array. You would also be able to return a collection of modes if there is more than one.
Ways of finding a mode: either find the number (or numbers) with the longest common run in your sorted data or, without needing to do any sorting yourself, use a map<int,int> to construct a frequency table and find the value (or values) with the highest frequency. Either method will work for sortable types of data (like ints); more of a sledgehammer job will be needed if you have data for which a less-than function hasn't been defined (like structs).