Question

Sports fans often get excited over a team or player who is having a "hot streak."...

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.

  1.    Inside the for loop you will get a random number 0 or 1

           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.

  1. End the loop
  2. Print out largest streak of heads

Homework Answers

Answer #1

#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT