#include #include #include #include // a program to simulate the tossing of a coin NUMTOSSES times int main() { const int NUMTOSSES = 1000; int heads = 0; // initialize heads count int tails = 0; // initialize tails count int i; float flip, perheads, pertails; // simulate NUMTOSSES tosses of a coin srand(time(NULL)); for (i = 1; i <= NUMTOSSES; i++) { flip = float(rand())/RAND_MAX; // scale the number between 0 and 1 if (flip > 0.5) heads = heads + 1; else tails = tails + 1; } perheads = (heads / float (NUMTOSSES)) * 100.0; // calculate heads percentage pertails = (tails / float (NUMTOSSES)) * 100.0; // calculate tails percentage cout << "\nHeads came up " << perheads << " percent of the time"; cout << "\nTails came up " << pertails << " percent of the time" << endl; return 0; }