Requirement. 1. Use the C++ library's random number generator, and include the srand statement so that the result is not the same every time you run the program. Program I/O. Input: none. Output: either Heads or Tails. Example. Heads
![**Coin Toss, v.1.0**
**Purpose:** Write your first computer game program, from scratch, using if-logic.
Pretend that you have been hired by the National Football League (NFL) to write a program to replace the coin toss. Name it `coinToss1.cpp`. The output should be simple: either the word "Heads" or the word "Tails". There is no console input — just run the program, and it says "Heads" or "Tails".
**Supplemental:** Read about “randomizing” in [this supplemental PDF](www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf).
**Requirement:**
1. Use the C++ library’s random number generator, and include the `srand` statement so that the result is not the same every time you run the program.
**Program I/O:**
- **Input:** none
- **Output:** either Heads or Tails
**Example:**
Heads](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6f46ae0e-a0ee-4500-808d-7fa0f129e90c%2Fd39db65c-8da9-4617-8819-f3ba550ee9ff%2Fcv7ozog_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
#include <iostream>
#include <ctime>
using namespace std;
//main function
int main()
{
//calling random number generator function
srand(time(0));
int input;
cout << "Enter the number of tosses to perform: ";
cin >> input; //read input
int counter=0; //initialize counter to 0
while(1) //infinite loop
{
if(counter==input) //if limit reached
break; //exit from loop
int flip = rand() % 2; //get value
if (flip == 0) //if value is 0
cout << "Heads" << endl;
else //if value is 1
cout << "Tails" << endl;
counter=counter+1; //add 1 to counter
}
return 0;
}
the program should be based on the code above, and should not use arrays.
![Coin Toss, v.3.0
Purpose. Learn how to use "nested loops" to put "replay" a program that already contains a loop.
Requirements. Rewrite your coinToss2.cpp from exercise 7.4 as coinToss3.cpp. Modify the
program to add another loop that lets the user replay the entire game. If the user enters sentinel value
of 0 (zero) for the number of tosses, break out of the outer replay loop to end the program.
Be sure to explain this to the user in the prompt -- something like: "Enter the number of tosses to
perform [0=exit]:".
Supplemental. Read about "randomizing" in www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf.
Program I/O. Input: a non-negative number from the console keyboard, repeated in a loop until the
sentinel value of 0 is entered. Output: a series of Heads or Tails repeated in a loop until the sentinel
value is entered.
Example. with user input in blue
Enter the number of tosses to perform [0=exit]: 3
Heads
Tails
Heads
Enter the number of tosses to perform [0=exit]: 2
Tails
Tails
Enter the number of tosses to perform [0=exit]: 0](https://content.bartleby.com/qna-images/question/5a4a4196-402b-41d8-8cc6-400e379b188a/2e8d329e-c0bd-4f1f-ae84-5578b733e8cd/isflu5i_thumbnail.png)
The program needs to be modified.
![Coin Toss, v.3.0
Purpose. Learn how to use "nested loops" to put "replay" a program that already contains a loop.
Requirements. Rewrite your coinToss2.cpp from exercise 7.4 as coinToss3.cpp. Modify the
program to add another loop that lets the user replay the entire game. If the user enters sentinel value
of 0 (zero) for the number of tosses, break out of the outer replay loop to end the program.
Be sure to explain this to the user in the prompt -- something like: "Enter the number of tosses to
perform [0=exit]:".
Supplemental. Read about "randomizing" in www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf.
Program I/O. Input: a non-negative number from the console keyboard, repeated in a loop until the
sentinel value of 0 is entered. Output: a series of Heads or Tails repeated in a loop until the sentinel
value is entered.
Example. with user input in blue
Enter the number of tosses to perform [0=exit]: 3
Heads
Tails
Heads
Enter the number of tosses to perform [0=exit]: 2
Tails
Tails
Enter the number of tosses to perform [0=exit]: 0](https://content.bartleby.com/qna-images/question/6f46ae0e-a0ee-4500-808d-7fa0f129e90c/54268fd9-34ea-4648-87d0-199a80538463/8ui8ba3_thumbnail.png)
The program should be according to the
Please refer to the algorithm highlighted.
![Coin Toss, v.2.0
Purpose. Learn how to use count-controlled loops.
Requirements. Rewrite your coinTossl.cpp from exercise 6.5 as coinToss2.cpp. Modify the
program to allow the user to specify (via keyboard data entry) the number of coin tosses to perform.
When you run the program, and it should say the result of each coin toss (that is, "Heads" or "Tails").
Supplemental. Read about "randomizing" in www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf.
Algorithm.
Call srand
Prompt the user to enter how many coin tosses to perform
Input and store the user's selection
Create a counter and set it to zero
Start the loop here
If the counter equals the number of tosses to perform
Break from the loop
Get and store a randomly-generated number in the range 0 to 1
If the randomly-generated number equals 0
Output "heads"
If the randomly-generated number equals 1
Output "tails"
Add one to the counter
Loop back from here
Example: with user input in blue
Enter the number of tosses to perform: 3
Heads
Tails
Heads](https://content.bartleby.com/qna-images/question/6f46ae0e-a0ee-4500-808d-7fa0f129e90c/7689a2d3-0fc0-46da-83cb-a2f7eed03120/dmwvwsj_thumbnail.jpeg)
![**Coin Toss, v.2.0**
**Purpose:** Learn how to use count-controlled loops.
**Requirements:** Rewrite your `coinToss1.cpp` from exercise 6.5 as `coinToss2.cpp`. Modify the program to allow the user to specify (via keyboard data entry) the number of coin tosses to perform. When you run the program, it should say the result of each coin toss (that is, "Heads" or "Tails").
**Supplemental:** Read about “randomizing” in [www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf](http://www.rdb3.com/cpp/exercises/Gaming.supplemental.pdf).
**Algorithm:**
1. Call `srand`.
2. Prompt the user to enter how many coin tosses to perform.
3. Input and store the user's selection.
4. Create a counter and set it to zero.
5. Start the loop here:
- If the counter equals the number of tosses to perform, break from the loop.
- Get and store a randomly-generated number in the range 0 to 1.
- If the randomly-generated number equals 0, output "heads."
- If the randomly-generated number equals 1, output "tails."
- Add one to the counter.
6. Loop back from here.
**Example:** With user input in *blue*
Enter the number of tosses to perform: *3*
- Heads
- Tails
- Tails
- Heads](https://content.bartleby.com/qna-images/question/6f46ae0e-a0ee-4500-808d-7fa0f129e90c/ba7abaf6-bcb5-4a91-a05c-f19d8021db0f/nsdnat7_thumbnail.png)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)