patterncMinor
Statistical simulation
Viewed 0 times
statisticalsimulationstackoverflow
Problem
I've written the following program as a submission to the German Federal Computer Science Competition for highschool students. The program makes the following simulation with a given number of days and a given α. In order to simplify my code, I use a shellscript to run multiple simulations in parallel. You can get all of the code from GitHub: (in directory 5).
I know that my implement correctly the fact that the happiness is calculated once a day. I calculate it just when needed.
Question
(Translated from German, see source)
The CEO of Nash Ltd. wants to see her workers happy. Thus, she likes
to provide them with free coffee. [...] After a period of ten days
where the CEO's assistant makes coffee, the workers have to make
coffee on their own. Every evening, the remaining coffee is poured
away.
The carafe can contain up to ten cups of coffee, there are 15 workers.
Each worker likes to drink three cups a day, one in the morning (8 to
10 AM), one at noon (12 to 2 PM) and one in the afternoon (3 to 5 PM).
I each of these timeslots the workers arrive in a random order and
behave like this:
If there is coffee left in the carafe they pour one cup and leave. If
the carafe is empty, the behavior depends on whether they are happy or
not. If they are, they brew a new carafe and take one cup. If not, the
leave in anger without drinking any coffee.
Whether a worker is happy on a certain day is decided right after they
get up. It depends on how often they drank coffee in the last ten days
and on how often they brew coffee in that time. The exact definition
reads:
If a person drank n cups in the last ten days and brew coffee for
m times, he or she is happy iff n >= 10 and m/n < α; α is a personal threshold.
Write a program that simulates the worker where α is equal for all
of them. How many cups of coffee is a worker drinking on the long-term
average every day depending on α?
stats.c
```
#include
#include
I know that my implement correctly the fact that the happiness is calculated once a day. I calculate it just when needed.
Question
(Translated from German, see source)
The CEO of Nash Ltd. wants to see her workers happy. Thus, she likes
to provide them with free coffee. [...] After a period of ten days
where the CEO's assistant makes coffee, the workers have to make
coffee on their own. Every evening, the remaining coffee is poured
away.
The carafe can contain up to ten cups of coffee, there are 15 workers.
Each worker likes to drink three cups a day, one in the morning (8 to
10 AM), one at noon (12 to 2 PM) and one in the afternoon (3 to 5 PM).
I each of these timeslots the workers arrive in a random order and
behave like this:
If there is coffee left in the carafe they pour one cup and leave. If
the carafe is empty, the behavior depends on whether they are happy or
not. If they are, they brew a new carafe and take one cup. If not, the
leave in anger without drinking any coffee.
Whether a worker is happy on a certain day is decided right after they
get up. It depends on how often they drank coffee in the last ten days
and on how often they brew coffee in that time. The exact definition
reads:
If a person drank n cups in the last ten days and brew coffee for
m times, he or she is happy iff n >= 10 and m/n < α; α is a personal threshold.
Write a program that simulates the worker where α is equal for all
of them. How many cups of coffee is a worker drinking on the long-term
average every day depending on α?
stats.c
```
#include
#include
Solution
Overall, this is very clean and I have only a few small improvements.
1) In
could be a single for loop.
2) In
could be
3) In
4) Comments like
describe what the code is doing, not why it's doing it.
5) There are a few small style inconsistencies (especially with regards to spaces after commas and semicolons). Consider using astyle and writing an
1) In
happy:/* get amount of cups drunk, cans brewed */
for (i = 0; i < STAT_DAYS; i++) cup_count += (*worker)[i].cups;
for (i = 0; i < STAT_DAYS; i++) can_count += (*worker)[i].cans;could be a single for loop.
2) In
day (by the way, this should probably be simulate_day or something):for (i = 0;i workers[i][day_mod].cups = 0;
company->workers[i][day_mod].cans = 0;
}could be
for (i = 0; i workers[i][day_mod] = {
.cups = 0,
.cans = 0;
}
}3) In
main, you calloc and free a variable; why not just allocate it on the stack?4) Comments like
/* Main function */
int main(int argc,char **argv) {
/* allocate and set to zero */
company = calloc(sizeof *company,1);
/* print output */
printf("%.6lf\t%.6lf\n",describe what the code is doing, not why it's doing it.
5) There are a few small style inconsistencies (especially with regards to spaces after commas and semicolons). Consider using astyle and writing an
.astylerc file for yourself.Code Snippets
/* get amount of cups drunk, cans brewed */
for (i = 0; i < STAT_DAYS; i++) cup_count += (*worker)[i].cups;
for (i = 0; i < STAT_DAYS; i++) can_count += (*worker)[i].cans;for (i = 0;i < WORKER_COUNT; i++) {
company->workers[i][day_mod].cups = 0;
company->workers[i][day_mod].cans = 0;
}for (i = 0; i < WORKER_COUNT; i++) {
company->workers[i][day_mod] = {
.cups = 0,
.cans = 0;
}
}/* Main function */
int main(int argc,char **argv) {
/* allocate and set to zero */
company = calloc(sizeof *company,1);
/* print output */
printf("%.6lf\t%.6lf\n",Context
StackExchange Code Review Q#19633, answer score: 8
Revisions (0)
No revisions yet.