Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've mentioned some examples to explain the problem below,I've written a code to solve the first two exercises ,is there more optimal solution, and could you please help me with the the third exercises

Heads or tails If an unbiased coin is tossed, then the value of the coin (Tails) has the same probability of showing after falling as the other side that contains the coat of arms (Heads). Because of this decision by "tossing a coin" is used frequently, for example this method is used to decide which team starts a football match. Your task is the simulation of the tossing of a coin and the analysis of sequences acquired by tossing a coin. In the exercises T stands for tails and H stands for heads. For example a sequence of 5 tosses can be: TTHTH. Create a program named Heads_Or_Tails to solve the following exercises. The input for the program is a so-called trial, i.e. the outcome of a sequence of tosses. This input must be read from the standard input. The first line of the input contains an integer, N , indicating the number of tosses.
The second line of the input line contains exactly N characters, each character is either H or T. For example, the input can be as follows.
8
HHHTHHTT
The output of the program should be written to the standard output. The output contains 3 lines: the ith line in the output is the solution to exercise i.

Exercise 1

How many times did it occur in the trial that exactly two heads were tossed after each other? For example, in the sequence THHHHTTHHTHHHTHH it occurred twice that exactly two heads were thrown after each other.

Exercise 2

Find the length of the longest subsequence that contained only heads, and the longest subsequence that contained only tails. Print both lengths as integer numbers separated by a space. (Any of the two numbers can be zero, but not both.)

Exercise 3

We shall call a sequence of heads in the trial a heads series, if both of the following two conditions hold:

the first item in the series is the first item in the trial, or the item preceding the first item of the series is tails;
the last item in the series is the last item in the trial, or the item following the last item of the series is tails.
Find out which is the most frequent length for heads series. If there are more than one "most frequent" heads series lengths, print the longest. If there are no heads in the trial, print zero.

Examples

We provide three simple examples to clarify the exercises. Test your solution with other, larger examples as well!

Example 1

Input:

10
HHTTHHTHHH
Output:

2
3 2
2
Example 2

Input:

14
HHTTHHTHHHTHHH
Output:

2
3 2
3
Example 3

Input:

1
H
Output:

0
1 0
1

What I have tried:

C++
int main()
{
	char TossesSequence[50];
	int N;
	int TwoHeadsN =0;
	int LongestH = 0;
	int LongestT = 0;
	int HeadsC = 0;
	int TailsC = 0;

	cout << "Please enter Number of tosses: ";
	cin >> N;
	cout << "Please enter outcome of sequence of the tosses: ";
	cin >> TossesSequence;

	
	for (int i = 0; i < N; i++)
	{
		if (TossesSequence[i] == 'H')
		{
			HeadsC++;
			if (HeadsC > LongestH)
				LongestH = HeadsC;
			TailsC = 0;
		}
		else if (TossesSequence[i] == 'T')
		{
			TailsC++;
			if (TailsC > LongestT)
				LongestT = TailsC;
			if (HeadsC == 2)
				TwoHeadsN++;
				HeadsC = 0;
		}
		else
		{
			cout << "wrong input" << endl;
			return -1;
		}
	}

	if (HeadsC == 2)
		TwoHeadsN++;

	cout << TwoHeadsN << "\n";
	cout << LongestH << "\t" << LongestT << endl;
	return 0;
}
Posted
Updated 28-May-16 11:28am
v4
Comments
Richard MacCutchan 28-May-16 8:25am    
You solve them by writing some code. However, the first thing you need to do is use paper and pencil to figure out the logic of each exercise. When you have a specific question then please edit the above and add the details.
Philippe Mori 28-May-16 9:46am    
You should do your homework yourself as otherwise you won't learn anything.
Member 12551704 28-May-16 13:29pm    
he solves most of the homework but he is asking to solve it in a simpler way.

This is HomeWork, but you have done some work.
Advice:
- In first I would have separated the code solving each exercises. It is just making reading the code easier and debugging too.

exercise 3
You already know the maximum length of sequence.
you have basically 2 solutions.
- you build an array holding the number of times you see each sequence length as you scan the input.
- for each possible length, you scan the input and you memorize the best answer.
 
Share this answer
 
This is homework, so I'll give you no code!
The way I'd do it is to "preprocess" the input string into a list of Heads and Tails sequence lengths: I'd create two arrays of integers, one for heads, one for tails, and run through the input data filling them with the lengths of the sequences.
So in your example:
THHHHTTHHTHHHTHH
I'd end up with two arrays containing:
HeadCount: 4 2 3 2
TailCount: 1 2 1 1

From that, your exercises are all trivial:
E1: How many 2's in HeadCount?
E2: Largest number in HeadCount and largest number in TailCount
E3: Create another array which counts the number of sequence lengths in HeadCount. Find the biggest number!

Give it a try: it's pretty easy to do, and it's a lot clearer than the code you are using! :laugh:
 
Share this answer
 
Comments
Member 12551704 28-May-16 13:36pm    
preporcessing the input string into a list of Heads and Tails will make the code more longer and a little bit more complicated. In my opinion, he should solve the third exercise in the same way. and try to use pointers to not determined the size of his sequence matrix.
Mohibur Rashid 30-May-16 5:33am    
Why are you answering for another user? Or are both of you same user?
Patrice T 28-May-16 17:29pm    
5ed
Preprocessing is a good idea, it simplify solving the exercises.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900