array with two ints for the mode

I need help to cout my two modes in my array. I have both 3 and 4 with the equal amount of integers but only 3 is shown as the result. How do I make it show both ints?

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
  #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> findMode(int array[], int size) //method to calculate mode from given array
{
	vector<int> modeVector; //creating vector
	// temp holds int temporaily to perform check
	int temp = 1;
	// max checks temp to count how many times the int appears
	int max = 0;
	int result = array[0];
	for (int i = 0; i < size - 1; i++)
	{
		if (array[i] == array[i + 1]) //comparing numbers to find max mode
		{
			temp++;
			if (temp > max) //counting for max number to be mode
			{
				max = temp;
				result = array[i];
			}
		}
		else
			temp = 1; // reset temp.
	}
	modeVector.push_back(result); //pushing result
	return modeVector;
} 
int main()
{
	int numbers[10] = { 1,2,2,3,3,4,5,4,4,3 }; //array declared
	sort(numbers, numbers + 10);

	cout << "Sorted Array looks like this." << endl;
	for (size_t i = 0; i != 10; ++i)
		cout << numbers[i] << " ";
	
	vector<int> modeVector; //vector creating
	modeVector = findMode(numbers, 10); //calling function
	sort(modeVector.begin(), modeVector.end()); //Sort the Vector
	for (int i = 0; i < modeVector.size(); i++) {
		cout << "\nThe modes calculated from numbers are: " << modeVector[i] << endl; //printing vector mode details.
	}
	return 0;
}
You can do it with two steps:

1. Determine highest count. (Your code kind of does that.)
2. List all values that have count equal to the highest count.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/178451/
Thanks, for the links and advice. It looks like someone else from my class is struggling too.

I tried to rework the loop, but I'm still struggling.

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
#include <iostream>
#include <vector>
#include <algorithm>

//Function for array
std::vector<int> findMode(int array[], int size)
{
	//Takes in array and passes each variable through to calculate the mode
	std::vector<int> modeVector;
	int mode = number;
	int temp = 1;
	int max = 0;
	int result = array[0];
	for (int i = 0; i < size; i++)
	{
		if (array[i] == array[i]) //comparing numbers to find max mode
		{
			temp++;
		}
		else
		{
		 // now this is a different number
			if (temp > max)
			{
				max = temp; // mode is the biggest ocurrences
				mode = number;
			}
			temp = 1; // reset count for the new number
			number = array[i];
		
		}
	}
	modeVector.push_back(result); //pushing result
	return modeVector;
}


int main()
{
	int array[] = { 23, 5, -10, 0, 0, 321, 1, 1, 99, 30 };
	int size = 10;
	std::sort(array, array + size);
	for (int i = 0; i < size; i++)
		std::cout << array[i] << std::endl;
	std::vector<int> modeVector; //vector creating
	modeVector = findMode(array, 10); //calling function
	for (int i = 0; i < modeVector.size(); i++) {
		std::cout << "\nThe modes calculated from numbers are: " << modeVector[i] << std::endl; //printing vector mode details.
	}
	return 0;
}
Last edited on
You cover the case when temp is > max...
What should happen when temp is equal to max?

You shouldn't need the variable mode. Line 33 should not be there. You shouldn't need the variable result.
Last edited on
When temp is equal to max the loop would stop because it has found its biggest occurrence. But what happens when you have two or more that are all equal?

I remove the push_back and my mode returned garbage values.

I really appreciate the help. My homework was already turned in, but I really want to understand the concept.
When temp is equal to max the loop would stop because it has found its biggest occurrence.

But it doesn't stop, does it?


But what happens when you have two or more that are all equal?

You have two or more that are equal... when temp is equal to max.


I remove the push_back and my mode returned garbage values.

You were returning array[0] before for any invocation of the function. You were already returning garbage. There should be no need to push_back after the loop... because your modes should already be in the vector when the loop finishes.




I really appreciate the help. My homework was already turned in, but I really want to understand the concept.


Here's a solution with some horrible pseudo-code:

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
// http://ideone.com/SrvApG
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

//Function for array
std::vector<int> mode(const std::vector<int>& values)
{
    std::vector<int> modes;
    if (values.empty())                 // if values is empty, there is no mode.
        return std::vector<int>();

    if (values.size() == 1)             // if values has size 1, the only element is the mode.
        return std::vector<int>(1, values.front());

    // Begin with a max_run length of 1 with the initial element as the mode.
    modes.push_back(values.front());

    std::size_t max_run_length = 1;

    // let index i be 0.
    std::size_t i = 0;

    // while i is less than the size of the array less 1,
    while (i < values.size() - 1)
    {
        // let run length be 1
        std::size_t current_run_length = 1;

        // while i is a valid index and the current and previous values match, increase the run length.
        while (++i < values.size() && values[i] == values[i - 1])   
            ++current_run_length;

        // if the run length is equal to the longest run length found so far, add the value for this run
        // to the list of mode candidates.
        if (current_run_length == max_run_length)
            modes.push_back(values[i - 1]);

        // else, if run length is greater than the longest run length found so far, remove the current
        // mode candidates, set the maximum run length to the current run length, and add the current
        // run value to the mode candidates.
        else if (current_run_length > max_run_length)
        {
            modes.clear();
            modes.push_back(values[i - 1]);
            max_run_length = current_run_length;
        }
    }

    // return the modes.
    return modes;
}


int main()
{
    int array [] = { 23, 5, -10, 0, 0, 321, 1, 1, 99, 30 };
    int size = 10;
    std::sort(array, array + size);
    for (int i = 0; i < size; i++)
        std::cout << array[i] << std::endl;
    std::vector<int> modeVector; //vector creating
    modeVector = mode(std::vector<int>(std::begin(array), std::end(array))); //calling function

    std::cout << "The modes calculated are:\n";
    for (std::size_t i = 0; i < modeVector.size(); i++)
        std::cout << '\t' << modeVector[i] << '\n'; //printing vector mode details.
}
Topic archived. No new replies allowed.