| | |
Max Consecutive Heads/Tails
![]() |
•
•
Join Date: Oct 2006
Posts: 20
Reputation:
Solved Threads: 0
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'.
Here is the code that I have so far:
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'.
Here is the code that I have so far:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
'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 IF num = 1 THEN PRINT "T" 'LET count = count + 1 'LET tcount = tcount + 1 'LET maxtcount = tcount 'IF maxtcount > tcount THEN 'LET maxtcount = maxtcount 'END IF ELSE PRINT "H" END IF LOOP PRINT tcount; 'PRINT "The maximum number of consecutive heads for " flipq; "coin flips is: " ;hcount 'PRINT "The maximum number of consecutive for " flipq; "coin flips is: " ;tcount
Last edited by ~s.o.s~; Oct 29th, 2006 at 5:35 pm.
I agree. Are they really teaching LET for VB? Or is this another version of BASIC? Maybe best in Legacy and Other Languages
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 The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2006
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
I agree. Are they really teaching LET for VB? Or is this another version of BASIC? Maybe best in Legacy and Other Languages
.
•
•
Join Date: Oct 2006
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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
Ananda
•
•
Join Date: Oct 2006
Posts: 20
Reputation:
Solved Threads: 0
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
'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 •
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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 ' Initialise values hcount = 0 tcount = 0 ' We will use LastValue to remember the last flip, and an ongoing tally ... Consec LastValue = 0 consec = 1 ' We set it at one, because the run will not match last, but will be consecutive maxhcount = 0 maxtcount = 0 DO WHILE count < flipq LET num=INT(RND(0)*2)+1 LET count = count + 1 IF num = 1 THEN PRINT "T" ' Show Tail LET tcount = tcount + 1 ' and increase tails counter ' Now we make sure we have a value in max values if maxtcount = 0 then ' maxtcount = 1 end if ELSE PRINT "H" LET hcount = hcount + 1 if maxhcount = 0 then maxhcount = 1 end if END IF ' Now we compare it with the last value, and increase the counter for the run IF num = LastValue THEN consec = consec + 1 ' Now we check if it's a run of Heads or tails, if num = 1 then if consec > maxtcount then maxtcount = consec end if else if consec > maxhcount then maxhcount = consec end if end if ELSE ' if it's not, we reset the run consec = 1 END IF ' And now we set the last value to this one LastValue = num 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: " ;maxtcount
![]() |
Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Wasnt sure where to put this
- Next Thread: Txt File
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows







:!:
:lol: :p