Calculating Mode, Only First Integer Input Being Displayed

Hello, I'm having trouble with a program that is supposed to read a set of integers input by the user and calculate and display the mode(s). Right now only the first input is being displayed whether or not it is the mode or not. I've been staring and reading materials online for two days now and I simply don't understand... Thanks!

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
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::cout;
using std::cin;
using std::sort;
using std::endl;

vector<int> findMode(int [], int);

int main()
{
	const int MAXNUM = 10;
	int values[MAXNUM];
	int num = 0;
	
	cout << "How many integers would you like to enter? " << endl;
	cin >> num;
	cout << "Please enter " << num << " integers: " << endl;

	for (int x = 0; x < MAXNUM; x++)
	{
		cin >> values[x];
	}
	vector<int> result = findMode(values, num);
	
	cout << "Mode(s): " << endl;

	for (int x = 0; x < result.size(); x++)
	{
		cout << "" << result[x] << endl;
	}
	return 0;
}

vector<int> findMode(int inputArray[], int sizeOfArray)
{
	int maxFrequency = 0;
	vector<int> result;

	for (int x = 0; x < sizeOfArray; x++)
	{
		int currFrequency = 1;
		for (int y = 0; y < sizeOfArray; y++)
		{
			if (inputArray[x] == inputArray[y]);
			{	
				currFrequency++;
			}
			if (currFrequency == maxFrequency)
			{
				result.push_back(inputArray[x]);
			}
			if (currFrequency > maxFrequency)
			{
				maxFrequency = currFrequency;
				result.clear();
				result.push_back(inputArray[x]);
			}
		}
	}
	
         std::sort(result.begin(), result.end());

	return result;
}
Biggest problem: semicolon at the end of line 47 (this ends the 'if' statement and the next block will always run):
if (inputArray[x] == inputArray[y]);
Remove that end semicolon.


Other issues:
- line 22: you should loop for num, not MAXNUM
- line 45: the loop range should start from x+1, or you will multiple-count everything and each mode would appear several times:
for (int y = x+1; y < sizeOfArray; y++)
(However, there is already a large amount of redundancy if you choose this nested-loop approach; you don't really need to consider any x that has already been "counted in" with an earlier one.)
- lines 51 to 60: these latter two if blocks must be inside the other if block (i.e. run only if inputArray[x] == inputArray[y]) - this can be remedied by moving the curly brace on line 50 to after the (current) line 60 (adjusting indentation accordingly).


Also consider:
- using a vector to hold inputArray, not a fixed-size array;
- if you sort inputArray first then you can easily find the mode with a single loop through elements, not a nested loop. (std::sort can work with fixed-size arrays too if you don't want to make it a vector.) Additionally, sorting will be useful if, in future, you wish to find the median.


Also suggest in general - don't stare at it for 2 days. I only found your error by printing out x,y,currFrequency,maxFrequency and looking at what was going on. If it compiles and runs then there will be a logic error that a debugger might not point out: be prepared to PRINT OUT VARIABLES.
Last edited on
I can't thank you enough for taking the time and helping me out, with this problem and with advice in general. Thanks!
Topic archived. No new replies allowed.