Hi everyone! I would really appreciate any help that I can get on this problem.
My professor asigned us a project where you create a program that essentially simulates coin flips. The first step in the program is to ask the user how many times they want to flip the coin. Regardless of how many times they choose to flip the coin the program needs to print the result of each flip whether it's heads or tails. I have all of that done i can't figure out how to implement the last step which is to display the maximum consecutive heads and tails that appear in the results from the coin toss'.
DO WHILE count < flipq
LET num=INT(RND(0)*2)+1
IF num = 1 THEN
PRINT "T"
'LET count = count + 1 ' put this before the IF
'LET tcount = tcount + 1 ' uncomment this and duplicate it for heads
'LET maxtcount = tcount
'IF maxtcount > tcount THEN
'LET maxtcount = maxtcount
'END IF
ELSE
PRINT "H"
END IF
LOOP
Initialize tcount and hcount before the while loop.
DO WHILE count < flipq
LET num=INT(RND(0)*2)+1
IF num = 1 THEN
PRINT "T"
'LET count = count + 1 ' put this before the IF
'LET tcount = tcount + 1 ' uncomment this and duplicate it for heads
'LET maxtcount = tcount
'IF maxtcount > tcount THEN
'LET maxtcount = maxtcount
'END IF
ELSE
PRINT "H"
END IF
LOOP
Initialize tcount and hcount before the while loop.
Thank you for your help. Your advice massively improved my program. But I have another question: what do you mean by initialize? Please don't repremand me for asking. I feel bad enough about having to ask for help already. Haha.
Here is the improved code after incorperating your advice and the advice of my professor. It does everything except display the correct maximum consecutive head and tail count. I am open to any advice. Once again it is greatly appreciated.
Ananda
'Ananda Bennett
'10/11/2006
'Heads or tails program
INPUT "How many times do you want to flip the coin?: " ;flipq
IF flipq < 1 THEN
PRINT "You must flip the coin at least 1 time"
END IF
DO WHILE count < flipq
LET num=INT(RND(0)*2)+1
LET count = count + 1
IF num = 1 THEN
PRINT "T"
LET tcount = tcount +1
LET maxtcount = tcount
IF maxtcount > tcount THEN
LET maxtcount = maxtcount
END IF
ELSE
PRINT "H"
LET hcount = hcount + 1
LET maxhcount = hcount
IF maxhcount > hcount THEN
LET maxhcount = maxhcount
END IF
END IF
LOOP
PRINT "t count: " ;tcount; " h count: ";hcount
PRINT "The maximum number of consecutive heads for " ; flipq; " coin flips is: " ;maxhcount
PRINT "The maximum number of consecutive tails for " ; flipq; " coin flips is: " ;maxtcounta
The code is below ... I have briefly commented it to give you an idea of what is going on, and left the keywords in lower case.
I kept it very rough, there are many, better ways to do this, it is my intention you will look at the method and learn from it and improve on the overall code.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.