944,031 Members | Top Members by Rank

Ad:
Oct 29th, 2006
0

Max Consecutive Heads/Tails

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anandarose is offline Offline
20 posts
since Oct 2006
Oct 29th, 2006
0

Re: Max Consecutive Heads/Tails

This forum is for C and C++.
What you've posted seems to be visual basic.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 30th, 2006
0

Re: Max Consecutive Heads/Tails

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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 30th, 2006
0

Re: Max Consecutive Heads/Tails

Click to Expand / Collapse  Quote originally posted by WaltP ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anandarose is offline Offline
20 posts
since Oct 2006
Oct 31st, 2006
0

Re: Max Consecutive Heads/Tails

Click to Expand / Collapse  Quote originally posted by WaltP ...

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anandarose is offline Offline
20 posts
since Oct 2006
Oct 31st, 2006
0

Re: Max Consecutive Heads/Tails

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anandarose is offline Offline
20 posts
since Oct 2006
Nov 1st, 2006
0

Re: Max Consecutive Heads/Tails

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yankie Dave is offline Offline
6 posts
since Oct 2006
Nov 1st, 2006
0

Re: Max Consecutive Heads/Tails

THANK YOU SO MUCH FOR YOUR HELP!

:cheesy: :mrgreen: :!: :lol: :p
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anandarose is offline Offline
20 posts
since Oct 2006

This thread is more than three months old

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.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Wasnt sure where to put this
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how to update my program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC