How Can I??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheHav
    New Member
    • Oct 2008
    • 4

    #1

    How Can I??

    How Can i do this with out using an if statement?
    Hint please

    Code:
    while (roll <= 3600) {
       int i = roll2Dice();
       if (i == 3) count[3] = count[3] + 1;
       if (i == 5) count[5] = count[5] + 1;
       if (i == 7) count[7] = count[7] + 1;
       if (i == 9) count[9] = count[9] + 1;
       if (i == 11) count[11] = count[11] + 1;
       roll = roll + 1;
    }
  • TheHav
    New Member
    • Oct 2008
    • 4

    #2
    For Ex if roll2Dice( ) feeds back a 3, I want to add 1 this to the "appropriat e element of the array with out using an if statement."

    If you want to look at the code i have so far
    Code:
    public class Dice {
        public static void main(String[] args) {
            int[] count = new int[13];
            int roll = 1;
            while (roll <= 3600) {
                int i = roll2Dice();
                if (i == 3) count[3] = count[3] + 1;
                if (i == 5) count[5] = count[5] + 1;
                if (i == 7) count[7] = count[7] + 1;
                if (i == 9) count[9] = count[9] + 1;
                if (i == 11) count[11] = count[11] + 1;
                roll = roll + 1;
             }
             printHeading();
             printRow(3,2/36.,count[3]);
             printRow(5,4/36.,count[5]);
             printRow(7,6/36.,count[7]);
             printRow(9,4/36.,count[9]);
             printRow(11,2/36.,count[11]);
         }
    
         private static int roll2Dice() {
            int d1 = (int) (6 * Math.random()) + 1;
            int d2 = (int) (6 * Math.random()) + 1;
            return d1 + d2;
         }
    
         private static void printHeading() {
            System.out.println("SUM   PROBABBILITY   FREQUENCY RATIO");
         }
    
         private static void printRow(int sum, double probability, int count) {
            System.out.printf("%3d%14.3f%18.3f\n",sum,probability,count/3600.);
         }
    }
    
    /*
    SUM   PROBABBILITY   FREQUENCY RATIO
      3         0.056             0.056
      5         0.111             0.113
      7         0.167             0.162
      9         0.111             0.108
     11         0.056             0.057
     */
    so can you give me a hint.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      The sum of two six sided dice is a number in the range [2,12]. If we don't use the numbers 0 and 1 a simple array with 13 elements can hold all sums:

      Code:
      int sum[13];
      So your code boils down to:

      Code:
              for (int roll = 1; roll <= 3600; roll++) {
                  int i = roll2Dice(); 
                  sum[i]++; // update current sum
              }
      After that loop has finished array 'sum' holds all sums, i.e. the number of times 2 was thrown, 3 was thrown ...

      kind regards,

      Jos

      Comment

      • TheHav
        New Member
        • Oct 2008
        • 4

        #4
        Thanks I didnt think of that

        Comment

        Working...