Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,415 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,421 people online right now. Registration is fast and FREE... Join Now!




Fun Basic C# App - Need Help!

 
Reply to this topicStart new topic

Fun Basic C# App - Need Help!, Dice Rolling Problem

Deej
14 Oct, 2008 - 05:36 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 1

biggrin.gif 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.

CODE
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.

User is offlineProfile CardPM
+Quote Post

n8wxs
RE: Fun Basic C# App - Need Help!
14 Oct, 2008 - 10:12 PM
Post #2

D.I.C Regular
***

Joined: 6 Jan, 2008
Posts: 448



Thanked: 43 times
My Contributions
QUOTE(Deej @ 14 Oct, 2008 - 06:36 PM) *

biggrin.gif 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! smile.gif

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. smile.gif
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
User is offlineProfile CardPM
+Quote Post

wingot
RE: Fun Basic C# App - Need Help!
14 Oct, 2008 - 10:20 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 38

Hi Deej,

Sounds like a homework assignment, which normally we wouldn't help with, but it also looks like you've given it a decent shot, and are just lost, which we would help with. As such, a couple of questions:

1) Based on the problem, you don't need to store the die rolls themselves, only the total of the die roll. As such, why have you created an array for each die instead of just an array of the results? If you do that you could have used only 1 for loop to generate the same result and would simplify the code so much.
2) From my reading of the problem, the tabular results that are needed are just the totals, not the 36,000 individual rolls. So 1 line for total of 2, 1 line for total of 3, 1 line for total of 4, etc. That makes the results actually meaningful as well smile.gif.

Just a couple of thoughts that should simplify the problem/solution a lot and might get you out of being stuck smile.gif.

EDIT: Looks like Jeff beat me on the above smile.gif.

This post has been edited by wingot: 14 Oct, 2008 - 10:23 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:46PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month