This program is supposed to calculate the mathemathical maximum of helpful flags you can have on the n
th 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.
2 Answers 2
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);
toscanf("%d", &nDays);
. The trailing space obilges the user to enter non-white-space (or EOF to occur) after the number forscanf()
to return. As @wilx answered, check the return value ofscanf()
.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. Eitheri++
or++i
infor (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.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
You ought to check scanf
return value and also that the input value is not negative. ++i
please.
-
3\$\begingroup\$
i++
or++i
doesn't matter wheni
is an primitive type and in an expression on its own. \$\endgroup\$ratchet freak– ratchet freak2016年09月21日 12:29:55 +00:00Commented Sep 21, 2016 at 12:29 -
\$\begingroup\$ @ratchetfreak: It is a style issue and good practice issue. \$\endgroup\$wilx– wilx2016年09月21日 12:52:10 +00:00Commented Sep 21, 2016 at 12:52
-
\$\begingroup\$ But
arr[i++]
is faster thanarr[++i]
when instruction level parallelism is involved due to data dependencies. So++i
is not always best. \$\endgroup\$ratchet freak– ratchet freak2016年09月21日 13:07:33 +00:00Commented Sep 21, 2016 at 13:07 -
\$\begingroup\$ @ratchetfreak: First,
arr[i++]
andarr[++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\$wilx– wilx2016年09月21日 13:12:41 +00:00Commented 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\$Lundin– Lundin2016年09月21日 13:13:34 +00:00Commented Sep 21, 2016 at 13:13
scanf
format string intentional or not? Because it may screw up the input. See this. \$\endgroup\$