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