Question

c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

c++

Program 2: Coin Toss Simulator
For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below.

Your program should have two sections that correspond to Problems 12 and 13:

1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your program, write and instantiate a Game class. The Game class should contain Coin objects for a quarter, dime, and nickel, as described in the problem. Your Game class should have at least the following two public member functions:

a) void playGame(); This function will begin an individual game at the start and play it to completion (i.e., the balance is either $1 or greater than $1). It will report whether that particular game was won or not.

b) void runSimulation(); This function will play 50 games to completion and keep track of the number of wins and losses. It will then report that information to the user.   

Note that you will need the random number generator for these problems.   

12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp field accordingly. A method named getSideUp that returns the value of the sideUp field Write a program that demonstrates the Coin class. The 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 program should create an

13. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the coin is CISC-185 Prof. M. Kadri added to your balance if it lands heads-up. For example, if the quarter lands heads-up 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when your balance reaches one dollar or more. If your balance is exactly one dollar, you win the game. You lose if your balance exceeds one dollar

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Program 12:

//Coin.h

#include<iostream>

#include<string>

#include<ctime>

#include<cstdlib>

using namespace std;

class Coin

{

string sideUp;

public:

Coin();

void toss();

string getSideUp();

};

=============================

//Coin.cpp

#include"Coin.h"

Coin::Coin()

{

int value=rand();

value%=2;

if( value == 1)

{

sideUp="heads";

}

else

{

sideUp="tails";

}

}

void Coin::toss()

{

int value=rand();

value%=2;

if( value == 1)

{

sideUp="heads";

}

else

{

sideUp="tails";

}

}

string Coin::getSideUp()

{

return sideUp;

}

======================================

//Main.cpp

#include <iostream>

#include<ctime>

#include<cstdlib>

#include"Coin.h"

#define SIZE 3

using namespace std;

int main() {

Coin coin;

struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

int heads=0,tails=0;

// srand ( (time_t)ts.tv_nsec );

srand ( time(0) );

for(int i = 0; i < 20; i++)

{

coin.toss();

if(coin.getSideUp() == "heads")

{

cout<<"SideUp: "<<coin.getSideUp()<<endl;

++heads;

}

else{

cout<<"SideUp: "<<coin.getSideUp()<<endl;

++tails;

}

for(int j =0; j < 1000; j++);

}

cout<<"heads: "<<heads<<endl;

cout<<"Tails: "<<tails<<endl;

}

=============================

//Output

SideUp: tails
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: tails
SideUp: tails
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: tails
SideUp: tails
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: tails
SideUp: heads
SideUp: tails
heads: 11
Tails: 9
============================

//Program 13

//Game.h

#include"Coin.h"

class Game

{

Coin quarter, dime, nickel;

double balance;

public:

Game();

void playGame();

void runSimulation();

};

===============================

//Game.cpp

#include"Game.h"

Game::Game()

{

balance=0;

}

void Game::playGame()

{

int value;

//srand ( time(0) );

for(;;)

{

//if random number generated 25, 10 or 5 add that to balnce

value=rand()%50;

//cout<<"Value: "<<value<<endl;

if(value%25 == 0) //it's quarter

{

//cout<<"0.25\n";

balance+=0.25;

}

else if (value%10 == 0) //it's dime

{

//cout<<"0.10\n";

balance+=0.10;

}

else if(value%5 == 0) //it's nickel

{

//cout<<"0.05\n";

balance+=0.05;

}

if(balance >= 1)

{

//cout<<"bal: "<<balance<<endl;

break;

}

}

}

void Game::runSimulation()

{

//this will play 50 games and report to user

int wins=0,loose=0;

for(int i = 0; i < 50; i++)

{

playGame();

//cout<<"bal: ";

//cout<<(balance*100) <<endl;

if( (balance*100) > 100)

{

++loose;

}

else if ((balance*100) ==100)

{

++wins;

}

}

cout<<"You won "<<wins<<" times!!\n";

cout<<"You loose "<<loose<<" times!!\n";

}

=============================

//Main modified to include both Coin and Game object in main.cpp

#include <iostream>

#include<ctime>

#include<cstdlib>

//#include"Coin.h"

#include"Game.h"

#define SIZE 3

using namespace std;

int main() {

Coin coin;

struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

int heads=0,tails=0;

// srand ( (time_t)ts.tv_nsec );

srand ( time(0) );

for(int i = 0; i < 20; i++)

{

coin.toss();

if(coin.getSideUp() == "heads")

{

cout<<"SideUp: "<<coin.getSideUp()<<endl;

++heads;

}

else{

cout<<"SideUp: "<<coin.getSideUp()<<endl;

++tails;

}

for(int j =0; j < 1000; j++);

}

cout<<"heads: "<<heads<<endl;

cout<<"Tails: "<<tails<<endl;

//now play the Game

Game game;

//srand ( time(0) );

game.runSimulation();

}

/*Problem 12 and 13 together in main and output given below

SideUp: heads

SideUp: tails

SideUp: heads

SideUp: tails

SideUp: tails

SideUp: heads

SideUp: tails

SideUp: tails

SideUp: heads

SideUp: tails

SideUp: heads

SideUp: tails

SideUp: tails

SideUp: tails

SideUp: heads

SideUp: heads

SideUp: tails

SideUp: heads

SideUp: tails

SideUp: heads

heads: 9

Tails: 11

You won 1 times!!

You loose 49 times!!

*/

//One more output

 ./main
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: tails
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: heads
SideUp: tails
SideUp: heads
SideUp: heads
SideUp: heads
SideUp: tails
SideUp: heads
SideUp: tails
heads: 9
Tails: 11
You won 8 times!!
You loose 42 times!!

Add a comment
Know the answer?
Add Answer to:
c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • implement coinclass and driver program within one source file, i.e. do not separate class specifications with...

    implement coinclass and driver program within one source file, i.e. do not separate class specifications with implementation Its a c++ problem 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • A string named sideUp. The sideUp member variable will hold 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...

  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a...

    Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the...

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block...

    LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block dia- gram, use Random Number (0-1) to generate a random floating-point number x, in the range from 0 up to (but not including1. When run, ifx 20.5, assign the result of the coin toss to be Heads; otherwise, the result is Tails. Then, use a Select icon as shown below to decide which of two strings should be sent to a front-panel String Indicator...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Program 2: Design (pseudocode) and implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head...

  • LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : D...

    LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : Display Name , Description and Price of the Products Insert Coin : Allows user to Add a USD coin into the machine allowed types are : Nickel(0.05) , Dime(0.1) , Quarter(0.25) and Dollar (1) Buy: If the sufficient amount of coins are thrown into the machine , machine should dispense the cold drink , and remove it from its product inventory. If insufficient amount is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT