HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Calculate maximum possible helpful flags for n days

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
maximumhelpfulpossiblecalculateforflagsdays

Problem

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 

// 
// 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
// 

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 = 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.

Solution

-
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.

-
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().

-
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);


-
Nice heading comments. I like at least a year in the header.

-
i++ is fine. Either i++ or ++i in for (int i = 0; i

-
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);


-
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.

-
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]);


-
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

Code Snippets

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);
// printf("Simulated %d days. Total helpful flags: %d", nDays, 2 * totalFlags);
//                                                 v 
printf("Simulated %d days. Total helpful flags: %d\n", nDays, 2 * totalFlags);
// 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]);
printf("Enter the amount of days you want to simulate flags for:\n");
 fflush(stdout);  // add

Context

StackExchange Code Review Q#142013, answer score: 2

Revisions (0)

No revisions yet.