I am having trouble calculating out the frequency of the sum of the rolling of a pair of dice. I am told I have to use a one dimensional integer array to count the number of times each possible sum appears in 36000 rolls. I am unsure what I am supposed to do since we just started going over arrays. Here is a link to the instructions: http://s65.photobucket.com/user/jls7884/media/DicePic-page-001_zpsd45d977f.jpg.html?filters[user]=139936213&filters[recent]=1&sort=1&o=0.
Here is the code I have so far:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Dice;
/**
*
* @author Jacob
*/
import java.util.Random;
public class Dice
{
public static void count1 ()
{
int count;
int frequency[] = new int[7];
frequency[1] = 2;
count = 1;
while (count <= 6)
{
frequency[count] = count + 1;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void count2 ()
{
int count;
int frequency[] = new int[8];
frequency[1] = 3;
count = 1;
while (count <= 6)
{
frequency[count] = count + 2;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void count3 ()
{
int count;
int frequency[] = new int[7];
frequency[1] = 4;
count = 1;
while (count <= 6)
{
frequency[count] = count + 3;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void count4 ()
{
int count;
int frequency[] = new int[7];
frequency[1] = 5;
count = 1;
while (count <= 6)
{
frequency[count] = count + 4;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void count5 ()
{
int count;
int frequency[] = new int[7];
frequency[1] = 6;
count = 1;
while (count <= 6)
{
frequency[count] = count + 5;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void count6 ()
{
int count;
int frequency[] = new int[7];
frequency[1] = 7;
count = 1;
while (count <= 6)
{
frequency[count] = count + 6;
count++;
}
System.out.printf
(
"%-2d %-2d %-2d %-2d %-2d %-2d \n"
, frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]
);
}
public static void main(String[] args)
{
int num1 = 1;
int num2 = 2;
int num3 = 3;
int num4 = 4;
int num5 = 5;
int num6 = 6;
int num = 0;
while (num < 7)
{
System.out.printf ("%-3d", num++);
}
System.out.println ();
System.out.printf ("%d: ", num1);
Dice.count1 ();
System.out.printf ("%d: ", num2);
Dice.count2 ();
System.out.printf ("%d: ", num3);
Dice.count3 ();
System.out.printf ("%d: ", num4);
Dice.count4 ();
System.out.printf ("%d: ", num5);
Dice.count5 ();
System.out.printf ("%d: ", num6);
Dice.count6 ();
String sumStr = "Sum";
String frequencyStr = "Frequency";
String percentageStr = "Percentage";
System.out.printf ("%-5s %-12s %s", sumStr, frequencyStr, percentageStr);
System.out.println ();
Random rollDie = new Random ();
for (int i = 1; i <= 36000; i++)
{
int die1 = 1 + rollDie.nextInt ((6));
int die2 = 1 + rollDie.nextInt ((6));
int sum = die1 + die2;
}
int count = 2;
while (count <= 12)
{
int frequency = 1003;
double minusFrequency = 36000 - frequency;
double divideTotal = minusFrequency / 36000;
double percentageTotal = divideTotal * 100;
double calculatePercentage = 100 - percentageTotal;
double percentage = calculatePercentage;
int sumCount = count++;
System.out.printf ("%3d %11d %12.1f%% \n",sumCount, frequency, percentage);
}
}
}
Any help would be appreciated.
Thanks, Jacob
How would you do it on paper?
You would separate a sheet of paper into 12 columns (one for 1, one for 2, etc. until 12). You would then roll the dices 3600 times, and each time you would get a 7, for example, you would add a mark in the column 7. Then you would count the marks in each column, and divide the sum by 3600 to get their frequency.
Just do the same in Java: have an int[] counts
array of 12 elements. Then do a loop from 0 to 3600. At each iteration, roll the dices, and increment the array element corresponding to the value you got. So, for example, if you got 7th, yo would increment the 7th element of the array.