0

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?

1
  • 2
    Study up on integer division. Divide 59 by 10 and you get 5. Commented Oct 16, 2012 at 15:03

6 Answers 6

2

Um, it sounds like you just want:

bucketedScores[score / 10]++;

Don't you? Or possibly clearer:

bucketedScores[score / 10] = roughScores[score / 10] + 1;
Sign up to request clarification or add additional context in comments.

Comments

1
int index = value / 10;
myArray[index] += 1;

FYI, given what you said, you will get an IndexOutOfBoundsException with a score of 100. May need to deal with that.

Comments

0

Try it this way....

bucketedScores[score / 10] = roughScores[score / 10] + 1;

Comments

0

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

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.
FYI, don't need to init array with 0, that is the default value for int
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?
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 normal
0

According 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

0

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

If there are deciles 0..10, why do you divide decile by 10??
as each decile has 10 in them, thus the name. The problem is there is 11 deciles between 0 and 100.
But dividing decile by ten you'll only ever get zero or one. The OP's question was about how to calculate decile.
When you divide by 10 you get the 0th, 1st, 2nd, 3rd, etc decile. My point is that there is 11 deciles not 10.
Check your dictionary -- centile and percentile are the same thing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.