Max Consecutive Heads/Tails

Reply

Join Date: Oct 2006
Posts: 20
Reputation: anandarose is an unknown quantity at this point 
Solved Threads: 0
anandarose anandarose is offline Offline
Newbie Poster

Max Consecutive Heads/Tails

 
0
  #1
Oct 29th, 2006
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. '10/11/2006
  2. 'Heads or tails program
  3. INPUT "How many times do you want to flip the coin?: " ;flipq
  4. IF flipq < 1 THEN
  5. PRINT "You must flip the coin at least 1 time"
  6. END IF
  7. DO WHILE count < flipq
  8. LET num=INT(RND(0)*2)+1
  9. IF num = 1 THEN
  10. PRINT "T"
  11. 'LET count = count + 1
  12. 'LET tcount = tcount + 1
  13. 'LET maxtcount = tcount
  14. 'IF maxtcount > tcount THEN
  15. 'LET maxtcount = maxtcount
  16. 'END IF
  17. ELSE
  18. PRINT "H"
  19. END IF
  20. LOOP
  21.  
  22. PRINT tcount;
  23. 'PRINT "The maximum number of consecutive heads for " flipq; "coin flips is: " ;hcount
  24. '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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Max Consecutive Heads/Tails

 
0
  #2
Oct 29th, 2006
This forum is for C and C++.
What you've posted seems to be visual basic.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Max Consecutive Heads/Tails

 
0
  #3
Oct 30th, 2006
I agree. Are they really teaching LET for VB? Or is this another version of BASIC? Maybe best in Legacy and Other Languages

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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 20
Reputation: anandarose is an unknown quantity at this point 
Solved Threads: 0
anandarose anandarose is offline Offline
Newbie Poster

Re: Max Consecutive Heads/Tails

 
0
  #4
Oct 30th, 2006
Originally Posted by WaltP View Post
I agree. Are they really teaching LET for VB? Or is this another version of BASIC? Maybe best in Legacy and Other Languages
.
The software is called Liberty Basic. Www.libertybasic.com
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 20
Reputation: anandarose is an unknown quantity at this point 
Solved Threads: 0
anandarose anandarose is offline Offline
Newbie Poster

Re: Max Consecutive Heads/Tails

 
0
  #5
Oct 31st, 2006
Originally Posted by WaltP View Post

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.

Ananda
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 20
Reputation: anandarose is an unknown quantity at this point 
Solved Threads: 0
anandarose anandarose is offline Offline
Newbie Poster

Re: Max Consecutive Heads/Tails

 
0
  #6
Oct 31st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: Yankie Dave is an unknown quantity at this point 
Solved Threads: 0
Yankie Dave Yankie Dave is offline Offline
Newbie Poster

Re: Max Consecutive Heads/Tails

 
0
  #7
Nov 1st, 2006
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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. INPUT "How many times do you want to flip the coin?: " ;flipq
  3. IF flipq < 1 THEN
  4. PRINT "You must flip the coin at least 1 time"
  5. END IF
  6. ' Initialise values
  7. hcount = 0
  8. tcount = 0
  9. ' We will use LastValue to remember the last flip, and an ongoing tally ... Consec
  10. LastValue = 0
  11. consec = 1 ' We set it at one, because the run will not match last, but will be consecutive
  12. maxhcount = 0
  13. maxtcount = 0
  14. DO WHILE count < flipq
  15. LET num=INT(RND(0)*2)+1
  16. LET count = count + 1
  17. IF num = 1 THEN
  18. PRINT "T" ' Show Tail
  19. LET tcount = tcount + 1 ' and increase tails counter
  20. ' Now we make sure we have a value in max values
  21. if maxtcount = 0 then '
  22. maxtcount = 1
  23. end if
  24. ELSE
  25. PRINT "H"
  26. LET hcount = hcount + 1
  27. if maxhcount = 0 then
  28. maxhcount = 1
  29. end if
  30. END IF
  31. ' Now we compare it with the last value, and increase the counter for the run
  32. IF num = LastValue THEN
  33. consec = consec + 1
  34. ' Now we check if it's a run of Heads or tails,
  35. if num = 1 then
  36. if consec > maxtcount then
  37. maxtcount = consec
  38. end if
  39. else
  40. if consec > maxhcount then
  41. maxhcount = consec
  42. end if
  43. end if
  44. ELSE ' if it's not, we reset the run
  45. consec = 1
  46. END IF
  47. ' And now we set the last value to this one
  48. LastValue = num
  49. LOOP
  50. PRINT "t count: " ;tcount; " h count: ";hcount
  51. PRINT "The maximum number of consecutive heads for " ; flipq; " coin flips is: " ;maxhcount
  52. PRINT "The maximum number of consecutive tails for " ; flipq; " coin flips is: " ;maxtcount
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 20
Reputation: anandarose is an unknown quantity at this point 
Solved Threads: 0
anandarose anandarose is offline Offline
Newbie Poster

Re: Max Consecutive Heads/Tails

 
0
  #8
Nov 1st, 2006
THANK YOU SO MUCH FOR YOUR HELP!

:cheesy: :mrgreen: :!: :lol: :p
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC