I have this problem which says, "A programmer can use the random feature to emulate the flipping of a coin. For example, if the generator produces a number 0 or 1, the value 0 can be assigned heads and the value 1 can be assigned tails.
Write a program that "flips" a coin 50 times. Count the number of times the result is heads. Count the number of times the result is tails."

I am using Flowgorithim (a flowchart and algorithim combo) and so far I have:
Declare Integer heads
Declare Integer tails
Declare Integer flipCoin
Declare Integer count
Display "This program emulates 50 coin tosses of heads (0) or tails (1): "
For count 1 = 1 to 50
Set heads = 0
Set tails = 1
Set flipCoin = random(2)
If flipCoin == tails Then
Set flipCoin = tails
Else
Set flipCoin = heads
End If
Display flipCoin
End For
Display "Total number of heads: ", heads
Display "Total number of tails: ", tails

Simply, I am confused on how to total up the heads and tails and then display them. Could someone help?

Thanks

Recommended Answers

All 3 Replies

If flipCoin == tails Then
Set flipCoin = tails

Look at this code. Does it do anything? If not, take it out.

Look at all your code. You need to stick some comments in there. Figure out how you would do it without a computer. Then code. What do you do? Exactly? Do you keep a tally? Do you add one to anything anywhere? Because you do not add anywhere in the program above.

For count 1 = 1 to 50
Set heads = 0
Set tails = 1

Think about the above. If heads and tails are the totals (and they are according to your printout), can you initialize them inside of a loop?

I don't know. I think I can? This seems so simple but I cannot figure it out.
It seems that when I set heads = 0 and tails = 1, that it always going to be heads = 0 and tails = 1. It'll never change! Should I change this completely or somehow initialize (which I don't know what to initialize here).

It seems that when I set heads = 0 and tails = 1, that it always going to be heads = 0 and tails = 1. It'll never change!

Hence the problem.

Display "Total number of heads: ", heads
Display "Total number of tails: ", tails

heads is the total number of heads in 50 flips. tails is the total number of tails in 50 flips. Seems like heads plus tails needs to add up to 50 by the time this printout happens.

Or does heads and tails mean something different? Hard to say since the names are vague. Perhaps totalHeads and totalTails instead? Vague names and no comments. Hard to say what these variables are meant to keep track of. Perhaps you yourself got confused what they were supposed to represent and that is the problem? Name the varibles as precisely as you can and stick in some comments. That's not only so that other people can follow, it's so YOU don't get confused either. My guess is once you rename your variables accurately and stick some comments in there, the error will jump right out at you.

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.