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:

'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

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.

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

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

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.

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

THANK YOU SO MUCH FOR YOUR HELP!

:cheesy: :mrgreen: :) :!: :confused: :lol: :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.