compile error

I am new to coding and I'm taking a intro course this semester. I'm currently stuck on a homework problem:

-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
- 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.

(a hint is given)
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 modes.
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.

---------------------------------------------------------------------

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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//function prototype header
vector<int> findMode(int[], int);
vector<int> v1();

int main()
{
        int array[] = {1,2,2,2,2};

        findMode(array, 5);

        for(int i=0; i<v1.size; i++)
                cout << v1[i] << "" endl;
        return 0;
}


// function
vector<int> findMode(int arr[], int size)
{
        int element = 0;
        int count=0;
        int tempElement, tempCount;

        // iterating array and finding the highest frequency value
        for(int x=0; x<size; x++)
                int tempElement= arr[x];
                int tempCount = 0;
                for(int p=0+1; p<size; p++)
                        if (arr[p] == tempElement)
                                tempCount++;
                if (tempCount> count)
                {
                        element = tempElement; // highest frequency element
                        count = tempCount;     // highest count for element
                }
        // Declare a vector to push values into
        vector<int>v1;

        // iterating the the array again
        for(int y=0; y<size; y++)
                int tempElement = arr[y];
                int tempCount = 0;
                for(int z=0+1; z<size; z++)
                        if (arr[y] == tempElement)
                                tempCount++;
                // if tempCounts are greater or equals to the count
                // (set from previous for loop)
                // then the push value into the vector v1
                if (tempCount > count || tempCount == count)
                {       count = tempCount;
                       v1.push_back.(arr[y]);
                }

        // sort elements in the vector in ascending order
        sort(v1.begin(), v1.end());

        // returning vector call v1 with the sorted modes
        return v1;


I believe I the logic makes sense but it doesn't compile. I don't understand the error statements and how to fix them. Any help would be appreciated.
error statements:

findMode.cpp: In function ‘std::vector<int, std::allocator<int> > findMode(int*, int)’:
findMode.cpp:8: error: invalid conversion from ‘int*’ to ‘int’
findMode.cpp:9: error: invalid conversion from ‘int*’ to ‘int’
findMode.cpp:17: error: no post-increment operator for type
findMode.cpp:21: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:22: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:23: error: overloaded function with no contextual type information
findMode.cpp:28: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:32: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:34: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:41: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:45: error: invalid types ‘int[int]’ for array subscript
findMode.cpp:50: error: invalid types ‘int[int]’ for array subscript

Sorry for being such a long read
Last edited on
Edit: You should read your the errors your compiler is giving you. They're telling you what's wrong. Here are a few things -

vector<int> v1(); // remove the ()

for(int i=0; i<v1.size; i++) // add (). v1.size()

cout << v1[i] << "" endl; // missing a << between "" and endl

Edit:
In your function, you keep redeclaring int tempElement and int tempCount. You can't have variables with the same name.

Edit 2:

1
2
3
4
5
 for(int y=0; y<size; y++)
                int tempElement = arr[y];
                int tempCount = 0;
                for(int z=0+1; z<size; z++)
                        if (arr[y] == tempElement) // y is out of scope. You can't use it here. 


You'll need to create "y" outside the for-loop to be able to use it in that if statement -
1
2
3
4
5
6
int y=0; // create it here
 for(y=0; y<size; y++)
                int tempElement = arr[y];
                int tempCount = 0;
                for(int z=0+1; z<size; z++)
                        if (arr[y] == tempElement)


Last edited on
You seem to have quite a few "little" syntax errors.
In function 'int main()':
15:27: error: request for member 'size' in 'v1', which is of non-class type 'std::vector<int>()'
16:29: warning: pointer to a function used in arithmetic [-Wpointer-arith]
16:37: error: expected ';' before 'endl'
In function 'std::vector<int> findMode(int*, int)':
30:21: warning: unused variable 'tempElement' [-Wunused-variable]
31:21: error: redeclaration of 'int tempCount'
26:26: note: 'int tempCount' previously declared here
45:21: warning: unused variable 'tempElement' [-Wunused-variable]
46:21: error: redeclaration of 'int tempCount'
26:26: note: 'int tempCount' previously declared here
48:33: error: 'y' was not declared in this scope
55:24: error: 'v1.std::vector<int>::push_back' does not have class type
55:37: error: expected unqualified-id before '(' token
55:42: error: 'y' was not declared in this scope
62:18: error: expected '}' at end of input

Edit: the Numbers in the above error messages are the line number and the location within that line where the problem was detected.


And note the error you posted don't match up with the code you posted.

Why are you using the array in main() instead of a vector and then pass the vector to the functions instead of the array.


Last edited on
Why are you using the array in main() instead of a vector and then pass the vector to the functions instead of the array.


one of the requirements is to pass an array to the function.
one of the requirements is to pass an array to the function.

Then why are you using vectors in the function instead of arrays?

Why are you returning a vector from your function but never using that return value?

Topic archived. No new replies allowed.