/*--------------------------------------------------------------------*/ /* Problem chapter5_12 */ /* */ /* This program simulates tossing an "fair" coin. */ /* The user enters the number of tosses. */ /*--------------------------------------------------------------------*/ #include #include #include #include using namespace std; int rand_int(int , int ); int main() { /* Declare variables and function prototypes. */ int head=0, total=0; /* Prompt user for number of tosses. */ cout << "\nEnter number of fair coin tosses: "; cin >> total; srand(time(NULL)); /* time(NULL) returns seconds from Jan 1, 1970 GMT */ /* Toss coin the required number of times, and */ /* keep track of the number of heads. */ for (int toss=1; toss <= total; toss++) if (rand_int(1,2) == 1) head++; /* Print results. */ cout << "\nNumber of tosses: " << total << endl; cout << "Number of heads: " << head << endl; cout << "Number of tails: " << total-head << endl; cout << "Percentage of heads: " << 100.0 * head/total << endl; cout << "Percentage of tails: " << 100.0 * (total-head)/total << endl; /* Exit program. */ return 0; } /*------------------------------------------------------------------*/ /* (rand_int function from page 185) */ /* This function generates a random integer */ /* between specified limits a and b (a