Author: Dann Corbit
Date: 17:13:23 07/24/03
Go up one level in this thread
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mtrand.h"
typedef struct tag_flips {
unsigned heads;
unsigned tails;
} flips;
flips pennies[1000];
flips nickel;
flips dime;
flips quarter;
int flipit()
{
return mtrand() % 2;
}
int compare(const void *rr, const void *ll)
{
flips *r = rr,
*l = ll;
if (r->heads > l->heads)
return 1;
if (r->heads < l->heads)
return -1;
return 0;
}
int main(void)
{
size_t i,
j;
mtsrand(4357 + (unsigned) time(NULL));
/* Let the contest begin: */
for (i = 0; i < 100; i++) {
for (j = 0; j < 1000; j++)
if (flipit())
pennies[j].heads++;
else
pennies[j].tails++;
if (flipit())
quarter.heads++;
else
quarter.tails++;
if (flipit())
dime.heads++;
else
dime.tails++;
if (flipit())
nickel.heads++;
else
nickel.tails++;
}
qsort(pennies, 1000, sizeof pennies[0], compare);
puts("bottom 25 pennies heads counts:");
for (i = 0; i < 25; i++) {
printf("pennies[%u].heads = %u\n", i, pennies[i].heads);
}
puts("top 25 pennies heads counts:");
for (i = 0; i < 25; i++) {
printf("pennies[%u].heads = %u\n", 999 - i, pennies[999 - i].heads);
}
printf("quarter.heads = %u\n", quarter.heads);
printf("dime.heads = %u\n", dime.heads);
printf("nickel.heads = %u\n", nickel.heads);
return 0;
}
This page took 0.01 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.