QUOTE(Deej @ 14 Oct, 2008 - 06:36 PM)


Hey guys - This problem says, "Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum an 2 and 12 being the least frequent sums. Your application should roll the dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Display the results in tabular format. Determine whether the totals are reasonable (e.g. there are six ways to roll a 7, so approximately one-sixth of the rolls should be 7).
Here is what I got so far:
By the way this is in C# Visual Studio 2008.
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _8._17
{
class Program
{
static void Main(string[] args)
{
Random randomNumbers = new Random(); // random number generator
int[] totaldie;
int[] die1;
int[] die2;
// roll die1 36,000 times
for (int roll1 = 1; roll1 <= 36000; roll1++)
++die1[randomNumbers.Next(1, 7)];
// roll die2 36,000 times
for (int roll2 = 1; roll2 <= 36000; roll2++)
++die2[randomNumbers.Next(1, 7)];
// add them together
for (int counter = 1; counter <= die1.Length; counter++)
totaldie = die1 + die2;
//display the frequnecy
Console.WriteLine("{0}{1,10}", "Face", "Frequency");
//output each array elemtns value
for (int face = 1; face < total.die1; face++ )
Console.WriteLine( "{0,4}{1,10}", face, total[ face ] );
}
}
}
Could I get some help on it? I'm just lost.
I think you are working at it too hard!
The problem statement says:
"Use a one-dimensional array to tally the numbers of times each possible sum appears."
You are keeping track of the number of times each individual number occurs:
csharp
++die1[randomNumbers.Next(1, 7)];
++die2[randomNumbers.Next(1, 7)];
instead of the number of times the sum of the two die faces occurs:
csharp
// initialize array
// c# indicies start at zero, so size to 13 (sum 2 to 12)
int[] totaldie = new int[13] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int roll = 0; roll < 36000; roll++)
{
totaldie[randomNumbers.Next(1, 7) + randomNumbers.Next(1, 7)]++;
}
I changed your output statement to:
csharp
//output each array elemtns value
int throws = 0;
for (int face = 2; face < totaldie.Length; face++)
{
Console.WriteLine("{0,4}{1,10}", face, totaldie[face]);
throws += totaldie[face];
}
Console.WriteLine("Throws = {0}", throws);
Console.ReadKey();
The Console.ReadKey() call will let you read the console screen.
Hit any key to continue (exit).
Here's what I came up with:
csharp
static void Main(string[] args)
{
Random randomNumbers = new Random(); // random number generator
int[] totaldie = new int[13] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int roll = 0; roll < 36000; roll++)
totaldie[randomNumbers.Next(1, 7) + randomNumbers.Next(1, 7)]++;
//display the frequnecy
Console.WriteLine("{0}{1,10}", "Face", "Frequency");
//output each array elemtns value
int throws = 0;
for (int face = 2; face < totaldie.Length; face++)
{
Console.WriteLine("{0,4}{1,10}", face, totaldie[face]);
throws += totaldie[face];
}
Console.WriteLine("Throws = {0}", throws);
Console.ReadKey();
}
Jeff
This post has been edited by n8wxs: 14 Oct, 2008 - 10:20 PM