training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

HorT.c (558B)


      1 /* Exercise 5.31 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 int flip(void);
      7 
      8 int main()
      9 {
     10    int i;
     11    int heads, tails;
     12    heads = tails = 0;
     13 
     14    srand( time(NULL) );
     15 
     16    for(i = 1; i <= 100; i++) {
     17       if(flip())
     18 	 ++heads;
     19       else
     20 	 ++tails;
     21    } /* end for (i) */
     22 
     23    printf("Results:\n");
     24    printf("Heads: %d\nTails: %d\n", heads, tails);
     25 
     26    return 0;
     27 } /* E0F main */
     28 
     29 /* simulate a currency launch */
     30 int flip(void)
     31 {
     32    int n = rand() % 2;
     33 
     34    if(n)
     35       printf("Heads\n");
     36    else
     37       printf("Tails\n");
     38 
     39    return n;
     40 } /* eof flip() */
     41