finding mode function and returns a vector

Ok guys. I have an assignment and I think I'm almost there for the most part. I'm just looking maybe for some guidance or advice on what I can do next. My code below is what I have so far. I need to use the function to take an array from main and find the mode of the values in the array. I need to be able to output several numbers if they are tied for frequency and they need to be in ascending order. I'm fairly new to programming and am having a hard time with this assignment but this is what I have so far. Any help or advice would be appreciated. thank you!

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> findMode(int intArray[], int size); //function prototype
vector<int> result; //vector for result of mode


int main()
{
const int ARRAY_SIZE = 8;
int myArray[ARRAY_SIZE] = {6,3,4,4,2,1,4,7}; //random numbers I threw in
int count;

findMode(myArray, ARRAY_SIZE);
std::vector<int> results = findMode(myArray, ARRAY_SIZE);


return 0;
}

/**********************************************
* Find mode *
* This function takes an array as an argument *
* and returns the mode to vector result *
**********************************************/

vector<int> findMode(int array[], int size)
{
//vector<int> mode;

int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<size; i++)
{
if (array[i] == number)
{ // count occurrences of the current number
countMode++;
}
else
{ // now this is a different number
if (count > countMode)
{
countMode = count; // mode is the biggest ocurrences
mode = number;
}
count = 1; // reset count for the new number
number = array[i];
}
}

cout << "mode: " << mode << endl;

}
closed account (48T7M4Gy)
OK!

First step: please put code tags around your code. See the <> symbol in the toolbox on the right hand side of your code.

Also you should fix up the return type etc with your function findMode. And there is a conflict with the name mode.
Last edited on
closed account (48T7M4Gy)
Funny!! Great minds think alike by the look of it.
http://stackoverflow.com/questions/19920542/c-calculating-the-mode-of-a-sorted-array
http://www.cplusplus.com/forum/beginner/178449/
Last edited on
Topic archived. No new replies allowed.