Question

C++, Write an iterative routine that will have a queue and a loop to flip 2...

C++, Write an iterative routine that will have a queue and a loop to flip 2 coins: 0 for tails and 1 for heads. Add the values of the tosses of the two coins and put them onto the queue. Then dequeue the values one at a time and sum all the values. After the queue is empty, and print out the number of heads. Assume srand has already been done.

Homework Answers

Answer #1

OUTPUT:

CODE:

#include <queue>
#include <ctime>
#include <iostream>

using namespace std;

int getHeads()
{
   srand(time(0));
   queue<int> q;
  
   for(int i = 0;i < 4;++i)
   {
       int coinTossFirstCoin = rand() % 2;
       int coinTossSecondCoin = rand() % 2;

       q.push(coinTossFirstCoin);
       q.push(coinTossSecondCoin);
   }

   int sum = 0;

   while (!q.empty())
   {
       sum += q.front();
       q.pop();
   }

   return sum;
}

int main()
{
   cout<<"Heads : "<< getHeads();
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Movie stars and U.S. presidents have fished Pyramid Lake. It is one of the best places...
    asked 29 minutes ago
  • what advantages do percent distributions have over frequency distributions when comparing populations at different years?
    asked 35 minutes ago
  • Richard has just been given a 6-question multiple-choice quiz in his history class. Each question has...
    asked 56 minutes ago
  • Write about westernized elements in life; westernization in our life, and this includes Americanization.
    asked 1 hour ago
  • The college Physical Education Department offered an Advanced First Aid course last summer. The scores on...
    asked 1 hour ago
  • Researchers hypothesized that increasing a woman's level of arousal would increase her perceptions of attractiveness of...
    asked 2 hours ago
  • Equation 37-14b in the textbook gives the energy emitted by Hydrogen when electrons transition between states...
    asked 2 hours ago
  • Theory of Computation Please provide explanation too on how it works a. Give an NFA recognizing...
    asked 2 hours ago
  • Question 1 A sequential pattern detection circuit (state machine) has input A and output Y, which...
    asked 2 hours ago
  • Natural Foods Inc. is planning to invest in new manufacturing equipment to make a new garden...
    asked 2 hours ago
  • Explain why a callable bond's price would be expected to decline less than an otherwise comparable...
    asked 3 hours ago
  • . What are the three types of pricing strategies services that services employ? Using a real...
    asked 3 hours ago