Mode Comments
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 isthat while i=0 j should have gone from 0 to ar.length, then i=1 and j would do the same, now while the iteration is occurring do a comparison for the value and keep track through a 'count' how many times it repeats, additionally as the comparison will take place multiple times we need to know which one repeats most and when it does make that variable the mode. On this code as the second iterator (j) starts always from 0 it will evaluate the value twice, however this shouldn't matter as it shouldn't affect the end result, meaning that all values will be treated equally. My suspicion was that the 'count' wasnt being handled properly, but it works for the other Outputs. However i still keep getting 4 on the second output rather than 2... I did read the hint but at least particularly for Me it is not so intuitive, i'm still thing over it. Code below: **/ int mode=0; int count=0; int maxCount=count; for (int i=0;i<=ar.length-1; i++){ for(int j=0; j<=ar.length-1; j++){ if(ar[i]==ar[j]){ count++;} }//2nd for if(count>maxCount){ maxCount=count; mode=ar[i];} } System.out.println(mode); }
-
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.
-
-
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
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}
. -
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
your new array to get the maximum number (like in earlier challenges). (See a solution if still stuck.)
-
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; }
if(c==ar[d]){ int m=1; m=m+1; an[c]=m; } if(d==0){ d=ar.length-1; c=c+1; } } System.out.println(an);
I'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
-
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; }
else if(c==ar[d]){ int m=1; m=m+1; an[c]=m; } if(d==0){ d=ar[ar.length-1]; c=c+1; } } for(int g: an){ System.out.print(g+" "); }
Two 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.
-
- 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").
- Any code inside a while-loop can change the value of any variable, including any code in an if-statement.
The solution can be much simpler. Here's a partial solution with the first part solved which you can complete.
-
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
there should be a moderate group of lessons that prepare the user for problems like this one. Thanks again.
-
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) -
Another solution:
https://www.learneroo.com/user_answers/1695637983
David
Apr 1, 9:34 AMSorry, the code servers are currently experiencing difficulties. Please try again in a short while. Nan-da-toooo ?!