Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm a beginner in c++ doing a homework assignment regarding flipping a coin. It is a zybooks LAB called 6.25LAB: Flip a coin. The instructions

I'm a beginner in c++ doing a homework assignment regarding flipping a coin. It is a zybooks LAB called 6.25LAB: Flip a coin. The instructions are as follows:Create a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0. For reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time. In that case, every program's output would be different, which is what is desired but can't be auto-graded.

Note: A common student mistake is to call srand() before each call to rand(). But seeding should only be done once, at the start of the program, after which rand() can be called any number of times.

Your program must define and call the following function that returns "heads" or "tails".

string HeadsOrTails()

The srand() seed can only be 2. I'm not allowed to use time to seed rand(). The goal is to achieve a specific pattern for zybooks to consider the question correct.

#include

#include

using namespace std;

string HeadsOrTails(int rand())

{

string coinFlip;

if(cin >> rand() == 0)

{

coinFlip = "heads";

}

else

{

coinFlip = "tails";

}

return coinFlip;

}

int main() {

int usrDecision;

cin >> usrDecision;

int rand() = srand(2);// Unique seed

for (int i = 0; i < userDecision; i++)

{

cout << HeadsOrTails(rand()) << endl;

}

return 0;

}

That is the code I have so far, this is my first time trying this. I'm still unsure on how to declare rand in a function. That may be the culprit as to why I get nothing back. I'm sure that there are plenty of mistakes. However this is my first time using srand and rand in conjunction to a function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions