Computer_Science_Tapestry(page 239/problem 5.5
#include <cstdlib>
#include <iostream.h>
#include<conio.h>
using namespace std;
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);
cout<<random_integer;
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<<endl<<maxTails<<" is maxTail."<<endl<<maxHead<<" is maxHead.";
getch();
return 0;
}—are defined and initialized to 0. These variables keep track of the lengthheads++, the value of headRun tails++ the value of tailRun is incremented.
1 Ocak 2011 Cumartesi
Toss_Coin
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
4 heads.
To keep track of the runs, four variables—
HTHTTTHHHHT there is a sequence of 3 tails and a sequence ofheadRun, tailRun, maxHeads, andmaxTails
of the current head run, the length of the current tail run, and the maximum runs of
heads and of tails, respectively. After the statement is incremented. After the statement
In addition, these variables must be reset to zero at the appropriate time and the values
of the max head run and max tail run variables set appropriately.
0 yorum:
Yorum Gönder