Thread: Problem in Finding the Mode of Dice Roll Outcomes? Help?

1. Problem in Finding the Mode of Dice Roll Outcomes? Help?

Ok, so I wrote a program in "Ready to Program with JAVA Technology IDE" that generates and prints out a total of 50 numbers between 1 and 6 to simulate dice throw outcomes. I also used arrays to try and find the mode number as efficiently as possible, using the least amount of code.

THE PROBLEM: ok so the problem is that when there is more than one mode, only the mode with the largest dice index will be set to the most occuring number as it is the one processed last. So dice roll of 6 would be mode even though dice roll of 5 occured the same number of times.

So does anyone know how to fix this problem with the least amount of code. I tried but just couldn't find a way. So this is what I have so far:

// The "DiceRoll" class.
import java.awt.*;
import hsa.Console;

public class DiceRoll
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();

int rollNumber, i=0;
int modeNumber, mode;

int[] num = new int[6];
for(i=0;i<6;i++){
num[i]=0;
}//end of for loop

for (i=0;i<50;i++) {

rollNumber = (int) (Math.random()*6)+1;
c.print(rollNumber+"\t");
num[rollNumber-1]++;

}//end of for loop

for (i=0;i<6;i++) {
c.println("\nNumber of "+(i+1)+"'s rolled: "+ num[i]);
}//end of for loop

modeNumber = num[0];
mode = 1;
for(int j=1; j<6; j++){
if (num[j]>modeNumber) {
modeNumber = num[j];
mode = j+1;
}
//if (num2l
}
c.println("\nThe mode is "+mode);

} // main method
} // DiceRoll class
Last edited by Kratos321; 05-11-2011 at 03:04 AM. Reason: forgot to include problem