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.
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();
}
Get Answers For Free
Most questions answered within 1 hours.