How to handle when there is no mode?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  // Unit 9 modularized.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include <iomanip>
using namespace 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 = new int[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;


}
Hello stormbot,

In the function "getMode" you start the for loop at 1, but the array is indexed from 0 to max elements -1, i.e., a 10 element array goes from 0 to 9.

Beyond that I will have to look into it in the morning.

Hope that helps,

Andy
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).
Last edited on
Topic archived. No new replies allowed.