Visual C# 8.17 (Dice Rolling)

This Solution is for the “Dice Rolling” of chapter 8 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/25/2017

/*Description:
simulates the rolling of two dice. Uses an object of class Random once to roll the
first die and again to roll the second die. Sums the two values. rolls the dice
36,000 times. uses a one-dimensional array to tally the number of times each
possible sum appears then display the roll(sums), frequency, & percentage,
in tabular format.
*/

using System;
class Program
{
static void Main(string[] args)
{

const int arraylength = 13;//sets arraylength as a constant
int[] array = new int[arraylength];//creates array & sets length to 13

int roll1;
int roll2;

var randomNumbers = new Random();//generates random number

//rolls dice and puts values in appropriate array element
for (var roll = 1; roll <= 36000; ++roll)
{
roll1 = randomNumbers.Next(1, 7);
roll2 = randomNumbers.Next(1, 7);

++array[roll1 + roll2];
}
//displays table lables
Console.WriteLine($”{ “Roll”} { “Frequency”,9} {“Percentage”,5}”);

//outputs each array element value & percent of value rolled
for (var element = 2; element < array.Length; ++element)
{
var percent = array[element] / (360.0);

Console.WriteLine($”{element,4}{ array[element],10}{percent,11:F}”);

}
}
}

Program Output:
Roll   Frequency       Percentage
2        1008                  2.80
3        1937                  5.38
4        3012                  8.37
5        3908                  10.86
6        5137                  14.27
7        6093                  16.93
8        4976                  13.82
9        3913                  10.87
10      2989                  8.30
11      1999                  5.55
12      1028                  2.86
Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s