-1

Question looks a little weird I accept.

What I want to ask is this:

int main(){
srand(time(NULL));
for(int x=0;x<10000;x++){
int result = rand()%2;
if(result==0) cout << "Heads" << endl;
if(result==1) cout << "Tails" << endl;
return 0;
}}

So what I want to do is to simulate a huge amount of coin tosses. The output will look like this:

Heads
Heads
Heads
Tails
Heads
Tails
Tails
.... 

But I want a statement like

Total number of Heads= 4900, Tails= 5100 Longest Heads streak=7,Tails streak=8

at the top of the output. How can I do it? Since the loop involves cout best I have been able to do is to place the statement at the bottom of output.

EDIT: I managed to solve the problem by using arrays. I first used a loop and simulated the tosses and record them by assigning them to array elements, and then printed them using another loop, in the order I need them. Thanks for all answers, upvoted them all.

SarpSTA
  • 279
  • 2
  • 15
  • 2
    Time to investigate console GUI libraries. ncurses, anyone? – Lightness Races in Orbit Dec 23 '15 at 19:01
  • 1
    The simple solution is to only print what you want, which is your example statement. Don't print the result of every coin flip. – Aiden Deom Dec 23 '15 at 19:01
  • 2
    Why not write it to a file? Then you can just print out the result followed by printing the file? – Fantastic Mr Fox Dec 23 '15 at 19:03
  • 1
    `int main()`. Note the `int`. Or is this C? Under C99? Then it's alright but change tags then, please. – cadaniluk Dec 23 '15 at 19:04
  • 1
    Why not simply use an array with 10,000 elements? – enhzflep Dec 23 '15 at 19:04
  • 1
    The... unorthodox (is that the right word?) way would be to store the string in a `std::string` or a stream and print it out later. Probably inefficient as hell but feasible and easy. – cadaniluk Dec 23 '15 at 19:06
  • So many coin toss questions on SO! http://stackoverflow.com/questions/27218980/c-coin-toss-simulator-not-working?rq=1 – hookenz Dec 23 '15 at 19:22
  • @Matt Yeah but my case isn't directly related to coin toss. I need it to analyse repeating patterns etc. to *statistically* prove how statistics about lotteries or roulette are completely unnecessary. The question you referred is simply a case of non-shuffled seed for rand(). – SarpSTA Dec 23 '15 at 19:28
  • 1
    I can see that. But the number of coin toss questions on here suggests homework. I'd rather not give away a full answer just suggestions so you guys can learn. – hookenz Dec 23 '15 at 19:34
  • @Matt Not *homework*. Yeah it is for academics but it is also it is good C++ practice. I am modifying the code. I will simulate the tosses and record the results into arrays and print them in a separate loop. – SarpSTA Dec 23 '15 at 19:44

3 Answers3

2

The simplest solution is to keep track of the results of the tosses in a bitset, display the numbers of heads and tails, and then iterate through the bitset, display head for each 1 and tails for each zero.

Arthur Laks
  • 524
  • 4
  • 8
2

Do you really need to keep printing Heads, Tails?

Why not just keep two counters. i.e.

int main(argc, char* argv[])
{
    // Keep track of heads/tails counts
    int heads = 0, 
    int tails = 0, 

    srand(time(NULL));
    for (int x=0; x < 10000; x++){
      int result = rand()%2;
      if (result == 0) heads++;
      if (result == 1) tails++;
    }

    // print results    
    printf("Heads=%d, Tails=%d otherstuff=TODO\n", heads, tails);
    return 0;
}

Keep track of tails or heads streak with other variables. You'd need the maximums of heads/tails inited to zero. And a variable to say what the current streak length is, and whether you are currently looking at a head or tail streak.

If you really want to be printing out head/tail you could do the bitset idea like @Arthur Laks suggests, or use an array of char's so that you don't need to deal with bitsets. Or you could write the results to a file and read them back which is rather ugly. There are many ways to do this.


For the array method something like this.

char* results = new char[10000];
char* p = results;

Inside the loop. To indicate heads:

p[x] = 'H';

For tails:

p[x] = 'T';

You can then run through the array again if needed to calculate other statistical patterns if required.

This is about all I'm going to say because I think if I give you a full answer you've not learned anything.

hookenz
  • 36,432
  • 45
  • 177
  • 286
  • I need to print them, or stream them into a file. I need the results of every single toss individually as well for statistical patterns. – SarpSTA Dec 23 '15 at 19:24
  • 1
    I think you've just answered your own question. You do know the answer. So get on with it. :) – hookenz Dec 23 '15 at 19:40
2

You will need to keep track (store) the results of each coin toss. After the tossing is finished, print your summary then the results of each coin toss.

Since each coin is either heads or tails, you could represent heads as false and tails as true. This allows you to use a container of bool.

Also, there are only ever two values, so you can represent them as bits. Let's say heads is 1 and tails is zero. The C++ language has a structure called bitset which can efficiently store the bits until the tossing is completed.

When printing the results, iterate through the bitset container. If the value is 1, print "heads"; otherwise print "tails". Simple. Fast.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154