Question

In: Computer Science

In an effort to find something to do during self-quarantine, your friend flips a coin many...

In an effort to find something to do during self-quarantine, your friend flips a coin many times and carefully records the result of each flip in a character vector using either H or T. Write a function named greatestRun that receives this character array and then analyzes it to find the largest number of consecutive heads or tails. It must return two values: (1) the number corresponding to how many heads or tails were thrown in a row and (2) the character H or T depending upon whether it was Heads or Tails that was thrown the most times. In the event of a tie for the longest run, return the character B for both

Please solve the Heads-or-Tails problem in C or C++ using either Windows or Linux. Read the H-or-T data from
a below file or from STDIN. Report the result and related statistics to STDOUT. Report any errors to
STDERR:

File: HTHHTTTHHTHHHTHTHTHHHHHTTHHHHHHHHHHHHTTTTTTTTTH

Solutions

Expert Solution

  • USING TOSS.TXT FILE FOR INPUTS.
  • USING STRUCT finalAns to send output from greatestRun()
  • Attempted in C++ using windows.

Code for the following question:-

#include <bits/stdc++.h>
using namespace std;
struct finalAns
{
std::string maxchar,count;
};
finalAns greatestRun(ifstream & point) //function asked in question
{char ch;
struct finalAns a;
char prev;
int maxH=0,maxT=0;
int count=0;

while (point>>ch) //reading characters from text file into char variable
{
if(count==0&&maxH==0&&maxT==0) //first character
{
prev=ch;
count++;
continue;
}
if(prev==ch)
count++;
else // check for maxH and maxT each time the value changes from H to or vice versa
{
if(prev=='H'&&count>maxH)
maxH=count;
if(prev=='T'&&count>maxT)
maxT=count;

count=1;
}   

prev=ch;   
}

if(prev=='H'&&count>maxH)
maxH=count;
if(prev=='T'&&count>maxT)
maxT=count;
//saving the final ans to struct variable a
  
std::string s ;
if(maxT>maxH) //T>H
{
a.maxchar='T';
s = std::to_string(maxT);
a.count=s;
}
else if(maxT<maxH) //H>T
{
a.maxchar='H';
s = std::to_string(maxH);
a.count=s;
}
else //draw
{
a.maxchar='B';
a.count='B';
}
return a;
}

int main()
{ ifstream point;
point.open("toss.txt");
struct finalAns x=greatestRun(point);
cout<<x.maxchar<<" "<<x.count;
return 0;
}
Screenshot of IDE:-

Screenshot of sample input file and output:-

*********************IN CASE OF ANY DOUBT FEEL FREE TO LEAVE A COMMENT*****************************


Related Solutions

in order to support individuals in eating healthy during self-quarantine and isolation
  in order to support individuals in eating healthy during self-quarantine and isolation, which could positively reflect on physical and mental wellbeing, in your opinion 1. What are the guidelines for healthy eating food? 2. What are the factors contributing to poor control of food intake in humans?  
You challenge a friend to a bottle flipping contest. Your friend flips 64 out of 226...
You challenge a friend to a bottle flipping contest. Your friend flips 64 out of 226 bottles that land upright. You flip 72 out of 238 bottles that land upright. Is there evidence, at a 5% level of significance, that there is a difference in proportion of bottles that land upright between you and your friend? Calculate the following: Standard Error : Standardized Test Statistic : p-value: Should the null hypothesis be rejected (yes or no)?
Find the expected number of flips of a coin, which comes up heads with probability p,...
Find the expected number of flips of a coin, which comes up heads with probability p, that are necessary to obtain the pattern h, t, h, h, t, h, t, h. This is from Sheldon/Ross Introduction to Probability models 11th edition Chapter 3#91. I know there is the textbook solution manual on Chegg, but I am not able to make sense of the solution. I would greatly appreciate if anyone can help me make sense of it!
An experimenter flips a coin 100 times and gets 42 heads. Find the 90% confidence interval...
An experimenter flips a coin 100 times and gets 42 heads. Find the 90% confidence interval for the probability of flipping a head with this coin.
(Expected values) Suppose you roll a fair die and your opponent flips a fair coin simultaneously....
(Expected values) Suppose you roll a fair die and your opponent flips a fair coin simultaneously. You win $4 whenever a Head appears and the number of dots on the top face of the dice is either one or six. For all other outcomes, you lose $1. (a) (2pts) How many possible outcomes are there? An outcome is the combined result of both coin-flipping and dice-rolling. (b) (3pts) What is your expected payoff? (c) (3pts) What is your opponent’s expected...
During the current COVID-19 pandemic many countries have imposed quarantine on their populations in order to...
During the current COVID-19 pandemic many countries have imposed quarantine on their populations in order to limit/ stop the spread of the disease and avoid overload on the health system. While these measures are justified from the health perspective, they have major economic consequences. In this assignment you are asked to analyse different economic aspects related to the COVID-19 pandemic Part 1 GDP and unemployment a) Analyse the effects of quarantine on GDP, employment and the unemployment rate. Which industries...
- Your friend claims he has a fair coin; that is, the probability of flipping heads...
- Your friend claims he has a fair coin; that is, the probability of flipping heads or tails is equal to 0.5. You believe the coin is weighted. Suppose a coin toss turns up 15 heads out of 20 trials. At α = 0.05, can we conclude that the coin is fair (i.e., the probability of flipping heads is 0.5)? You may use the traditional method or P-value method.
Do some research on your externality, find something specific and recent to address. For instance, "environmental...
Do some research on your externality, find something specific and recent to address. For instance, "environmental pollution" or "education" are too broad. Noise pollution due to fireworks on 4th of July or the measles outbreak/vaccine are more specific topics. Describe the externality, identify whether it is positive or negative, and explain why.   Relate your externality to the following concepts (include graphs) Describe the effect on the market using relevant MB, MC, MSB, and MSC curves. Use arrows to indicate direction....
You ask a friend to do the laundry. There is a 10% chance that your friend...
You ask a friend to do the laundry. There is a 10% chance that your friend will forget to do the laundry. If he does the laundry, there is a 10% chance that the washing machine will lose it. If the washing machine doesn’t lose the laundry, there is a 10% chance that it will be folded by the wrong person. (a) What is the chance that the laundry is folded by the right person? (b) If the laundry isn't...
The worldwide adoption of IFRS is something that many people do not agree on. In my...
The worldwide adoption of IFRS is something that many people do not agree on. In my opinion I think the worldwide adoption of IFRS would result in global comparability of financial statements. The IFRS was originally started to bring consistency to accounting standards and practices no matter what company it is or what country it is located. The IFRS also creates better transparency for companies that are alike. With the good also comes the bad. IFRS is principle based compared...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT