Udyant Wig <> writes:
> I was following Steve Summit's C programming notes at
> http://www.eskimo.com/~scs/cclass/notes/top.html
> and got inspired by the dice-rolling project. I have written a naive
> Gaussian distribution program that simulates throwing a specific number
> of n-sided dice a given number of times. The output is the expected
> bell curve along with the numerical value of each sum's occurences.
<snip>
> Being a novice, I request the readers of this newsgroup to look over the
> source to suggest improvements regarding readability, layout, style,
> correctness, idioms, etc. I would appreciate it very much.
>
> I have placed the source files on Pastebin at this URL
> http://pastebin.com/1PTVJrTN
I think it's good.
By the way, it's easier to comment on posted code though I know you were
probably worried about posting a long listing here. It is longish, but
I think it's probably within the length where people are more likely to
read it when posted directly than when it's referenced on a sharing site.
Anyway, I have only a few of remarks:
* Why have the function 'd' in file on its own? If it's going to be used
a lot other programs, it needs a much better name than 'd', and if it
isn't, why not just put it where it is used? (Either way, 'd' is not
really a good name for it -- too sort whatever the use).
* It's widely regarded a good style to put parentheses round all
macro bodies that are anything more the a constant or an identifier.
One day you'll forget that you wrote
#define LIMIT (MAXSUM) + 1
and you'll write LIMIT * 2.
* I'd just initialise my array where it is declared:
int sums[LIMIT] = {0};
rather than have an initsums function.
* Finally, I prefer to avoid having extra variables. Rather than:
int sum; /* Sum of the dice. */
int i;
for (i = 0; i < maxrolls; i++) {
sum = sumdice (NDICE);
++sums [sum];
}
I'd write:
for (i = 0; i < maxrolls; i++)
++sums[ sumdice(NDICE) ];
For one thing, after a while, you run out of good names for them.
That's already happening here: 'sumdice' is a better name than 'sum'
i.e. the function name says more than the extra variable does.
Anyway, it's good. Don't let my nit-picking get you down.
--
Ben.