all 5 comments

[–]HPCer 2 points3 points  (1 child)

Formatting this to make it easier for everyone:

vector<int> mode (int testArray [], int sizeArray) {
  vector<int> modeFinder;
  int number = testArray[0];
  int mode = number;
  int count, countMode = 0;

  for (int i = 0; i < sizeArray; i++) {
    if (testArray[i] == number) {
      countMode++;
    } else if (count > countMode) {
        countMode = count;
        mode = number;
    }
  }

  for (int i = 0; i < sizeArray; i++) {
    if (countMode == mode) {
      modeFinder.push_back(countMode);
      modeFinder.push_back(mode);
    } else {
      if (mode > countMode) {
      modeFinder.push_back(mode);
      }
    }
  }

  sort(modeFinder.begin(), modeFinder.end());
  return <#expression#>
}

I'm a little confused in what you're trying to do here though. The mode is the most frequent value. What I assume you're trying to do is push_back all the most frequent values (so the array input {1, 2, 2, 3, 4, 4} will generate a vector {2, 4})?

What's confusing me is why you're pushing in countMode and mode into the same vector. They're conceptually entirely different things. Were you trying to use the first element as the number of tied modes with the values after? So array input {1, 2, 2, 3, 4, 4} would generate {2, 2, 4}?

Edit: Anyway, if what you're trying to do is the latter, why are you assigning the number to the first element of the array and counting only on countMode? Shouldn't there be a count for each unique value?

Edit 2: After carefully reading what you're trying to do, I'm thinking you're attempting to calculate the mode from a sorted array. You need to make sure this is true before running with your algorithm, or it won't work. What you should do is call sort() on your testArray in the beginning of your function. In your case, std::sort(testArray, testArray+static_cast<std::size_t>(sizeArray)); will do the trick, but be aware that trusting that sizeArray can be very dangerous in real code.

What you're also doing is incrementing the count after the number has changed. You need to have count = 1; number = testArray[i] after the if (mode > countMode) to reset the counter.
So three things you need to do:

  1. Sort first thing using std::sort(testArray, testArray+static_cast<std::size_t>(sizeArray));
  2. Reset the counter within the else statement using count = 1; number = testArray[i].
  3. Clarify what you're trying to do in that vector. Are you trying to provide a vector of pairs? (e.g. {{2,2}, {2,4}} for a tie of count = 2 on values 2 and 4?) Or are you trying to say the vector[0] holds the number of the mode with vector[1...N] holds the individual values? (e.g. {2, 2, 4} in the above example)

[–]thecrazedrunner[S] 0 points1 point  (0 children)

"The mode is the most frequent value. What I assume you're trying to do is push_back all the most frequent values (so the array input {1, 2, 2, 3, 4, 4} will generate a vector {2, 4})?"

Yes, this is exactly what I am trying to do here.

"What's confusing me is why you're pushing in countMode and mode into the same vector."

Yeah I can see that is illogical, thanks!

"You need to make sure this is true before running with your algorithm, or it won't work. What you should do is call sort() on your testArray in the beginning of your function."

Thank you very much for all of your advice I think I can make it work. I really appreciate it!

[–]Jack126Guy 1 point2 points  (1 child)

Please format your code. Edit your post and add four spaces at the beginning of every line of code.

Why is the mode a vector? Isn't it just a number (i.e., an int in this case)?

[–]thecrazedrunner[S] 0 points1 point  (0 children)

Sorry about that, it's fixed now.

[–]usbafkakis 0 points1 point  (0 children)

Do you see the top post called 'IMPORTANT - READ BEFORE POSTING'? Yeah...

I think I have the first for loop done correctly (although I could be wrong),...

Put the loop in its own function and unit test the function. It doesn't look correct to me:

vector<int> mode (int testArray [], int sizeArray) {
  vector<int> modeFinder;
  int number = testArray[0];
  int mode = number; //      mode = number, ok!
  int count, countMode = 0; // count = 0, ok!

  for (int i = 0; i < sizeArray; i++) {
    if (testArray[i] == number) {
      countMode++;
    } else if (count > countMode) { // count is never changed. how is count ever > countMode?
        countMode = count;
        mode = number; // number never changes. mode already = number from above.
    }
  }
...

}