Calculating Head/Tails and winning totals

So I am writing a program to randomly flip a coin and count until I get a "Head" for a total of 10 rounds.

I also need to make a function for winnings which calculates 2^n (n being number of "Tails" and the one "Head" during the flips.

last is the average payout at the end.

so it would print something like this;
TTH you win $8
TH you win $4
TTTTH you win $32
etc.
the average payout $x.xx

I'm pretty lost on where to go with my functions and the calling of them. The code before I added the payout function would run but would give one extra "H" in every round. If someone could just point me in the right directions. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;


int randomNum(int num);
void payOff(double win);

int main()
{
	int num = 0;
	srand(time(0));	
	string choice = "";
	double average;	
		
	for (int i = 1; i <= 10; i++)
	{	
		do
		{
			num = randomNum(num);
			if (num == 1)
				choice = "H";
			else
				choice = "T";
		
			cout << choice << payOff(win);	
			
		
		}while (choice != "H");

			cout << choice << endl;
			int count = i++;
			payOff(win)++;

	}
	average = payOff() / count;

	cout << "The average payout was $" << average << endl;

	return 0;
}

void payOff(double win)
{
	for (int i = 1; i != 0; i++)
	{
		win = pow(2, i);
		cout << "You win $" << win;
	}
}

int randomNum(int num)
{
	num = 1 + rand() % 2;
	return num;
}
How does one compute average? By dividing sum by count.

What is the count? Is it 10 for 10 games, or do you count only the games that did win? (I.e. no immediate H.)
1
2
3
4
5
6
7
size_t sum {};
contexpr size_t Count {10};
for ( size_t i = 0; i < Count; ++i )
{
  sum += game();
}
auto average = static_cast<double>(sum) / Count;



1
2
3
4
5
6
win = 1
while ( tail() )
{
  win *= 2
}
if ( win < 2 ) win = 0
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <random>
#include <chrono>


int main()
{
   const unsigned Heads = 0;
   const unsigned Tails = 1;

   // obtain a seed from the system clock:
   unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

   // create a random number engine using the previously obtained seed
   std::default_random_engine eng(seed);

   // create an int distribution to simulate a coin flip
   std::uniform_int_distribution coin_toss(Heads, Tails);

   unsigned int heads = 0;
   unsigned int tails = 0;

   do
   {
      unsigned flip = coin_toss(eng);

      (flip == Heads) ? heads++ : tails++;
   }
   while (heads < 10);

   if (heads > tails)
   {
      std::cout << "Heads";
   }
   else if (heads < tails)
   {
      std::cout << "Tails";
   }
   else
   {
      std::cout << "A tie";
   }

   std::cout << " was the winner!\n\n";

   std::cout << "(Heads: " << heads << ", Tails: " << tails << ")\n";
}

Heads was the winner!

(Heads: 10, Tails: 7)
so there is a total of ten rounds which the program will randomly flip the coin.

each round will have a payout as to 2^n (with n being how many flips were able to be done in a given round) the flips keep going until the coin lands on a Head. so if the program lands Tail, Tail, Tail, Head in round one, then the payout is 2^4 (4 total flips).

the average is based on the payouts/the 10 rounds. so I guess I don't need the count, I can just do the payouts++/10.

sorry I just did a redo on the whole project and have this so far. I tried to incorporate what both of you have said but some of the stuff I have yet to cover in class. I went less fancy and this is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	srand(time(0));	
	int flip;
	char choice;
	double average;	
		
	for (int i = 1; i <= 10; i++)
	{		
		for (choice = 'T'; choice != 'H'; i++)
		{
			flip = (rand() % 2) + 1;

			if (flip == 1)
				choice = 'H';
			else
				choice = 'T';

			cout << choice;
		} 									
	cout << flip << endl;
	}
	return 0;
}


This is kinda working. however, Its not giving me 10 rounds and its adding a 1 after each round. I'll worry about average once I get this straightened out.
I think your first post was actually closer. Some questions about that code. Read these questions and look at your code. Don't get frustrated if you can't answer the questions, keep reading below.

Line 20: what is i? Be specific. Now look at line 36. Why are you incrementing i here?
Line 30: What exactly does payoff() do? What does it return? You're printing whatever it returns here. Why?
Line 35. This will print the last choice, but you already printed it once at line 30.
Line 37. What's going on here? What are you trying to do?

In payOff():
Line 49: What is the purpose of this loop?
Line 52: This prints the payoff, but you print the return value of the payoff at lines 30 & 37. Who is actually supposed to print the payoff?

There isn't a good answer to most of my questions. Much of the code just doesn't do what it needs to do. It's very close, but not quite right.

When programming, you have to be very precise. You need to be clear in own mind what you're trying to accomplish. Then you need to write code to accomplish it. I strongly urge you to write comments in the code that say what the code is supposed to do. In theory, you should be able to read the comments and convince yourself that your algorithm will work. Comment your functions to say what they do and what they return. This would help avoid the ambiguity about payOff().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int randomNum(int num);

int
main()
{
    int num = 0;
    int totalWinnings = 0;
    string choice = "";
    double average;

    srand(time(0));		// seed the randon number generator

    // Play the game 10 times.
    for (int plays = 1; plays <= 10; ++plays) {
	//
	// The code below is for one play of the game
	//
	int flips = 0;		// the number of times we flip the coin
	do {
	    // Get a random number 1-2 in num. Then set "choice"
	    // to "H" for 1 and "T" for 2.
	    num = randomNum(num);
	    if (num == 1)
		choice = "H";
	    else
		choice = "T";

	    ++flips;		// increment flips
	    // Print the result of this flip. Note that there's no
	    // newline so each time through the loop we'll print H or T
	    // right next to the last one.
	    cout << choice;

	} while (choice != "H"); // the game ends when you get a Heads

	int winnings = pow(2, flips); // winnings for this game are 2^n
	// print the payoff for this game. Note that it's on the same line
	// as the H/T printouts.
	cout << " payoff is $" << winnings << '\n';

	// Increment the total winnings
	totalWinnings += winnings;
    }

    // Average payout is the total / the number of plays.
    // Express the # of plays to a double so the result is a double
    average = totalWinnings / 10.0;

    cout << "The average payout was $" << average << endl;

    return 0;
}

// Return a random number 1 or 2
int
randomNum(int num)
{
    num = 1 + rand() % 2;
    return num;
}

Thank you very much dhayden for your work up. It does look like I was close to getting what I actually wanted. I am going line by line and thinking of your questions while working through this. I'm actually going to start a new blank sheet and just retry, using your code as reference if I get stuck again. I'll start putting the comments as I'm working through it. I have been putting them in for reference after I completed the code, but it does make sense that it will help me define what a am making.

Thanks again to all who responded with help.
Just as an added (non-C++) extra, from probability theory what is the EXPECTATION of the payout for this problem?
Topic archived. No new replies allowed.