hi i need help about this problem.
Write a program to simulate tossing a coin (use a two-sided die). The program should
toss a coin 10,000 times (or some number of times specified by the user) and keep track
of the longest run of heads or tails that occurs in a sequence of simulated coin flips.
Thus, in the sequence HTHTTTHHHHT there is a sequence of 3 tails and a sequence of
4 heads.
To keep track of the runs, four variables—headRun, tailRun, maxHeads, and
maxTails—are defined and initialized to 0. These variables keep track of the length
of the current head run, the length of the current tail run, and the maximum runs of
heads and of tails, respectively.
thanks for your help...this is my e-mail adress:djimi_18@hotmail.com you can write here.
for example when I write on the screen random_number(0,1) HHHTTTTH in this have 3 maxhead and 4 maxtail, so I must calculate the max of tail and head at 100 random number where the number are only 0 and 1.
I write this but here show just how are total tail and head in 100 tossing,
You need two variables to keep number longest runs of tails and heads.
Then you have to check if current run is longer than longest run; and if it's longer, assign current number of runs to the variable that holds the longest number of runs.
int main()
{
int i,t=0,h=0,maxHead=0,maxTails=0;
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<10; index++){
random_integer =(rand()%2);
if(random_integer==1)
{
t++;
if(t>maxTails)
maxTails=t;
h=0;
}
if(random_integer==0)
{
h++;
if(h>maxHead)
maxHead=h;
t=0;
}
cout<<maxTails<<" is maxTail."<<endl<<maxHead<<" is maxHead.";
}
getch();
return 0;
}
So I write this code and the result was this: 1 is maxTails.
0 is maxHeads.2 is maxTails
0 is maxHeads.3 is maxTails
...........................................
............................................
1 is maxHead.
but this code don't show me the maxHead and maxTails at 100 random tossing,do I have anywhere mistake,for example at the previous code when I was just for random number you saw there the result was other,but here doesn't. And I couldn't understand why at head we take t=0 why not at tail?