Re: Printing A Histogram...
I think the problem lies in this line..
Code :
System.out.print(
" " + total + ": " + frequency[total] + " ");
Re: Printing A Histogram...
Re: Printing A Histogram...
Quote:
Originally Posted by
Nuggets
I think the problem lies in this line..
Code :
System.out.print(
" " + total + ": " + frequency[total] + " ");
Hello Nuggets!
I'm not sure but what if you remove frequency[total] from that code?
Re: Printing A Histogram...
Quote:
Originally Posted by
andreas90
Hello Nuggets!
I'm not sure but what if you remove frequency[total] from that code?
Removing frequency[total] helped , as it got rid of those numbers. But then it added a 13 to the output. Do you know where that 13 came from?
Before with frequency[total] -
2: 2**
3: 5*****
4: 6******
5: 14**************
6: 11***********
7: 21*********************
8: 9*********
9: 9*********
10: 9*********
11: 12************
12: 2**
Without -
2: **
3: ****
4: ************
5: **************
6: *****************
7: ****************
8: **********
9: ************
10: ********
11: ***
12: **
13:
Re: Printing A Histogram...
Can you post the updated code that gives you this output? And also what you input in the How many dice rolls would you like to simulate? question?
Re: Printing A Histogram...
Code :
import java.util.*;
public class DiceSim {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int[] frequency = new int [13]; //Declares the array
int die, die2;
int numbThrows;
int asterisk;
int total;
double fractionOfReps;
System.out.println("How many dice rolls would you like to simulate? ");
numbThrows = Input.nextInt();
//Roll the dice
for (int i=1; i<=numbThrows; i++) {
die = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
total = die + die2;
frequency[total]++;
}
System.out.println("DICE ROLLING SIMULATION RESULTS " + '\n' + "Each " + '\"' + "*" + '\"' + " represents 1% of the total number" +
" of rolls.");
System.out.println("Total number of rolls = " + numbThrows);
//output dice rolls
for (total=2; total<=numbThrows; total++)
{
System.out.print(
" " + total + ": ");
fractionOfReps = (float) frequency[total] / numbThrows;
asterisk = (int) Math.round(fractionOfReps * 100);
for (int i=0; i<asterisk; i++)
{
System.out.print("*");
}
System.out.println();
}
}
}
I input 100 dice rolls to simulate. Output is this:
DICE ROLLING SIMULATION RESULTS
Each "*" represents 1% of the total number of rolls.
Total number of rolls = 100
2: ****
3: ********
4: *******
5: ******
6: **************
7: ******************
8: *****************
9: ************
10: *********
11: ***
12: **
13:
Re: Printing A Histogram...
Code java:
for (total=2; total<=numbThrows; total++)
In the above code total goes up to numbThrows. And then
Code java:
fractionOfReps = (float) frequency[total] / numbThrows;
So when total equals 13 your program stops and you should face an ArrayIndexOutOfBoundsException (don't you?) because frequency[] has 13 indexes (from 0 to 12) and you try to access index 13.
Re: Printing A Histogram...
Quote:
Originally Posted by
andreas90
Code java:
for (total=2; total<=numbThrows; total++)
In the above code
total goes up to
numbThrows. And then
Code java:
fractionOfReps = (float) frequency[total] / numbThrows;
So when
total equals 13 your program stops and you should face an
ArrayIndexOutOfBoundsException (don't you?) because
frequency[] has 13 indexes (from 0 to 12) and you try to access index 13.
Code :
for (total=2; total<=numbThrows; total++)
Okay, for the code above, I replaced the numbThrows with a 12.
Code :
for (total=2; total<=12; total++)
This seemed to fix the problem with the 13 occurring at the end of the output statement. Thanks for the help btw. :)
Re: Printing A Histogram...
Unfortunately, i can't help you with that. I'm not at all familiar with the Eclipse IDE and how it works.