I have been given this question, in my computer science class, and I cannot figure out how to answer it. Question:
•Create a procedure called tossCoins with one integer parameter - numTosses
•Inside your tossCoins procedure, call your tossCoin function from challenge #5 numTosses times. So for example if we enter tossCoins(50), the tossCoin function will be called 50 times.
•Your tossCoins procedures should then print out the number of times that the coin landed on heads and the number of times the coin landed on tails
•Extend your main program, so that the user can choose how many times to toss the coin
I have already created the tossCoin function, but cannot figure out how to run it the amount of times the user asks, or how to create a tally of heads and tails. This is the code I have so far:
static void Main(string[] args)
{
Random rnd = new Random();
Console.WriteLine(tossCoin(rnd));
Console.Write("Please enter a number: ");
int numTosses = Convert.ToInt16(Console.ReadLine());
Console.ReadLine();
}
static string tossCoin(Random rnd)
{
int num = rnd.Next(1, 3);
string heads = "Heads";
string tails = "Tails";
if(num == 1)
{
return heads;
}
else
{
return tails;
}
}
static void tossCoins(int numTosses)
{
int headsTally = 0;
int tailsTally = 0;
for(int i = 0; i < numTosses; i++)
{
string outcome = tossCoin(rnd);
numTosses--;
if(outcome == heads)
{
headsTally++;
}
else
{
tailsTally++;
}
}
}
}
If anyone can help, it would be great, as I am trying to learn the different rules of using functions, and procedures. Thanks, Leighton.