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
Which best compares appointments and events in Outlook 2010
lesantik [10]

In Microsoft Outlook, there are differences between appointment and event. Appointment is defined as <em>an activity that you schedule in your calendar which does not involve other people.</em> Appointments can be scheduled to a certain duration in a day. Event is defined as <em>an activity that you do with other people which lasts from 24 hours to longer. </em>

Thus, from these descriptions, the best answer to the question is (C) appointments have a start and end time of day, and events do not.

5 0
3 years ago
If you must apply for a loan, you should _____. contact a broker and provide certain loan information visit a loan officer at a
azamat
B.) V<span>isit a loan officer at a financial institution and complete an application.</span>
4 0
2 years ago
Read 2 more answers
Write a for loop that sets each array element in bonusScores to the sum of itself and the next element, except for the last elem
Nat2105 [25]

Answer:

The program to this question can be given as:

Program:

#include<stdio.h>  //header file.

int main()

 //main function

{

   int SCORES_SIZE = 4;  //define variables.

   int bonusScores[]={10,20,30,40};  //define array.

   int i;

   printf("Values:");

 //message

   for (i = 0; i < SCORES_SIZE; ++i)  //loop for print values.

   {

       printf("%d ", bonusScores[i]);

   }

void CombineScores(int numberScore, int userScore) //function

{

   for (i = 0; i < SCORES_SIZE; i++)

  //loop for convert values

   {

       if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))  //check array elements

       {

           bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);

 //add values

       }

       else

       {

           bonusScores[i] = bonusScores[i];

       }

   }

   printf("\nReturn values:");  //message

 for (i = 0; i < SCORES_SIZE; i++)

{

printf("%d",bonusScores[i]);

}

}

CombineScores(0,0); //calling

return 0;

}  

Output:

Values:10 20 30 40  

Return values:30 50 70 40  

Explanation:

In the above c++ programming code firstly we define the header file. Then we define the main function in this function we define variable that name is given in the question that is SCORES_SIZE, i. Variable i is used in the loop for a print array. Then we define and initialize an array. The array name is and elements are define in the question that is bonusScores[]={10,20,30,40}. Then we use the loop for print array elements.Then we define function that is CombineScores() in this we pass two parameter. Then we use the second time in this loop we check array elements and add the elements that are given in the question. In this function we print values and at the end we call function.

3 0
2 years ago
When programming, which of the following is true of the editor?
OLEGan [10]

Answer:

The correct answer is It is a program that checks for grammatical errors.

Explanation:

In programming, when we want to check that our grammar is correct, we can use an editor.

This is a program that allows you to correct spelling through a software application. It analyzes the entered text and automatically checks whether it is spelled correctly or not, comparing the words with its internal database.

Given this information we can say that the correct answer is It is a program that checks for grammatical errors.

5 0
3 years ago
Characteristic of first generation computer<br>​
ella [17]

Characteristics

based on vaccum tubes

they were large,slow,low capacity

Magnetic drums for primary memory

used mll

input punched cards and output printsout

expensive

get heated faster

eg. ENIAC,UNIVAC

6 0
2 years ago
Other questions:
  • X2/3-2x1/3-35=0
    14·1 answer
  • [Java] Using the comparable interface
    13·1 answer
  • Materials such as copper, silver, and aluminum through which electric energy passes freely are called
    13·1 answer
  • Is spread spectrum transmission done for security reasons in commercial WLANs?
    13·1 answer
  • The principle of time preference requries a larger payment in the future than the present. Which situation best illustrates this
    12·1 answer
  • Which devices typically generate computer output ?
    8·2 answers
  • Discuss the importance of top management commitment and the development of standards for successful project management. Provide
    10·1 answer
  • A nested folder can best be described as what?
    12·1 answer
  • 1. Which of the following is not true about high-level programming language s? (a) Easy to read and write (b) Popular among prog
    12·1 answer
  • Difference between multi-national and global company​
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!