2010 Lab-12

Lab elements include:
 • Classes
 • Constructors
 • Overloaded operators
 • #include files

Extra credit...

Recording a coin landing on its edge.

Copy your 2010/c/toss.cpp program to 2010/c/toss2.cpp

You may make some changes to the toss member function.
The function should now return "heads", "tails", or "edge".

How to test your function...

  1. Write some code in main that will flip your coin 1-million times.
     You may flip the coin 10-million, 100-million, etc.
	 Then adjust your expected outcome.

  2. Record how many heads, tails, and edge outcomes.

  3. Display the results
     Your output may be simple, showing the results.
	 You may also produce more formal results as shown below.
 

possible format of output:

Lab-12 flipped coin landing on edge? ------------------------------------ Tossing coin 1000000 times. heads: 499801 tails: 500040 edge: 159
More formal output...
Lab-12 flipped coin landing on edge? ------------------------------------ Tossing coin 1000000 times. expected actual outcome outcome difference accuracy -------- ------- ---------- -------- heads: 499916.6 499801 115.6 99.97% tails: 499916.6 500040 123.4 99.97% edge: 166.6 159 7.6 95.44%

Getting started

Do your work in your Odin 2010/c/ folder.

Challenges were derived from textbook chapter-13.

Coin Toss Simulator

 - Do not add any extra member variables or member functions to Coin.
 - Only the variables and functions below are allowed.
 - Do not initialize class member variables outside of a constructor.
 - Do not go online for program solutions during a lab.
 - Instead, ask questions to get help.
 - Fix your toss.cpp program to meet the requirements.
 - The coingame.cpp program must also meet these requirements.


Program name: 2010/c/toss.cpp

Write a class named Coin.


The Coin class should have the following member variables:

  • A C-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 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 ("heads" or "tails") 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.

To determine heads or tails outcome, write an overloaded equality operator
for your class. Function name will be operator== Use of this function
might look like this:

   Coin coin;
   ...
   ...
   if (coin == "heads")
      cout << "heads";


format of output:
Lab-12 Coin toss simulator -------------------------- Initial coin side up: heads Next 20 tosses: heads heads tails heads tails tails tails heads tails heads heads tails heads tails tails heads tails tails tails heads heads: 9 tails: 11 Program complete.

Coin Tossing Game

Name your program 2010/c/coingame.cpp

Use your Coin class from the previous challenge.

Create a new file named 2010/c/coin.h
Put your Coin class definition only in this file.

Create a new file named 2010/c/coin.cpp
Put your Coin class function bodies in  this file.

We will do a sample program together on the big-screen to see how these
files will work.

Write a program to simulate the following game.

The program will have three instances of the Coin class:
   one representing a quarter
   one representing a dime
   one representing a nickel

When the game begins, your starting balance is $0.00.

During each round of the game, the program will toss the simulated coins.
When a coin is tossed, the value of the coin is 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 $2.00 or more. If your balance
is exactly $2.00, you win the game. You lose if your balance exceeds $2.00.

format of output:
Lab-12 Coin toss game --------------------- Game has started! Tossing coins now... $0.35 $0.45 $0.80 $1.20 $1.35 $1.70 $1.80 $2.05 <--- you went over. You Lose. :( Program complete.
Files required: coingame.cpp coin.h coin.cpp
Extra credit

When coin tossing a Nickel, there is a 1/6000 probability that the coin
will not land on heads or tails, but on its edge.

Build this into your coin toss game.

When a Nickel is tossed, check to see if it landed on its edge, and toss
it again. Keep a count of how many times this happens in your game.

Make the counter as a private member variable, with a getter function.

We will write a test program together to see if the Nickel really lands
on its edge about 1 every 6000 tosses.

------------
How to do it
------------
You are allowed to write an overloaded member function named toss.
Your overloaded toss function must have one or more arguments.
The arguments aren't used, except for overloading.
C++ textbooks recommend this method for several other types of overloading.

This overloaded function will be called only for flipping a Nickel.
It will set sideUp randomly with "heads", "tails", or "edge".
Handle all other logic of a Nickel landing on edge in the main function.

Note:
You may also handle the tossing of a Quarter with an overloaded toss function.
The chance of a Quarter landing on edge is about 1 every 20000 tosses.

You may also create an overloaded function for tossing a dime.
My experiments suggest that the edge probability is about 1/10000.

Note:
To get the probabilities, I set some coins up on edge by hand. The ones that
were harder to do by hand got higher probabilities. You may experiment and
set your own probabilities.