diff options
| author | Jacob McDonnell <jacob@simplelittledream.com> | 2021-11-01 11:10:32 -0400 |
|---|---|---|
| committer | Jacob McDonnell <jacob@simplelittledream.com> | 2021-11-01 11:10:32 -0400 |
| commit | cb7e1688c61dc6861d06b56475f46d5c7ac75f91 (patch) | |
| tree | 012dedf66466fd30a927f740169bc852617010d4 /src | |
| parent | fe5e287e5d517bb301ab403bcc0b92ff89dbdc3e (diff) | |
Updated coin to allow for multiple flips
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/coin/coin.c | 49 | ||||
| -rw-r--r-- | src/cmd/dice/dice.c | 2 | ||||
| -rw-r--r-- | src/cmd/echo/echo.c | 2 |
3 files changed, 46 insertions, 7 deletions
diff --git a/src/cmd/coin/coin.c b/src/cmd/coin/coin.c index 09ca479..5c58b3c 100644 --- a/src/cmd/coin/coin.c +++ b/src/cmd/coin/coin.c @@ -1,15 +1,54 @@ #include <stdio.h> #include <stdlib.h> #include <time.h> +#include <string.h> + +int coinFlip (int times); int -main (void) +main (int argc, char *argv[]) { - int c; - time_t t; - srand((unsigned) time(&t)); + int nflag = 0, i, times = 1, tails, heads; + argc--; - printf("%s\n", (rand() % 2) ? "Heads" : "Tails"); + for (i = 1; i <= argc; i++) { + if (strcmp(argv[i], "-n") == 0) + nflag++; + else if (nflag == 1) + times = atoi(argv[i]); + if (times == 0) { + printf("Non Integer Value Inputted\n"); + return -1; + } + } + if (times == 1) + printf("%s\n", coinFlip(times) ? "Heads" : "Tails"); + else { + tails = coinFlip(times); + heads = times - tails; + float ph = (float)heads/times, pt = (float)tails/times; + + printf("Heads: %d\nTails: %d\nProb Heads: %f\nProb Tails: %f\n", heads, tails, ph, pt); + } return 0; } + +/* coinFlip: Flips a coin n numbers of times and returns the number of tails */ +int +coinFlip (int times) +{ + int tails, i, c; + time_t t; + srand((unsigned) time(&t)); + + tails = 0; + + for (i = 0; i < times; i++) { + c = rand() % 2; + if (c == 1) + tails++; + } + + return tails; +}
\ No newline at end of file diff --git a/src/cmd/dice/dice.c b/src/cmd/dice/dice.c index b31670f..f211d67 100644 --- a/src/cmd/dice/dice.c +++ b/src/cmd/dice/dice.c @@ -12,4 +12,4 @@ main (void) printf("%d\n", (rand() % sides) + 1); return 0; -} +}
\ No newline at end of file diff --git a/src/cmd/echo/echo.c b/src/cmd/echo/echo.c index 4733830..3dc8334 100644 --- a/src/cmd/echo/echo.c +++ b/src/cmd/echo/echo.c @@ -14,4 +14,4 @@ main (int argc, char *argv[]) printf("%s%c", argv[i], (i == argc) ? ((nflag == 1) ? '\0' : '\n') : ' '); } return 0; -} +}
\ No newline at end of file |
