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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
**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
Transcribed Image Text:**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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

#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
Transcribed Image Text: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
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

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
Transcribed Image Text: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
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

The program should be according to the algorithm provided as it says create a counter and set it to zero and if the counter equals the number of tosses to perform, it should break from the loop.

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
Transcribed Image Text: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
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question
**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
Transcribed Image Text:**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
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education