Sunday, July 12, 2009

Basic C++ How do I count consecutive tails/heads in a coin flip program?

I made a basic coin flip program. Heads/tails based on random number. The program records and displays how many heads and tails occurred, but how do I count the biggest number of consecutive heads/tails?

Basic C++ How do I count consecutive tails/heads in a coin flip program?
IF randomvariable=heads THEN


countheads=countheads+1


Then you just keep looping it around, and make another if then statement for tails
Reply:you can use two sets of these two counters (one for heads and one for tails), one counter for the consecutive number of coin flips and the other counter for the largest consecutive number


example


int counter1=0;


int counter2=0;


if (condition)


counter1=counter1+1;


if (counter1%26gt;counter2)


counter2=counter1;


note:


first if statement is used to count the consecutive numbers of heads/tails (*condition required)


second if statement used to determine the largest number of consecutive heads/tails
Reply:Make a consecutive basic coin flip program, or start counting, one by one.

send flowers

No comments:

Post a Comment