coin toss

This is a discussion on coin toss within the C Programming forums, part of the General Programming Boards category; i had a homework assignment from C how to program 5th edition by dietel (#5.31). it says: Write a program ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    coin toss

    i had a homework assignment from C how to program 5th edition by dietel (#5.31).

    it says:

    Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times, and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time for a total of approximately 50 heads and 50 tails.]

    Code:
    #include <stdio.h>
    int flip( );
    int main( void )
    {
    int side;
    int toss;
    int heads = 0;
    int tails = 0;
    
    for ( toss = 1; toss <= 100; toss++ ){
        printf( "%d\n", flip( ));
        
        if (flip( ) == 0)
        heads++;
        else
        tails++;
    
            
    
    }
    printf( "heads was flipped %d times\n", heads );
    printf( "tails was flipped %d times\n", tails );
    
    return 0;
    
    }    
    int flip( )
    {
        int i = rand() % 2;
       
    
            if (i == 0)
                 return 0;
                 
            else
                 return 1;
                 
                 
           }
    output:
    1
    0
    1
    0
    0
    1
    1
    1
    1
    1
    1
    0
    0
    1
    0
    1
    1
    1
    1
    1
    1
    0
    1
    1
    1
    1
    0
    1
    1
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    1
    0
    0
    0
    1
    0
    0
    1
    0
    1
    1
    1
    0
    1
    1
    1
    0
    0
    1
    1
    0
    1
    1
    1
    1
    0
    1
    0
    1
    1
    1
    1
    1
    1
    1
    0
    0
    1
    0
    0
    0
    0
    0
    1
    0
    1
    0
    0
    0
    0
    1
    1
    0
    0
    1
    0
    0
    1
    0
    1
    heads was flipped 55 times
    tails was flipped 45 times


    when i run my program it produces the output every time. how can i make it (more random) produce a different output every time it's ran?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Use srand.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    thanks!!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    From your code, do it like this

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int flip( );
    
    int main( void )
    {
      int side;
      int toss;
      int heads = 0;
      int tails = 0;
    
      srand(time(NULL)); //     <- srand() here, just ONCE
    
      ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 02:02 AM
  2. Coin Toss
    By kazuakijp in forum C Programming
    Replies: 4
    Last Post: 08-31-2004, 10:02 PM
  3. coin toss program?
    By girliegti in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 11:09 AM
  4. Football - The AFC
    By Cshot in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-17-2002, 07:29 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21