Coin Toss Simulator. C++ Write a class named Coin. The Coin class should have th
ID: 3573311 • Letter: C
Question
Coin Toss Simulator. C++ Write a class named Coin. The Coin class should have the following member variables: A string named sideUp. The sideUp member variable will holder either "heads" or "tails" indicating the side of the coin that is facing up. The Coin class should have the following member functions: A default constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp member variable accordingly. A void member function named toss that simulates the tossing of the coin. When the toss member function is called, it randomly determines the side of the coin that is facing up and sets the sideUp member variable accordingly. A member function named getSideUp that returns the value of the sideUp member variable. Write a program that demonstrates the Coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. The program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes.
// Chapter 13, Programming Challenge 12: Coin Toss Simulator
#include <iostream>
#include "Coin.h"
int main()
{
// Variables
// To hold the number of times heads is facing up
int headCount = 0;
// To hold the number of times tails is facing up
int tailCount = 0;
// Create an instance of the Coin class.
Coin c;
// Display the side that is initially facing up.
cout << "The side initially facing up is: " << c.getSideUp() << endl;
cout << "Now tossing the coin 20 times... " << endl;
// Toss the coin 20 times.
for(int i=0; i<20; i++)
{
// Toss the coin.
c.toss();
//Display the side facing up.
string toss = c.sideUp();
cout << toss << endl;
//Determine if "heads" if facing up.
if(c.sideUp() == "heads")
{
//If so, increment the heads counter variable.
headCount++;
}
else
{
//If not, increment the tails counter variable.
tailCount++;
}
}
// Display the number of times heads was facing up.
cout << "Heads was facing up... " << headCount << endl;
// Display the number of times tails was facing up.
cout << "Tails was facing up... " << tailCount << endl;
return 0;
}
// Specification file for the Coin class
#ifndef COIN_H
#define COIN_H
#include <string>
using namespace std;
class Coin
{
private:
// The side facing up
string sideUp;
public:
// Constructor
Coin()
{
// Simulates tossing of the coin
int toss = rand() % 2;
if(toss == 0)
{
sideUp = "heads";
}
else
{
sideUp = "tails";
}
// Returns the side facing up
string getSideUp()
{
return sideUp;
}
};
#endif
Explanation / Answer
// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
using namespace std;
class Coin
{
private :
string sideUp;
public :
Coin()
{
int s = rand()%2;
if(s == 0)
{
sideUp = "heads";
}
else
{
sideUp = "tails";
}
}
void toss()
{
int s = rand()%2;
if(s == 0)
{
sideUp = "heads";
}
else
{
sideUp = "tails";
}
}
string getSideUp()
{
return sideUp;
}
};
int main()
{
srand(time(NULL));
int headCount = 0;
int totalToss = 20;
Coin coin;
cout << "Coins will be tossed " << totalToss << " times ";
cout << "Initial value on top : " << coin.getSideUp() << endl;
for(int i=0; i<totalToss; i++)
{
coin.toss();
string s = coin.getSideUp();
cout << "Toss: " << s << endl;
if(s.compare("heads") == 0)
{
headCount++;
}
}
cout << "Heads facing up: " << headCount << endl;
cout << "Tails facing up: " << (20-headCount) << endl;
return 0;
}
/*
output:
Coins will be tossed 20 times
Initial value on top : heads
Toss: tails
Toss: heads
Toss: heads
Toss: heads
Toss: tails
Toss: tails
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: tails
Toss: heads
Toss: heads
Toss: tails
Toss: tails
Toss: heads
Toss: tails
Toss: tails
Toss: heads
Heads facing up: 12
Tails facing up: 8
*/