Need help with an error code.

I don't know what I am doing wrong I keep getting an error that says "findMode.cpp:11: error: expected constructor, destructor, or type conversion before ‘<’ token"

I have to turn this in tonight and my professor's policy is if it doesn't compile it's a zero.

Assignment;
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it's similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: "std::sort(result.begin(), result.end());".

The most straightforward approach is to:

Iterate (loop) through the array to find out what the highest frequency for any value is without worrying about storing any of the values.
Iterate through the array again, this time comparing the counts for each value to the highest frequency that you already found, if the count for a value is the same as the highest frequency, push that value into your results vector.
The file must be named: findMode.cpp


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

using namespace std;

vector<int> findMode(int data[], int size)
{
    int uniqueArray[size];
    int countArray[size];
    int item = data[0];
    int frequency = 0;
    int index = 0;
    for(int i=0; i< size; i++)
    {
        if(item == data[i])
        {
            frequency++;
        }
        else
        {
            uniqueArray[index] = data[i];
            countArray[index] = frequency;
            frequency = 0;
            item = data[i];
        }
    }
    
    
    int max = countArray[0];
    int maxIndex = 0;
    for(int j=1; j< size; j++)
    {
        if(max < countArray[j])
        {
            max = countArray[j];
            maxIndex = j;
        }
    }
    vector<int> v1;
    
    if(countArray[0] == 0 && maxIndex ==0)
    {
        for(int k = 0; k < size; k++)
        {
            v1.push_back(uniqueArray[k]);
        }
    }
    else
    {
        v1.push_back(uniqueArray[maxIndex]);
    }
    for (int i = 0; i < (int)v1.size(); i++)
        cout<< v1.at(i) <<endl;
    return v1;
    
}


int main()
{
    int data [10] = {2,1,1,1,1,1,1,1,1,1};
    findMode(data,10);
    return 0;
}
int uniqueArray[size];

this is allowed only if the 'size' is a compile time constant.

So, in your case, use vectors:
vector<int> uniqueArray(size);
I don't understand. So I need to change all the arrays to vectors?
I tried that, it still gave me the exact same error.
There is no '<' token in line 11. You are doing something wrong. Are you compiling the correct .cpp file?
Yes!
closed account (48T7M4Gy)
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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> findMode(int data[], int size)
{
	int *uniqueArray = new int[size];
	int *countArray = new int[size];
	int item = data[0];
	int frequency = 0;
	int index = 0;
	for (int i = 0; i< size; i++)
	{
		if (item == data[i])
		{
			frequency++;
		}
		else
		{
			uniqueArray[index] = data[i];
			countArray[index] = frequency;
			frequency = 0;
			item = data[i];
		}
	}


	int max = countArray[0];
	int maxIndex = 0;
	for (int j = 1; j< size; j++)
	{
		if (max < countArray[j])
		{
			max = countArray[j];
			maxIndex = j;
		}
	}
	vector<int> v1;

	if (countArray[0] == 0 && maxIndex == 0)
	{
		for (int k = 0; k < size; k++)
		{
			v1.push_back(uniqueArray[k]);
		}
	}
	else
	{
		v1.push_back(uniqueArray[maxIndex]);
	}
	for (int i = 0; i < (int)v1.size(); i++)
		cout << v1.at(i) << endl;
	return v1;

}


int main()
{
	int data[] = { 2,1,1,1,1,1,1,1,1,1 };
	int size = sizeof( data )/sizeof( int );
	
	findMode( data, size );

	return 0;
}
Last edited on
If this is the required declaration: vector<int> findMode( int data[], int size ) (the array may be modified),
sorting the array at the start would make the code simpler.

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

// return the number of consecutive occurrences of the values starting at first
std::size_t frequency_of( const int* first, const int* end )
{
    // pointer to the first element that is greater than the current value
    // http://en.cppreference.com/w/cpp/algorithm/upper_bound
    const int* next = std::upper_bound( first, end, *first ) ;
    return next - first ;
}

// return the the highest frequency for any value in [begin,end)
// invariant: the sequence [begin,end) is sorted in ascending order
std::size_t max_frequency( const int* begin, const int* end )
{
    assert( std::is_sorted(begin,end) ) ; // assert the invariant

    std::size_t max_so_far = 0 ;

    const int* curr = begin ; // start with the first value ;
    while( curr != end )
    {
        const std::size_t frequency_of_this_value = frequency_of( curr, end ) ;
        max_so_far = std::max( max_so_far, frequency_of_this_value ) ;

        curr += frequency_of_this_value ; // repeat for the next value
    }

    return max_so_far ;
}


std::vector<int> find_mode( int data[], std::size_t size )
{
    std::vector<int> result ;
    int* begin = data ; // beginning of the sequence ;
    int* end = data + size ; // end of the sequence ;

    // 1. sort the values in ascending order
    std::sort( begin, end ) ;

    // 2. Iterate (loop) through the array to find out what the highest frequency
    //    for any value is without worrying about storing any of the values.
    const std::size_t frequency_of_mode = max_frequency( begin, end ) ;


    // 3. Iterate through the array again, this time comparing the counts for each value
    // to the highest frequency that you already found, if the count for a value is
    // the same as the highest frequency, push that value into your results vector.
    const int* curr = begin ; // start with the first value ;
    while( curr != end )
    {
        const std::size_t frequency_of_this_value = frequency_of( curr, end ) ;
        if( frequency_of_this_value == frequency_of_mode ) result.push_back(*curr) ;
        curr += frequency_of_this_value ;
    }

    return result ;
}

int main()
{
    const std::size_t N = 20 ;
    int data[N] = { 8, 0, 7, 0, 5, 3, 1, 8, 7, 1, 5, 8, 3, 1, 7, 1, 5, 3, 7, 8 };

    for( int v : find_mode( data, N ) ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/dee4f5b0d3f17fa6
Last edited on
Topic archived. No new replies allowed.