Mode
Premium Content - Free Preview
The remaining challenges will be more challenging!
To finish up your grading software, you would like to find the most common grade, or the mode. Each grade will be an integer from 1 to 5. For example, given {1,2,3,1}
, you should print 1
(on its own line).
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
David
Apr 1, 10:04 AM/**
Basically the idea is to compare each value for an index against
//all the values of the rest of indexes, so basically staying fixed
//on one index (i) while iterating through the next (j), an example is
David
Apr 1, 12:37 PMPlease update with a second hint with further insight!
Learneroo
Apr 2, 10:59 AM@David, it looks like your code needs some fixes. E.g. do you reset
count
for each number...If you want extra support and content, consider membership on learneroo for $25.
David
Apr 3, 4:48 PMAdmin,
Thanks much for your prompt answer, i wasn't resetting the counter, now my mind is more aware for that in the future... At the end i ended up thinking inside the box {1 to 5} rather than outside {random amount of numbers} to fit to this exercise....
Thanks and best regards.
thales
Jul 18, 5:49 PMa solution , this was very hard!
my code
Bernard Mitchell
Jul 28, 2:40 PMCan I get another hint on this one I've tried to create another array but I'm not sure about how to keep track of the mode. Thanks
Learneroo
Jul 28, 2:55 PM@Bernard, all the grades are integers 1 to 5, any way to keep track of them? A solution would be to create an array of size 6 where the index represents the grade and the value represents that
grade's frequency. E.g. for the example
{1,2,3,1}
, you would create an array that at the end would be{0,2,1,1,0,0}
.Bernard Mitchell
Jul 28, 4:12 PMI thought about this earlier but dismissed it , can you have two loops going at the same time, a loop within a loop is what I mean?
Learneroo
Jul 28, 4:16 PMYou can have nested loops (one loops inside another), but that's not used in the hinted-to solution, which uses two separate loops. (You can solve it another way if you prefer.)
Bernard Mitchell
Jul 29, 2:25 PMSo I understand that the zeros in the new array are just empty slots right? I still don't understand how you select the number that repeats the most. So far I've done...
Learneroo
Jul 29, 2:36 PMYour displayed code above doesn't actually do anything. The goal is to have an array at the end that contains the frequency of each number (1-5) from the original array. You can then go through
your new array to get the maximum number (like in earlier challenges). (See a solution if still stuck.)
Bernard Mitchell
Aug 4, 4:31 PMI'm trying to see if my new array points to the mode can you tell me why I'm getting number and words when I try to print out the array? I feel like my I wrote my if statements incorrectly. Thanks
Learneroo
Aug 4, 7:38 PMJava doesn't provide a built-in method for printing an Array's contents. You can either use an ArrayList of create your own method like this one:
Bernard Mitchell
Aug 5, 4:34 PMTwo questions...If I have two if statements that are true at the same time how does Java respond to this? Second question since one of my variables is the index of the loop is it affected by an if statement that is within the loop. I've provided my code for reference. And if my questions aren't really relevant to helping me solve this problem, can I get a hint to what I'm doing wrong? Thanks PS my code is only meant to establish an array that will lead me to the mode , not the mode itself.
Learneroo
Aug 6, 9:22 AMThe solution can be much simpler. Here's a partial solution with the first part solved which you can complete.
Bernard Mitchell
Aug 6, 3:37 PMI understand now, I never would have thought of that , especially not knowing about the +=. Just some feedback I feel like the lessons sometimes jump from do able to extremely hard. I feel like
there should be a moderate group of lessons that prepare the user for problems like this one. Thanks again.
kerry2021
Jun 3, 11:11 AMwhat does this error mean?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.doStuff(Main.java:9)
at Main.main(Main.java:33)
Murat Han
Jul 31, 9:44 AMAnother solution:
https://www.learneroo.com/user_answers/1695637983