0

I have an array that counts how many times each value from 1 to 6 appears in a dice simulator which "rolls a dice" 100 times. My goal is to find the most frequent dice roll.

This is my code so far, and everything works fine except the for-loop in the end which only outputs "6".

    Random dice = new Random();

    int diceThrow[] = new int[100];
    int throwsPerDice[] = new int[6];       

    for(int i = 0; i < 100; i++){
        diceThrow[i] = dice.nextInt(6) + 1;

        switch (diceThrow[i]){
            case 1:
                throwsPerDice[0]++;
                break;
            case 2:
                throwsPerDice[1]++;
                break;
            case 3:
                throwsPerDice[2]++;
                break;
            case 4:
                throwsPerDice[3]++;
                break;
            case 5:
                throwsPerDice[4]++;
                break;
            case 6:
                throwsPerDice[5]++;
                break;
            default:
                System.out.println("error");
        }

    }

    Arrays.sort(throwsPerDice);
    int max = throwsPerDice[throwsPerDice.length-1];
    int mostFrequent = 0;

    //Only outputs "mostFrequent = 6;" Why?
    for(int i = 0; i < throwsPerDice.length; i++){
        if(max == throwsPerDice[i]){
            mostFrequent = i+1;
        }
    }

    System.out.println("Most frequent dice roll : " + mostFrequent);

Any idea about what I'm doing wrong? I'm trying to keep the code short and easy. I'm in my first semester learning java so an unadvanced solution would be preferable.

Also, is it possible to count the frequency of each diceThrow without using a switch/if statement?

1
  • 2
    You know, that switch statement is totally unnecessary. Commented Oct 5, 2014 at 13:58

3 Answers 3

1

The main problem is that, once you've sorted throwsPerDice, you no longer know which of the counts refers to which die. No matter what you do afterwards you cannot recover that information.

Your code always returns 6 because the highest count has been sorted into the final position in throwsPerDice.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see! I didn't notice that. Thanks!
0

Let's say your array contains

 [10, 20, 30, 20, 5, 15]

after the first loop.

Now the code sorts the array, so it becomes

[5, 10, 15, 20, 20, 30]

And max is initialized with the last value in the array: 30.

Now the last loop iterates to find the index of the array containing the max element. Of course it's always the last one, since you just sorted the array.

Rethink your algorithm: don't sort the array, but iterate over the array to find the max element, and its index.

Just a note: your big switch statement should be replaced with

throwsPerDice[diceThrow[i] - 1]++;

Comments

0

remove this part of your code:

Arrays.sort(throwsPerDice);
int max = throwsPerDice[throwsPerDice.length-1];
int mostFrequent = 0;

//Only outputs "mostFrequent = 6;" Why?
for(int i = 0; i < throwsPerDice.length; i++){
    if(max == throwsPerDice[i]){
        mostFrequent = i+1;
    }
}

and replace this:

int mostFrequent = 0;
for(int i = 0; i < throwsPerDice.length; i++){
    if(throwsPerDice[i] > throwsPerDice[mostFrequent]){
        mostFrequent = i;
    }
}
System.out.println("Most frequent dice roll : " + mostFrequent + 1);

this will work. Your code didnt work because your did not keep track of your dices when you use : Arrays.sort

Comments

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.