So I'm basically trying to recreate the simulation of two dice being rolled, and I need to count the frequency in a two dimensional rectangular array, then display the array. I'm so lost I wrote some code but it didn't work, can anyone help.

This is the assignment I'm not looking to cheat because I've done some work, I'm just stuck.

reate a C# application to simulate the rolling of two dice. Use a Random object to generate a random integer in the range 1 to 6 for each die. There are 36 possible combinations (6 possible numbers for die 1 times 6 possible numbers for die 20. Repeat this for 360 times. Use a two dimensional rectangular array to count the frequency of each combination. Display the frequency counts.

using System;

class RollingDice
{
   public static void Main(string[] args)
   {
      Random randomNumbers = new Random();
      int[,] frequency = new int[7, 7];
      int[] dice = new int[7];
      int[] dice2 = new int[7];

      Console.WriteLine("The following example will roll a dice 360 times and display the frequency count!");


      for (int totRoll = 1; totRoll >= 360; totRoll++)
      {
         for (int roll = 1; roll <= 2; roll++)
         {
            ++dice[randomNumbers.Next(1, 7)];
         }

         for (int roll2 = 1; roll2 <= 2; roll2++)
         {
            ++dice2[randomNumbers.Next(1, 7)];
         }
      }





      for (int x = 1; x <= dice.Length; x++)
      {
         Console.WriteLine("There are 36 combinations rolled these are the frequencies");
         for (int y = 1; y <= dice2.Length; y++)
         {
            Console.WriteLine("{0} and {1} were rolled: {2}", x, y, frequency[x, y]);
         }
      }







      Console.ReadKey();
   } // end Main
} // end class RollingDice

Line 15 should be <=, not >= . It's also standard to start at zero, not 1, so the line would look like for (int totRoll = 0; totRoll < 360; totRoll++)

What are dice1 and dice2 for? No one asked you to store the individual rolls or a count of them.

Where are you adding the rolls to the 2d array?

There are not 36 combinations no matter what your instructor thinks. There are 36 permutations, 21 combinations. Which is it the instructor wants? Some fo the permutations aren't distinguishable from eachother (1,1; 2,2; etc.)

Where do you calculate the frequency?

I have to count how many times each combination was rolled, that's why i included dice1 and dice2

I'm not sure how or what calculating frequency is to be honest

So I edited the code according to my teacher's advice, but I'm stuck. I want the dice to roll 360 times but according to the code I see that it rolls more than that, so I was wondering if anyone could advise me. Thanks

using System;


class Program
{
   static void Main(string[] args)
   {
      Random rand = new Random();
        int[,] count = new int[6, 6];
        int dice1, dice2;

        for (int z = 0; z < 6; z++)
        {
           dice1 = rand.Next(1, 6);
           for (int j = 0; j < 6; j++)
           {
              dice2 = rand.Next(1, 6);



              for (int i = 0; i < 360; i++)
              {
                 ++count[dice1, dice2];
                 for (int x = 0; x < 6; x++)
                 {



                    Console.WriteLine("Dice 1: {0}\t Dice 2: {1}\t Count: {2}", x + 1, z + 1, count[x, z]);
                 }
              }
           }
        }


      Console.ReadKey();
   } // end Main
} // end class Program

Put the for loop on line 21 outside the two other for loops.

Like this? But still rolls more than 360 times on some counts.

using System;


class Program
{
   static void Main(string[] args)
   {
      Random rand = new Random();
        int[,] count = new int[6, 6];
        int dice1, dice2;

        for (int i = 0; i < 360; i++)
        {

         for (int z = 0; z < 6; z++)
         {
              dice1 = rand.Next(1, 6);
            for (int j = 0; j < 6; j++)
            {
               dice2 = rand.Next(1, 6);




                    ++count[dice1, dice2];
                    for (int x = 0; x < 6; x++)
                    {



                       Console.WriteLine("Dice 1: {0}\t Dice 2: {1}\t Count: {2}", x + 1, z + 1, count[x, z]);
                    }
               }
            }
        }


      Console.ReadKey();
   } // end Main
} // end class Program

Why don't you try with 1 instead of 360. If that works fine, you can still aument to 360 or whatever.

Here is a small program I wrote to help understand what nested for loops do:

        static void Main(string[] args)
        {
            string inputStr = String.Empty;
            int outerForLoopCount = 0;
            int firstNestedForLoopCount = 0;
            int secondNestedForLoopCount = 0;

            int totalOuterLoops = 0;
            int totalFirstNestedLoops = 0;
            int totalSecondNestedLoops = 0;

            do
            {
                Console.Write("How many times do you want to run the outer for loop? ");
                inputStr = Console.ReadLine();
            } while (Int32.TryParse(inputStr, out outerForLoopCount) == false);

            do
            {
                Console.Write("How many times do you want to run the 1st nested for loop? ");
                inputStr = Console.ReadLine();
            } while (Int32.TryParse(inputStr, out firstNestedForLoopCount) == false);

            do
            {
                Console.Write("How many times do you want to run the 2nd nested for loop? ");
                inputStr = Console.ReadLine();
            } while (Int32.TryParse(inputStr, out secondNestedForLoopCount) == false);

            for (int i = 0; i < outerForLoopCount; i++)
            {
                for (int j = 0; j < firstNestedForLoopCount; j++)
                {
                    for (int k = 0; k < secondNestedForLoopCount; k++)
                    {
                        totalSecondNestedLoops += 1;
                    }//for

                    totalFirstNestedLoops += 1;
                }//for

                totalOuterLoops += 1;
            }//for

            Console.WriteLine();
            Console.WriteLine("Outer for loop executed: " + totalOuterLoops + " time(s)");
            Console.WriteLine();
            Console.WriteLine("First nested for loop executed: " + totalFirstNestedLoops + " times(s)");
            Console.WriteLine("     This is " + outerForLoopCount + " * " + firstNestedForLoopCount);
            Console.WriteLine();
            Console.WriteLine("Second nested for loop executed: " + totalSecondNestedLoops + " times(s)");
            Console.WriteLine("     This is " + outerForLoopCount + " * " + firstNestedForLoopCount + " * " + secondNestedForLoopCount);
            Console.WriteLine("");
            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }

14ef764bae9d7125ad1c57de5e47332a

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.