Hi guys, noob programmer here, so anyway, my professor gave us an assignment this week and i'm just stumped on it - i'm sure it's a really simple solution, cuz i tend to overthink all of her assignments. Anywya, the assignment is to get a sequence of numbers from the user and find the mode and how many times it was entered.
The thing is, we're supposed to psuedocode the whole lab this week and do the actual coding next week, so i don't have any actual code. If someone could just give me a hint on how to code this, I can easily figure out the code/psuedo on it. I cannot figure out how to get the mode without using arrays, and we aren't allowed to use those since we haven't gone over them yet.
If anyone can answer, help would be greatly appreciated. I go to work soon and am looking for answers asap.
If you haven't learned about arrays then have you learned about any other collections? Lists? Stacks? Vectors? Maps? Anything? It seems to me that it would be hard to find the mode without having a collection.
Nothing of the sort, that's why I've been stumped... The last thing we learned was functions, like void. I can't think of anything and I'm just really lost.
I've found a way to do it, but it's pretty ugly.
Here's the concept:
- Use std::string as your array of numbers.
- Delimit every number(as a string) the user enters with a newline character '\n'.
- I have a function that gets the frequency of the number(as a string), using std::string::find() called getFrequency().
- I use a loop to iterate through each number(as a string) calling getFrequency(). From this I get the highest frequency, that is, the frequency of the modal number.
- I then loop through each number again calling getFrequency() and compare it with the highest frequency. If the highest frequency is equal to the return value of getFrequency(), I return the number(as a string). This is the mode.
If possible, please post the exact text of the assignment. I suspect we're missing something. For example, if the numbers are entered in sorted order, then the solution is easy. Likewise if the problem asks to find mean instead of mode.