Originally Posted by
rogster001
A mention should go to whiteflags who was first through with a very good answer, that was acheived in a very compact program of around fifty actual lines, great work, I only failed it as it requested input for the number of balls in the game, which i had stated should be done programatically, a minor technicality i know..but a very worthy second place to whiteflags.
Yeah well I don't want to be a noodle incident. For those curious, this was my code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double uniform_deviate (int seed)
{
return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
}
void plinko (int buckets[], int nBuckets, int balls)
{
int i, ball, rightBounce;
srand((unsigned long)time(NULL));
for(ball = 0; ball < balls; ball++) {
rightBounce = 0;
for(i = 0; i < (nBuckets - 1); i++) {
if(uniform_deviate(rand()) > 0.5) {
++rightBounce;
}
}
++buckets[rightBounce];
}
}
int main ()
{
int nBuckets = 0;
int balls = 0;
int sum = 0;
int *buckets = NULL;
printf("enter the number of bottom pins: ");
scanf("%i", &nBuckets);
printf("enter the number of balls: ");
scanf("%i", &balls);
++nBuckets;
if((buckets = calloc(nBuckets, sizeof buckets[0])) != NULL) {
int i;
plinko(buckets, nBuckets, balls);
printf("\n\n|");
for(i = 0; i < nBuckets; i++) {
sum += buckets[i];
printf("%d|", buckets[i]);
}
printf("\n\ncheck sum = %d\n", sum);
free(buckets);
} else {
printf("Memory allocation failure\n");
}
return 0;
}