Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Udyant Wig <udyantw@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Request for source code review |
| Date | 2012-08-31 20:03 +0530 |
| Organization | A noiseless patient Spider |
| Message-ID | <87y5kvdtcm.fsf@panda.goosenet.in> (permalink) |
| References | <87bohty09k.fsf@panda.goosenet.in> |
I thank all readers who responded to my request.
I have revised the source taking into account many of the remarks made.
Follows the new listing.
/* utils.h begins */
#include <stdlib.h>
extern int uniform_int_in (int min, int max);
/* utils.h ends */
/* utils.c begins */
#include "utils.h"
/* Pick a number in [1, n] with uniform probability. */
int
uniform_int_in (int min, int max) {
return min + (rand () / (RAND_MAX / max + 1));
}
/* utils.c ends */
/* gauss.h begins */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "utils.h"
#define NDICE 5 /* The number of dice to be rolled. */
#define SIDES 6 /* Each die has these many sides. */
#define MAXSUM (SIDES * NDICE) /* The maximum possible sum of NDICE
dice.
*/
#define LIMIT ((MAXSUM) + 1) /* This constant is used:
0) to declare the array of sums
1) as a loop limit
*/
#define ROLLS 100000 /* The number of throws of the dice */
#define SCALE 100 /* Used to fit the histogram into the
screen width. A lower value
implies longer bars.
*/
extern int roll_die (int n);
extern int sumdice (int ndice);
extern void fillsums (int sums [], int maxrolls);
extern void printsums (int sums [], int begin_limit, int end_limit);
extern void histogram (int sum, int n, int scale);
extern void histsums (int sums [], int begin_limit, int end_limit);
/* gauss.h ends */
/* gauss.c begins */
#include "gauss.h"
/* Given n, throw an n-sided die. */
int
roll_die (int n) {
return uniform_int_in (1, n);
}
/* Roll the given number of dice and return their sum. */
int
sumdice (int ndice) {
int i;
int sum = 0;
for (i = 0; i < ndice; i++)
sum += roll_die (SIDES);
return sum;
}
/* For the specified number of dice throws: calculate the sum of the
given number of dice, and increment the array slot whose index is
the sum.
*/
void
fillsums (int sums [], int maxrolls) {
int i;
for (i = 0; i < maxrolls; i++)
++sums [sumdice (NDICE)];
}
/* Print the number of occurrences of the various sums. */
void
printsums (int sums [], int begin_limit, int end_limit) {
int i;
for (i = begin_limit; i < end_limit; i++)
printf ("%2d: %d\n", i, sums [i]);
}
/* Print a horizontal bar histogram for a given sum. */
void
histogram (int sum, int n, int scale) {
int i;
printf ("%2d: ", sum);
for (i = 0; i < n / scale; i++)
putchar ('=');
putchar ('\n');
}
/* Print a histogram of the occurrences. */
void
histsums (int sums [], int begin_limit, int end_limit) {
int i;
if ((NDICE > 0) && (SIDES > 1)) {
for (i = begin_limit; i < end_limit; i++)
histogram (i, sums [i], SCALE);
}
else {
if (NDICE < 1) {
fputs ("NDICE set to insane value\n", stderr);
fputs ("So, no histogram.\n", stderr);
}
if (SIDES < 2) {
fputs ("SIDES set to insane value\n", stderr);
fputs ("So, no histogram.\n", stderr);
}
}
}
/* gauss.c ends */
/* main.c begins */
#include "gauss.h"
int
main (void) {
int sums [LIMIT] = {0}; /* We use only indices
NDICE to LIMIT - 1
for indexing the sums of
the given of dice. We waste
sizeof (int) * NDICE
bytes.
*/
srand (time (NULL)); /* Seed the RNG using the
current time.
*/
fillsums (sums, ROLLS); /* Count the sums. */
printsums (sums, NDICE, LIMIT); /* Print how many of each sum
counted.
*/
putchar ('\n');
histsums (sums, NDICE, LIMIT); /* Print a histogram of
the same.
*/
exit (EXIT_SUCCESS); /* Done. So, exit. */
}
/* main.c ends */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 00:49 +0530
Re: Request for source code review Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-08-29 19:43 +0000
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 18:46 +0530
Re: Request for source code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-29 22:27 +0100
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 09:44 +0530
Re: Request for source code review Seungbeom Kim <musiphil@bawi.org> - 2012-09-07 17:31 -0700
Re: Request for source code review Ian Collins <ian-news@hotmail.com> - 2012-09-08 12:41 +1200
Re: Request for source code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 04:06 +0100
Re: Request for source code review 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-07 23:50 -0700
Re: Request for source code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-08 12:28 +0100
Re: Request for source code review "BartC" <bc@freeuk.com> - 2012-08-29 22:58 +0100
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 09:48 +0530
Re: Request for source code review Keith Thompson <kst-u@mib.org> - 2012-08-29 18:55 -0700
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 10:06 +0530
Re: Request for source code review lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2012-08-29 21:59 -0700
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 18:51 +0530
Re: Request for source code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-30 13:44 +0100
Re: Request for source code review lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2012-09-12 03:24 -0700
Re: Request for source code review Ian Collins <ian-news@hotmail.com> - 2012-08-30 17:04 +1200
Re: Request for source code review James Kuyper <jameskuyper@verizon.net> - 2012-08-30 07:43 -0400
Re: Request for source code review Ian Collins <ian-news@hotmail.com> - 2012-08-31 20:43 +1200
Re: Request for source code review Les Cargill <lcargill99@comcast.com> - 2012-08-31 21:18 -0500
Re: Request for source code review James Kuyper <jameskuyper@verizon.net> - 2012-09-01 20:34 -0400
Re: Request for source code review Les Cargill <lcargill99@comcast.com> - 2012-09-01 20:45 -0500
Re: Request for source code review Ian Collins <ian-news@hotmail.com> - 2012-09-02 15:11 +1200
Re: Request for source code review Les Cargill <lcargill99@comcast.com> - 2012-09-01 23:43 -0500
Re: Request for source code review Adrian Ratnapala <raka@scooter.fritz.box> - 2012-09-01 07:40 +0000
Re: Request for source code review Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-30 05:10 -0700
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-30 19:14 +0530
Re: Request for source code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-30 16:42 +0100
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-31 19:44 +0530
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-08-31 20:03 +0530
Re: Request for source code review Keith Thompson <kst-u@mib.org> - 2012-08-31 13:34 -0700
Re: Request for source code review Adrian Ratnapala <raka@scooter.fritz.box> - 2012-09-01 07:49 +0000
Re: Request for source code review Keith Thompson <kst-u@mib.org> - 2012-09-01 02:53 -0700
Re: Request for source code review pete <pfiland@mindspring.com> - 2012-08-31 21:15 -0400
Re: Request for source code review Jan Kuiken <jan.kuiken@quicknet.nl> - 2012-08-31 19:55 +0200
Re: Request for source code review Udyant Wig <udyantw@gmail.com> - 2012-09-02 15:39 +0530
Re: Request for source code review Jan Kuiken <jan.kuiken@quicknet.nl> - 2012-09-02 19:57 +0200
csiph-web