so i'm creating a program in java where you enter scores and it classifies them into deciles (0-9, 10-19, 20-29, ... 80-89, 90-100) and I've got a grasp on how the program is supposed to work but I'm missing one key element. I create an array with 10 elements (one for each decile range). Once the user inputs their score it divides by 10 and then needs to put the score into it's appropriate spot in the array, but that's where I'm lost. It has to do a bunch of things after that, that I understand but how am I supposed to tell the code when someone enters 55 to increase the 50-59 part of the array by 1 and so on?
6 Answers
Sounds like
int[] myarray = new int[10];
// Divide and increase the number returned (like 84/10 = 8 in integer division)
myarray[num/10]++
Though 100 would throw it off, you would need a special case for that one.
4 Comments
Hot Licks
Well, I was going to downvote you for initializing the array when that's unnecessary. But I won't because you did raise the point that 100 is a special case.
John B
FYI, don't need to init array with
0, that is the default value for intEvan Lemmons
i just figured i would do an if case and say if score = 100 to put it into the 90-100 decile, but how would i phrase that?
Ben
Might be simpler to just use
if (num == 100) num = 99 then you dont have to worry about the rest of the code executing as normalAccording to the Question get into loops say for example i hope you're dividing 100 values 10-10 each. Use for loops and check and categorize it by assigning the input to a temporary variable for each iteration of inputs.
for(int i=0; i<=100; i++)
{
if(a[i] >= 0 && a[i] < 10)
your desired o/p(execution)
else if(a[i] > 10 && a[i] < 20)
your desired o/p(execution)
}
Comments
As the range is has 101 values from 0 to 101 you need to do more than divide by 10.
Do you mean like?
int[] scores = new int[10];
int decile = ...
int score = ....
scores[Math.min(9, decile/10)] += score;
This ensures that 100 is mapped to 9. Another solutions is to use (int)(decile/10.1) which would maps 0 to 10 to the first decile.
9 Comments
Hot Licks
If there are deciles 0..10, why do you divide decile by 10??
Peter Lawrey
as each decile has 10 in them, thus the name. The problem is there is 11 deciles between 0 and 100.
Hot Licks
But dividing decile by ten you'll only ever get zero or one. The OP's question was about how to calculate decile.
Peter Lawrey
When you divide by 10 you get the 0th, 1st, 2nd, 3rd, etc decile. My point is that there is 11 deciles not 10.
Hot Licks
Check your dictionary -- centile and percentile are the same thing.
|