Here is what i need the PRG to do...

Assignment #4

When you toss a coin, generally, you don't get too many heads or tails in a row, but
if you toss enough times, eventually you will get a number of heads in a row or tails in
a row. It's easy to get 2 or 3 heads or tails in a row, but if you want a larger number
of heads or tails in a row, you'll have to toss more times.
Write a program that reads an integer from the user between 2 and 9.
If the number entered is outside the above range, ask for another until the number
entered is in that range. If n represents the integer entered by the user, keep generating
and displaying 0's and 1's randomly until n 0's in a row have been generated and displayed.
Print how many numbers had to be generated to get that many 0's in a row.

The following is a sample interaction between the user and the program:
How many consecutive 0's would you like to see? [between 2 and 9]: 20
Invalid entry! Enter an integer between 2 and 9, please.
How many consecutive 0's would you like to see? [between 2 and 9]: 4
0010111001010110010010000
It took 26 tries to geet 4 0's in a row.
Press any key to continue.
*/

#include<iostream>
#include<ctime>

using namespace std;

int main()
{
	int num,toss;
	int headsCounter=0,tailsCounter=0;
	
	srand((unsigned)time(0));//uses system clock to generate random #

	cout<<"Enter the number of consecutive 0's, you want to display.(2-9): ";
	cin>>num;

	do{	    		
		if(num<2||num>9)
		{
			cout<<"\n\tERROR! The number must be larger than 2 and less than 9.";
			cout<<"\n\nEnter another number: ";
			cin>>num;
			break;
		}		
	}while(num<2||num>9);	

		toss=rand()%2;
		if(toss==1)
			headsCounter++;
		else
			tailsCounter++;
		
		toss=rand()%2;
		if(toss==1)
			headsCounter++;
		else
			tailsCounter++;

	
	
	system ("pause");
	return 0;
}

Recommended Answers

All 3 Replies

Create two counters. First counts the rolls. Assuming heads, second gets set to 0 on each tail, incremented by 1 on each head.

When second matches the number entered, first counter is number of rolls it took.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.