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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 14PE
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 coin Toss1.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 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 coin Toss1.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 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 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
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning