Reputation: 53
I need to display the average number of rolls needed to get a six, and the number of sixes that average was based. The problem I am having I think is with this part of the code? So I want the average number of rolls which I think I have as the variable AVGroll. The number of sixes the average was base on should be the loopcount variable.
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
Tried to comment my code as best as possible to make it readable.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CE0721a
{
class Tut4_7
{
public void run()
{
// Random number generator
Random rndm = new Random();
//declaring number for Random Number Generator
int number;
// average number of runs
int average;
//declaring loopcount starts at 1
int loopcount = 1;
//Average roll starts at 0
int AVGroll = 0;
//Variable if it continues
int progcontinue;
//Start lable
Start:
do
{
number = rndm.Next(6) + 1;
Console.WriteLine(number);
Console.ReadKey();
if (number < 6)
{
loopcount++;
}
} while (number != 6);
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
Console.WriteLine("The roll count is:");
Console.WriteLine(loopcount);
Console.WriteLine("average");
Console.WriteLine(AVGroll);
Console.WriteLine("press 1 to continue or 0 to exit");
progcontinue = (int.Parse(Console.ReadLine()));
if (progcontinue > 0)
{
loopcount = 1;
goto Start;
}
else
{
}
}
}
}
Upvotes: 0
Views: 966
Reputation: 93030
You divide by the wrong thing:
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
You want to average over the number of trials. One trial is you roll until you get 6.
Then based on progcontinue
you do more trials.
Thus have an extra variable that counts trials and divide by that:
int trial = 1;
//...
AVGroll = AVGroll + loopcount;
average = AVGroll / trials;
//...
if (progcontinue > 0)
{
loopcount = 1;
++trials;
goto Start;
}
Also you need to print average
, not AVGroll:
Console.WriteLine("average");
Console.WriteLine(AVGroll); //should be average
Upvotes: 1
Reputation: 405
I am a bit confused by your code but I think you are using the wrong variables to calculate the AvgRoll. the first run AVGroll will always be 1 example:
AVGroll = AVGroll (0) + loopcount (5);
average = AVGroll (5) / loopcount (5);
so it will be 1
I think you need to do something like this:
int timesContinued = 1;
//Start lable
Start:
do
{
number = rndm.Ne
xt(6) + 1;
Console.WriteLine(number);
Console.ReadKey();
if (number < 6)
{
loopcount++;
}
} while (number != 6);
AVGroll = AVGroll + loopcount;
average = AVGroll / timesContinued;
Console.WriteLine("The roll count is:");
Console.WriteLine(loopcount);
Console.WriteLine("average");
Console.WriteLine(AVGroll);
Console.WriteLine("press 1 to continue or 0 to exit");
progcontinue = (int.Parse(Console.ReadLine()));
if (progcontinue > 0)
{
loopcount = 1;
timesContinued++;
goto Start;
}
this way you will devided the total times rolled by the number of times that you pressed continue which I hope is what you wanted.
Upvotes: 1