Sports fans often get excited over a team or player who is having a "hot streak." However it turns out that long runs of events, even random ones, occur more often than many people think. In this exercise you'll write a program to find the longest run of heads in a sequence of coin flips.
1.Set up Random
2.Declare and initialize the variables you will need
3.You will have a for loop that goes from 1 to 100.
Print out the value in terms of heads or tails
You will test to see if it is heads(heads =0).
If it is heads you will increment the heads counter. Print out headsCounter.
You will test the heads counter to see if it is greater than the maxHeadsCounter
and if it is put the new value of the headsCounter in the maxHeadsCounter
If it is tails you will reset the heads counter to 0 and start again.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int max_streak=0;//maximum streak
int counter=0; //keep s current cound of head streak
int x;//random input
for(int i=0;i<100;i++)
{
cin>>x;
if(x==0)
{
cout<<"heads\n";
counter++;
cout<<"Heads counter : "<<counter<<"\n";
max_streak=max(max_Streak,counter);
}
else
{
cout<<"tails\n";
counter=0;
cout<<"Heads counter : "<<counter<<"\n";
}
}
cout<<"Maximum Streak of Head :
"<<max_streak<<"\n";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.