Difference between revisions of "Rolling"

From CompSciWiki
Jump to: navigation, search
(Added Solution Code)
Line 22: Line 22:
  
  
|Solution=Nothing
+
|Solution=
  
  
  
 +
|SolutionCode=insert solution code here
 +
import java.util.Scanner;
 +
/**
 +
* A4Q1 -
 +
*
 +
* COMP 1010 Section A00
 +
* INSTRUCTOR
 +
* ASSIGNMENT 4, Question 1
 +
* @author 1010
 +
* @version 2010-Dec-3
 +
*
 +
* PURPOSE: simulation with two n-sided dice
 +
*/
 +
public class A4Q1 {
  
 +
public static void main(String[] args) {
  
 +
// declare variables
 +
int[] rollArray;
 +
Scanner myScanner = new Scanner(System.in);
 +
int numSides;
 +
int numSteps;
  
==Organize the Code==
+
// get the number of sides
 +
System.out.println("Enter the number of sides to the dice");
 +
numSides = myScanner.nextInt();
  
 +
// allocate the memory for the array
 +
rollArray = new int[2*numSides - 1];
  
===Separate the Statements===
+
// get the number of steps
 +
System.out.println("Enter the number of simulation steps");
 +
numSteps = myScanner.nextInt();
  
 +
while(numSteps != -1) {
  
===Separate Statements into Code Blocks===
+
// initialize all elements of the array to 0
 +
initializeArray(0, rollArray);
  
 +
// perform the simulation
  
 +
performSimulation(numSides, rollArray, numSteps);
  
===Add Comments to Explain the Code===
+
// print the histogram
 +
printRollHistogram(numSides, rollArray, numSteps);
  
 +
// get the next number of simulation steps
 +
System.out.println("Enter the number of simulation steps");
 +
numSteps = myScanner.nextInt();
  
==Optimize the Code by Removing Unnecessary Variables==
+
}
  
 +
System.out.println("Programmed by [your name here].\nEnd of Processing.");
  
 +
}
  
==Fix the Errors in the Code==
+
/**
 +
* Set all array elements to an initial value
 +
*
 +
* @param initValue The initial value
 +
* @param array The array of integers
 +
*
 +
*/
 +
public static void initializeArray(int initValue, int[] array)  {
 +
for(int i = 0; i < array.length; i++) {
 +
array[i] = initValue;
 +
}
 +
}
  
===Error One===
+
/**
 +
* Generate a random number between 1 and a maximum number (inclusive)
 +
*
 +
* @param maximum The maximum number to generate
 +
* @return A random number between 1 and maxium (inclusive)
 +
*/
 +
public static int generateRandomNumber(int maximum) {
 +
int number = (int)(Math.random()*(maximum) + 1);  // generate a random number between 1 and maximum
 +
return number;
 +
}
  
  
===Error Two===
+
/**
 +
* Perform the simulation by rolling the dice a number of times
 +
*
 +
* @param numSides The number of sides to the dice
 +
* @param counts An array containing the number of times each sum was rolled
 +
* @param numSteps The number of steps in the simulation
 +
*
 +
*/
 +
public static void performSimulation(int numSides, int[] counts, int numSteps) {
 +
int roll1;
 +
int roll2;
 +
for(int i = 1; i<=numSteps; i++) {
 +
roll1 = generateRandomNumber(numSides);
 +
roll2 = generateRandomNumber(numSides);
 +
counts[roll1 + roll2 - 2]++;
 +
}
  
 +
}
  
===Error Three===
+
/**
 +
* Print out the histogram
 +
*
 +
* @param numSides The number of sides to the dice
 +
* @param counts An array containing the number of times each sum was rolled
 +
* @param numSteps The number of steps in the simulation
 +
*
 +
*/
 +
public static void printRollHistogram(int numSides, int[] counts, int numSteps) {
 +
int percentage;
  
===Error Four===
+
System.out.println("Results of a "+ numSteps + "-step simulation with two " + numSides + "-sided dice: ");
 
+
for(int i = 0; i < counts.length; i++) {
 
+
percentage = (int)Math.round(counts[i] / ((double) numSteps) * 100);
===Error Five===
+
if(i < 8) {
 
+
printBar("Sum of  " + (i+2), percentage, '=');
 
+
}
===Error Six===
+
else {
 
+
printBar("Sum of " + (i+2), percentage, '=');
 
+
}
===Error Seven===
+
}
 
+
}
 
+
===Error Eight===
+
 
+
==Add Code to Output Progress Reports as the Program Executes==
+
 
+
===Location===
+
 
+
 
+
===Report Content===
+
 
+
===Adding Output Code===
+
  
 +
/**
 +
* Prints out a single bar for a histogram.
 +
*
 +
* @param label the label this bar should use
 +
* @param percent the number of symbols the bar should have
 +
* @param barSymbol the character the bar uses to indicate 1%
 +
*/
 +
public static void printBar(String label, int percent, char barSymbol) {
 +
// works well when all labels are the same width, but not so well otherwise.
 +
System.out.print(label + ": ");
  
 +
for (int i = 0; i < percent; i++) {
 +
System.out.print(barSymbol);
 +
}
  
 +
System.out.println(" (" + percent + "%)");
 +
}
  
  
 +
}
 
}}
 
}}

Revision as of 12:15, 31 March 2011

Back to the Case Studies homepage

Problem

Write a program that uses an array to store the results of a simulation with two n-sided dice. At each step in the simulation, “roll” the pair of the dice and use the array to keep track of the number times each of the possible sums has been rolled so far. To store the results of the simulation, you will need an array of length 2n - 1, where n is the number of sides to each die. The following is an example of what an array for two 6-sided dice could look like after a 24-step simulation.

0 1 2 3 4 5 6 7 8 9 10 1 3 0 2 3 5 3 4 2 1 0

Element 0 in our array represents a sum of 2, element 1 represents a sum of 3 and so on. The above array indicates that during the simulation, a sum of 2 (element 0) was rolled once, while a sum of 7 (element 5) was rolled 5 times. Your program should ask the user for two values: • The number of sides to the dice. Both dice will have the same number of sides. • The number of steps in the simulation. After reading in the input using Scanner, simulate rolling the die by generating two random numbers between 1 and the number of sides (inclusive). After the simulation is complete, print out a histogram displaying the percentage of simulation steps that generated each possible sum. You may re-use code from A3 to print the histograms.

 

SideSectionTitle

SideSection goes here.

Solution

Code

Solution Code

Back to the Case Studies homepage