Advertisement
tolfasn

coinFlipper

Mar 7th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. //prototyping for custom function
  6. int coinFlipper(void);
  7.  
  8. int main()
  9. {  
  10.  
  11.    //intializes tracking variables
  12.    float flips = 1.0;
  13.    float heads = 0.0;
  14.    float tails = 0.0;
  15.  
  16.    //intializes placeholder for custom function
  17.    int test = 0;
  18.  
  19.    //loop to track the number of flips
  20.    for (int counter = 0; counter <= 5; counter++)
  21.    {
  22.          //multiplies flips to increase the # of flips
  23.          flips *= 10.0;
  24.         //resets deads/tails count at start of loop
  25.         heads = 0.0;
  26.         tails = 0.0;
  27.          for (int i = 0; i < flips; i++)
  28.          {  
  29.             //assigns variable test to the return value of coinFlipper()
  30.             test = coinFlipper();
  31.  
  32.             //checks if coinFlipper output is even
  33.             if (test % 2 == 0){
  34.                //If yes, +1 to heads
  35.                ++heads;
  36.             }
  37.             //If no, +1 to tails
  38.             else{
  39.                ++tails;
  40.             }
  41.          }
  42.          //outputs the results to the user
  43.          printf("\nNumber of Flips: %.f\n", flips);
  44.          printf("\nResults:\n");
  45.          printf("\nHeads: %.f || %.2f percent\n", heads, heads / flips * 100); //conversion for percentage
  46.          printf("\nTails: %.f || %.2f percent\n\n", tails, tails / flips * 100); //conversion for percentage
  47.          printf("\n------------------------\n\n");
  48.    }
  49.    system("pause");
  50.    return 0;
  51. }
  52.  
  53. //custom function to generate random int
  54. int coinFlipper(void)
  55. {  
  56.    int num = rand() % 100;
  57.    return(num);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement