Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C# program to simulate the drawing of 2 playing cards valued from 1 to 1

ID: 3541500 • Letter: W

Question

Write a C# program to simulate the drawing of 2 playing cards valued from 1 to 13. Use an object of class Random to draw the cards. The sum of the 2 cards then will be calculated. The sum of the values will vary from 2 to 26. Use a one-dimensional array to tally the number of times each possible sum appears. The program should draw the 2 cards 70,000 times. Display the results in tabular format. Note: the random number should use .Next(1,14) to generate random values from 1 to 13.



the code I have is shown below:


I am getting an error on the line rcards[i,2] = colVal;

I am new to c# and don't know many ways to debug the program.  Any help in teaching me how to debug the program would be greatly appreciated.


Thank You




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {         

            int[,] rcards = new int[70000,2];

            Random card1 = new Random();

            Random card2 = new Random();


            int rowVal = 0;

            int rowVal2 = 0;

            int colVal = 0;


            for (int i = 0; i < 70000; i++)

            {

                rowVal = card1.Next(1,14);

                rowVal2 = card2.Next(1, 14);

                colVal = (int)Math.Pow((int)rowVal,2);


                rcards[i,0] = rowVal;

                rcards[i,1] = rowVal;

                rcards[i,2] = colVal;


            for (int j = 0; j < 10; j++)

                {

                    Console.WriteLine("rowVal = {0}, colVal = {1}", rcards[j,0], rcards[j,1]);

                }


                Console.ReadLine();


        }

    }

    }

}

Explanation / Answer

There is error that you are accessing array of column 2 that does not exists. I have written below code which fulfills your requirement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

int[,] rcards = new int[70000,2];
int[] result = new int[27];

Random card1 = new Random();

Random card2 = new Random();


int rowVal = 0;

int rowVal2 = 0;

int colVal = 0;


for (int i = 0; i < 70000; i++)

{

rowVal = card1.Next(1,14);

rowVal2 = card2.Next(1, 14);

//colVal = (int)Math.Pow((int)rowVal,2);


rcards[i,0] = rowVal;

rcards[i,1] = rowVal2;


Console.ReadLine();


}
for(int i=0;i<70000;i++)
{
    if(((rcards[i,0]+rcards[i,1])%27)>=2 && ((rcards[i,0]+rcards[i,1])%27)<=26)
    {
        result[((rcards[i,0]+rcards[i,1])%27)]+=1;
    }
}
for(int i=2;i<27;i++)
{
    Console.WriteLine("sum {0} occurs:{1}",i,result[i]);
}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote