From 8c8386740bd8bdd7f6973f10c6f66d178a5fe9ad Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 6 Apr 2025 16:55:54 +0200 Subject: [PATCH] Fix: no need for the if condition there Instead check if no arguments were given and exit sooner. --- src/main.c | 55 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main.c b/src/main.c index bcae2dd..63d28e3 100644 --- a/src/main.c +++ b/src/main.c @@ -12,35 +12,34 @@ int main(int argc, char** argv) { srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds } - // if we have input; perform coinflips for those times - if (argc > 1) { - for (uint32_t i = 1; i < argc; ++i) { - int n = 0; - uint32_t headsc = 0; - uint32_t tailsc = 0; - - // get the integer from the string, return 1 if an error occurred - errno = 0; - long c = strtol(argv[i], NULL, 10); - if (errno == EINVAL) { - fprintf(stderr, "syntax error for string: %s\n", argv[i]); - return 1; - } - - // perform for the inputted count - for (long j = 0; j < c; ++j) { - if (n == 0) n = rand(); - (void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter - n >>= 1; // shift the random integer right by 1 - } - - // print the results - printf("results:\n heads: %u\n tails: %u\n", headsc, tailsc); - } + // just print whether it's heads or tails + if (argc == 1) { + printf("%s\n", (rand() & 1) ? "heads" : "tails"); return 0; } - // just print whether it's heads or tails - printf("%s\n", (rand() & 1) ? "heads" : "tails"); - return 0; + // if we have input; perform coinflips for those times + for (uint32_t i = 1; i < argc; ++i) { + int n = 0; + uint32_t headsc = 0; + uint32_t tailsc = 0; + + // get the integer from the string, return 1 if an error occurred + errno = 0; + long c = strtol(argv[i], NULL, 10); + if (errno == EINVAL) { + fprintf(stderr, "syntax error for string: %s\n", argv[i]); + return 1; + } + + // perform for the inputted count + for (long j = 0; j < c; ++j) { + if (n == 0) n = rand(); + (void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter + n >>= 1; // shift the random integer right by 1 + } + + // print the results + printf("results:\n heads: %u\n tails: %u\n", headsc, tailsc); + } }