Trouble with a Nested Loop in a Vector

I have an assignment that is giving me some trouble and I was hoping somebody could help steer me in the right direction. I have to write a function that takes an array as a parameter and returns a vector displaying the mode(s) from the array. What I have below compiles but it doesn't loop through all of the input of the array, only the first integer input is displayed as the mode. Where is my error?

I'm also not sure on one other thing. I listed a constant int of 10 to set the array for testing, however I'm not sure how many values the instructor will use to test the function. Do I need to change anything in my function or am I good to go in that regard?

As you can tell I'm very new to C++/programming in general, thanks in advance for any help!

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 abcArray[], int sizeOfArray)
{
	int maxFrequency = 0;
	vector<int> result;

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

	return result;
}
Last edited on
Use a vector to store the input values also. That way the prof can give you as many as he/she wants. You can pass the vector to findMode also, so that becomes:
vector<int> findMode(vector<int> abcArray)
You're code to find the mode isn't quite right. I think the algorithm you want is:
1. sort abcArray
2. Go through the array and find the maximum frequency. Since the array is sorted, you know that you'll see the duplicates in order.
3. Once you know the maximum frequency, go back through the array a second time, looking for numbers whose frequency is the maximum. Any time you come across one, add it to the result vector.

I appreciate your answers, but could I get another nudge or two? When you say to store the input values in a vector, are you saying remove the const int of 10? Or change the values[MAXNUM] array to a vector? Or neither?

I also thought that I was storing values (currFrequency, maxFrequency) to the result vector already, but am I not?
Change values to vector<int> values.

I also thought that I was storing values (currFrequency, maxFrequency)

You are, but it isn't working. That was your original question.

Lines 51 & 55: why are you checking frequencies in the inner loop? Why are you checking even when abcArray[x] != abcArray[y]?
Line 63: This is inside the outer loop. So you check the first element with the unsorted array. Then you sort the array and check the second element. Then you sort the array again and check the third element....

My advice is to throw away findMode() and rewrite it using the advice I've already given.
Topic archived. No new replies allowed.