1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
elena-s [515]
3 years ago
6

Write a program that simulates flipping a coin repeatedly and continues until three consecutive heads. are tossed. At that point

, your program should display the total number of coin tips that were made the average number of heads. #1 - Write a function to read in the user's information (name & gender) #2 - Write a function to flip the coin and return a bool (heads = true, tails = false) #3 - Write a function to calculate the average of heads vs. total flips #4 - Write a function to output the results Address male users as Mr. and female users as Ms. HINT: Code one function at a time and test it... then add the loop and re-test. Turn in as a single PDF file-Include three (3) test runs. 1-Output (cut and pasted into a txt file in eclipse) 2- Source Code (printed from eclipse - PROPERLY DOCUMENTED) INPUT/OUTPUT - should be formatted as follows - This represents one possible sample run (Class heading should be also displayed) Welcome to coin toss! Get 3 heads in a row to win! What is your name? Ed Peck What is your gender (m/f): M Try to get 3 heads in a row. Good luck Mr. Ed Peck! Press to flip TAIL Press center> to flip HEAD Press to flip HEAD Press center to flip HEAD Press center> to flip HEAD It took you 6 tosses to get 3 heads in a row. On average you flipped heads 67% of the time
Computers and Technology
1 answer:
quester [9]3 years ago
8 0

Answer and Explanation:

#include <iostream>

#include <string>

#include <time.h>

#include <vector>

using namespace std;

//Takes user info

int user_info(string *name,char *gender){

   char G;

   cout<<"What is your name?: ";

   getline(cin, *name);

   cout<<"What is your gender(m/f):";

   cin>>G;

   cout<<endl;

   *gender = tolower(G);

   return 0;

}

//Toss coin(part 2)

bool coin_toss(double seed){

   double r;

   r = (double)seed/(double)RAND_MAX;

   //cout<<r;

   if (r<0.5) return true;

   return false;

}

//check results

float toss_result(vector<char> v,int *h){

   int num_heads=0;

   int num_toss = v.size();

   *h = 0;

   for(int i=0;i<num_toss;i++){

       if(v[i]=='h'){num_heads++;(*h)++;}

   }

   return (float)num_heads/(float)num_toss;

}

void show_result(int total_num,int head_num,float ratio){

   cout<<"it took you "<<total_num<<" tosses to get 3 heads in a row"<<endl;

   cout<<"on average you flipped heads "<<(ratio*100)<<"% of the time"<<endl;

}

int main()

{

   string name;

   char gender;

   string K; //Mr or Mrs

 

   cout<<"Welcome to coin toss! Get 3 heads in row to win!"<<endl;

 

   //part 1

   user_info(&name,&gender);

 

   if(gender == 'f') K = "Mrs.";

   else K = "Mr.";

 

   cout<<"Try to get 3 heads in a row. Good luck "<<K<<name<<"!"<<endl;

   char dummy = getchar();

 

   //part 2

   int num_toss;

   vector<char> toss_results;//store toss results

   bool result;//result of toss

   srand(time(0));

   while(true){

       //cout<<rand()<<endl;

       result = coin_toss(rand());

       cout<<"Press <enter> to flip"<<endl;

       while (1)

       {

           if ('\n' == getchar())

           break;

       }

     

 

     

       if(result) {toss_results.push_back('h');cout<<"HEAD"<<endl;}

       else {toss_results.push_back('t');cout<<"TAIL"<<endl;}

     

       num_toss = toss_results.size();

     

       if(num_toss>=3){

           if ((toss_results[num_toss-1] == 'h')&&(toss_results[num_toss-2] == 'h')&&(toss_results[num_toss-3] == 'h')){

               break;

           }

       }

   }

 

   //part 3

   float ratio_head;

   int num_of_heads;

   ratio_head = toss_result(toss_results,&num_of_heads);

 

   //part 4

 

   show_result(toss_results.size(),num_of_heads,ratio_head);

 

   return 0;

}

You might be interested in
If a storm is 7.5 kilometers away, how much time is expected between observations of lightning and thunder? Round your answer to
Svetlanka [38]

Answer:

22.5 s is the correct answer to the given question .

Explanation:

Given that

d=7.5km=7.5\times 10^3 m

For light:

We know that speed of light c

c=3\times 10^8 m/s

We know that

Distance = Speed \times timed=C\time tt=\dfrac{D}{t}t=\dfrac{7.5\times 10^3}{3\times 10^8}\ st=2.5\times 10^{-5}st=0.025 ms

For thunder :

Speed of sound ,v=332 m/s

Time\  taken\  by\  sound \\t\’=\dfrac{7500}{332}=22.59 s

Therefore the difference between time is given as follows

\Delta t= t' - \ t=22.59-2.5\times 10^{-5}=22.59 s

22.5 is the answer

4 0
3 years ago
____________ reference is used when you want to use the same calculation across multiple rows or columns.
vlada-n [284]

Answer:

relative cell

Explanation:

So, if you want to repeat the same calculation across several columns or rows, you need to use relative cell references. For example, to multiply numbers in column A by 5, you enter this formula in B2: =A2*5. When copied from row 2 to row 3, the formula will change to =A3

7 0
3 years ago
Read 2 more answers
Im lonellly whos down to date me
inna [77]

Answer:

I was actually just looking to help with someone's schoolwork. . .

Explanation:

5 0
2 years ago
Read 2 more answers
4. Which of the following root keys contains the data for a system’s non-user-specific configurations? a. HKEY_LOCAL_MACHINE b.
seropon [69]

Answer:

The system’s non-user-specific configurations are stored in the HKEY_LO-

CAL_MACHINE root key of the Registry.

Explanation:

6 0
3 years ago
The most common markup language used with web pages is ___________ Question 4 options: A. HTML B. VRML C. Javascript
leonid [27]
Hi there!

The correct answer is A. HTML.

HTML stands for Hypertext Markup Language which is used in nearly all websites and web-based programs. HTML acts as a structure for a website giving you the text, page titles, text-boxes, etc... HTML is also used along with the languages of Javascript, CSS, C++, and others. DO NOT confuse languages like Javascript and the rest with Markup Languages because they are not the same! Javascript, for example, is what is used to make websites more user-friendly and interactive. When you use Brainly, for example, and you hover over someone's name or picture and that box pops up that is Javascript at work and not HTML or any other Markup Language.

-<span>ASIAX </span><span>  </span><span>Frequent Answerer</span>
4 0
3 years ago
Read 2 more answers
Other questions:
  • Talking to a friend _____.
    5·2 answers
  • The critical path of a network is the A. shortest time path through the network. B. path with the most activities. C. longest ti
    8·1 answer
  • What is ment by creative middle way solution
    6·1 answer
  • Mr. Perry has bookmarked a large number of webpages in Chrome while researching class topics. Unfortunately, he's bookmarked so
    13·1 answer
  • To access your gradebook you click on the __________.
    6·2 answers
  • Which type of virus includes protective code that prevents outside examination of critical elements?
    14·1 answer
  • Having friends who cause you stress can decrease your happiness, which can in turn
    13·2 answers
  • 100 tickets are sold in a raffle with one prize. John has 8 tickets, Jamie has 10 tickets, find the probability that John wins
    11·2 answers
  • How does abstraction help us write programs
    11·1 answer
  • ...............is a personal computer that fits on desk.​
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!