5
\$\begingroup\$

This program is supposed to calculate the mathemathical maximum of helpful flags you can have on the nth day, starting with 0 helpful flags, using the StackExchange system of gaining more possible flags, meaning every 10 helpful flags you gain one possible one, immediately.

#include <stdio.h>
// <summary>
// Small C program to simulate how many helpful flags someone could have raised on both comments and posts
// Assumptions:
// - Flags would be immediately marked helpful or near immediately
// - Every 10 flags, you get one extra flag per day, up to 100 total flags.
// - No declined flags
// @Author: Magisch - http://stackoverflow.com/users/5389107/magisch
// @License: CC0
// </summary>
int main(void) {
 int nDays = 0; // Days to simulate for
 int totalFlags = 0; 
 int dailyFlags = 10;
 int bonusCountdown = 0;
 int totalFlagsBeforeCalc = 0;
 printf("Enter the amount of days you want to simulate flags for:\n");
 scanf("%d ", &nDays);
 printf("Computing maximum helpful comment flags and post flags for %d days.\n",nDays);
 for (int i = 0; i < nDays; i++) {
 totalFlags += dailyFlags;
 bonusCountdown += (totalFlags - totalFlagsBeforeCalc);
 totalFlagsBeforeCalc = totalFlags;
 while (bonusCountdown >= 10 && dailyFlags < 100) {
 dailyFlags++;
 totalFlags++;
 bonusCountdown -= 10;
 }
 printf("Day %d : total helpful flags: %d | daily available flags: %d\n", i+1, 2*totalFlags, dailyFlags);
 }
 printf("Simulated %d days. Total helpful flags: %d",nDays,2*totalFlags);
 return 0;
}

Example of the Results for n = 52 : http://pastebin.com/TbjRPjBn

I'm a beginner still in coding (second year apprentice), so please don't hold back on any critique.

asked Sep 21, 2016 at 11:48
\$\endgroup\$
2
  • \$\begingroup\$ Is the space in the scanf format string intentional or not? Because it may screw up the input. See this. \$\endgroup\$ Commented Sep 21, 2016 at 13:38
  • \$\begingroup\$ @Lundin I wasn't giving too much thought to that, similarly to how I don't really sanitize the input. Goal was to stop stdin from having leftover whitespace in there. But you're right, I should do more input checking. \$\endgroup\$ Commented Sep 21, 2016 at 13:47

2 Answers 2

2
\$\begingroup\$
  1. Architecture. I'd put the simulation in a stand-alone function leaving the user input in main(). This facilitates code re-use and clearly identifies the data needed to perform and results from the simulation.

  2. As @Lundin commented, drop the trailing space from scanf("%d ", &nDays); to scanf("%d", &nDays);. The trailing space obilges the user to enter non-white-space (or EOF to occur) after the number for scanf() to return. As @wilx answered, check the return value of scanf().

  3. Avoid wide lines for review purposes. This format adjustment should be able to be done with auto-formatting.

     printf("Day %d : total helpful flags: %d | daily available flags: %d\n", i+1, 2*totalFlags, dailyFlags);
    // vs 
     printf("Day %d : total helpful flags: %d | daily available flags: %d\n", 
     i+1, 2*totalFlags, dailyFlags);
    
  4. Nice heading comments. I like at least a year in the header.

  5. i++ is fine. Either i++ or ++i in for (int i = 0; i < nDays; i++) { can be expected to create the same optimal code. Use what you feel best conveys the code meaning. Should a holy war occur on what you should use, defer to your group's style guide. If your group lacks a style guide, make/copy/develop one.

  6. Recommend \n

    // printf("Simulated %d days. Total helpful flags: %d", nDays, 2 * totalFlags);
    // v 
    printf("Simulated %d days. Total helpful flags: %d\n", nDays, 2 * totalFlags);
    
  7. Minor: As code is only working with small positive numbers, could use unsigned to help convey that "positiveness". Some find working with unsigned types error-prone though.

  8. Minor: Case for correct grammar?

     // printf("Computing maximum ... for %d days.\n", nDays);
     // Slightly obfuscated code that looked fun to post
     printf("Computing maximum ... %d for day%s.\n", nDays, &"s"[nDays == 1]);
    
  9. Pedantic: fflush(stdout) after a prompt to insure it is printed before user input. C does not require \n to flush.

     printf("Enter the amount of days you want to simulate flags for:\n");
     fflush(stdout); // add
    
answered Sep 22, 2016 at 18:22
\$\endgroup\$
2
\$\begingroup\$

You ought to check scanf return value and also that the input value is not negative. ++i please.

answered Sep 21, 2016 at 12:21
\$\endgroup\$
5
  • 3
    \$\begingroup\$ i++ or ++i doesn't matter when i is an primitive type and in an expression on its own. \$\endgroup\$ Commented Sep 21, 2016 at 12:29
  • \$\begingroup\$ @ratchetfreak: It is a style issue and good practice issue. \$\endgroup\$ Commented Sep 21, 2016 at 12:52
  • \$\begingroup\$ But arr[i++] is faster than arr[++i] when instruction level parallelism is involved due to data dependencies. So ++i is not always best. \$\endgroup\$ Commented Sep 21, 2016 at 13:07
  • \$\begingroup\$ @ratchetfreak: First, arr[i++] and arr[++i] obviously have different meanings and index different elements of the array. One of them is correct and the other is not, depending on the surrounding code. Second, there is no array to index in OP, so your point is moot here. \$\endgroup\$ Commented Sep 21, 2016 at 13:12
  • \$\begingroup\$ You should never use i++ or ++i together with other operands in the same expression, period. That has nothing to do with style and everything to do with program safety. There is never a performance reason to mix them with other operands either, doing so is just wannabe-programmer posing. \$\endgroup\$ Commented Sep 21, 2016 at 13:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.