From bea82bc7f5d65641620cc25ba8da1bb450473062 Mon Sep 17 00:00:00 2001 From: Deathsbreed Date: Tue, 4 Nov 2014 15:56:07 -0600 Subject: [PATCH] Fixed progress problem by using long long int --- README.md | 2 +- src/main.c | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2ef18a8..f78e74a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ CoinToss ======== -The creation of this program has a little story. I was in math class and a classmate was playing around with the probability simulator on his TI Calculator. He was trying to do as many coin tosses as he could, until finally he reached 9999, which was the limit of tosses for the simulator. So I decided that I would create this, which allows you to do more than that, specifically 2,147,473,649 more times than that. +The creation of this program has a little story. I was in math class and a classmate was playing around with the probability simulator on his TI Calculator. He was trying to do as many coin tosses as he could, until finally he reached 9999, which was the limit of tosses for the simulator. So I decided that I would create this, which allows you to do more than that, specifically 9223372036854765808 more trials than that. ### Compiling To compile make sure you have the GNU C Compiler installed (GCC) and just go to the root directory of this project and run: diff --git a/src/main.c b/src/main.c index 1145cea..da7b580 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ void printWarranty(); int main(int argc, char **argv) { // If inappropriate number of arguments if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2) - printf("Inappropriate amount of arguments."); + printf("Inappropriate amount of arguments.\n"); printf("Usage: %s [num_trials]\n", argv[0]); return 1; } @@ -65,33 +65,32 @@ int main(int argc, char **argv) { printCopyright(); - int trials = atoi(argv[1]); - int heads = 0; - int tails = 0; + long long int trials = atoi(argv[1]); + long long int heads = 0; + long long int tails = 0; // Setup the random number generator with the seed being the time srand(time(NULL)); - //int oldprogress = 0; + long long int oldprogress = 0; printf("Calculating random numbers...\n"); - //printf("Progress: 0 %%\n"); - for(int i = 0; i < trials; i++) { - /*int progress = i * 100 / trials; + printf("Progress: 0 %%\n"); + for(long long int i = 0; i < trials; i++) { + long long int progress = (i * 100) / trials; if(progress != oldprogress && progress % 10 == 0) { - printf("Progress: %i %%\n", progress); + printf("Progress: %lli %%\n", progress); oldprogress = progress; - }*/ - //if(i % 100000000 == 0) printf("Working...\n"); + } int r = rand() % 100 + 1; // Calculate random number between 1 and 100 if(r <= 50) heads++; else tails++; } - //printf("Progress: 100 %%\n"); + printf("Progress: 100 %%\n"); - printf("Done.\n"); - printf("Heads: %i\n", heads); - printf("Tails: %i\n", tails); + printf("Done.\n\n"); + printf("Heads: %lli\n", heads); + printf("Tails: %lli\n", tails); return 0; }