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

  • /**
    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

    cont...
  • Please update with a second hint with further insight!

  • @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.

  • Admin,

    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.

  • a solution , this was very hard!

    my code

  • Can 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

  • @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

    cont...
  • I 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?

  • You 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.)

  • So 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...

    int [] a= new int [6];
        for(int b=0;b<a.length-1;b++){
            int c=ar[b];
    
  • Your 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

    cont...
  • int[] an = new int [6];// an ={0,0,0,0,0,0}
        int c=0;
        int d=ar.length-1; 
        while(c<an.length-1){
            d=d-1;
            if(c!=ar[d]){
                an[c]=0;
            }
    
    cont...
  • Java 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:

    static void printArray(int[] ar){
      for(int n: ar)
        System.out.print(n+" ");
    }
    
  • int[] an = new int [6];// an ={0,0,0,0,0,0}
        int c=0;
        int d=ar.length-1; 
        while(c<an.length-1){
            d=d-1;
            if(c!=ar[d]){
                an[c]=0;
            }
    
    cont...
    1. Once Java reaches a true statement in an if-else-if block, it stops checking the remaining else-if statements (they're only checked if the previous statements were false, that's why it says "else if").
    2. Any code inside a while-loop can change the value of any variable, including any code in an if-statement.
    cont...
  • I 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

    cont...
  • what does this error mean?
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Main.doStuff(Main.java:9)
    at Main.main(Main.java:33)

All Node Comments
Contact Us
Sign in or email us at [email protected]