1. ## Coin Toss

For a class I am taking, I have to write a program that looks at the out come of a coin toss for 1000, 10000, and 100000 tosses. I am also required to keep count of the longest sequence of heads or 1's and the longest sequence of alternating head and tails 1010101. heads = 1 tails = 0.
i am having trouble with the latter, I need help on keeping track of the sequences. Here is my code so far:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, heads = 0, tails = 0, x;
for(i = 1; i < 1000; i++){
x = rand() % 2;
if(x == 1)
if(x == 0)
tails++;
}
return 0;
}```

2. >> I am also required to keep count of the longest sequence of heads or 1's

when you encounter a head, increment 'count' and compare it to 'max' - if it's greater than 'max', set 'max' to 'count' and then 'count' to zero.

>> and the longest sequence of alternating head and tails 1010101

same as above, have a 'count' and 'max' variable to keep tally. create a 'last' variable to hold the value determined during the previous iteration. then, when you are inspecting the current toss, compare it with 'last' and, if they are different, do the same as above. if they are the same, just set 'count' to zero.

>> for(i = 1; i < 1000; i++){

that only does 999 iterations, btw...

3. Make some counters for "last", "longest heads", "longest tails", and "current run". Track what the last coin flip was. If it's the same as it was last time, see what you need to update.

Pesudocode would basicly give you the answer, so I won't post it. But with what I've suggested above, you should have it in no time.

Curses! Foiled again.
[/edit]

Quzah.

4. I wrote a program to do this just because it interested me. Here's the results I got so you can compare it to something when you've finished the program I won't show my code until you've figured it out since it's homework and all...
Code:
```itsme@dreams:~/C\$ ./cointoss
1000 tosses:
max tails run: 9
max alternate: 10

10000 tosses:
max tails run: 11
max alternate: 12

100000 tosses:
max tails run: 18
max alternate: 17```
Of course, the numbers vary a bit between runs, but not much

5. Originally Posted by kazuakijp
For a class I am taking, I have to write a program

...
Well, since it's a programming class, I guess the first thing I should do is start programming.

Now what?

How about pretending it's not a programming assignment, but a coin flipping assignment. You are given a coin and locked in a room with a white board and a marker. The assignment: toss the coin lots of times and keep track of the longest sequence of heads. (After you learn how to do this, the same algorithm will undoubtedly be applicable to sequences of tails. Worry about the 101010 stuff later.)

How would you do it? Well, since it's a coin-flipping assignment, better get to flipping the coin, right? Or, maybe, just maybe, you should map out a plan before you start flipping.

1. Erase the board.

2. Flip the coin

2a. If it's tails, do nothing (we are only interested in heads right now).

2b. If it's heads, make a mark on the board (this will be a tally of the number of heads in the sequence).

3. Flip again.

3a. If it's heads, make another mark

3b. If it's tails, count the number of marks and write this number in a place on the board where it won't be disurbed by further counting. (This is where you will keep track of the maximum-length sequence of heads.) Now erase the tally marks so that you will be ready for the next time you get heads.

4. Flip again.

4a. If it's tails, do nothing.

4b. If it's heads, make a tally mark

Now you see the pattern. If only you had a computer, you can do the whole thing in a program loop:

Before the loop begins, set a counter, say heads_counter, equal to zero. Set another counter, say max_heads_count, equal to zero.

Now flip the coin repeatedly (Probably use rand(), or something.)

Whenever you see tails, see if the value of heads_counter is greater than the value of max_heads_count. If it is, set max_heads_count equal to the value of heads_counter. In any case set the value of heads_counter back to zero.

(Sound of repeated pounding on the door of the experiment room.)

Hey, lemme outta here! I got some programming to do.

Can you make a program now? I'll bet you can (assuming you know how to count and how to add in your programming language of choice).

Regards,

Dave