So, this is my coding below. Both T and H are 2 dollar and it should end when it gets H. Runs 10 games.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(0));
int i;
bool out = true;
double money = 0, total = 0, average;
for (i = 1; i <= 10; i++)
{
do
{
int coinflip = rand() % 2;
if (coinflip == 1)
{
cout << "H" << endl;
total = 2.00;
out = false;
}
if (coinflip == 0)
{
cout << "T";
money = 2.00;
total *= money;
}
} while (out);
cout << "You win $ " << total << endl;
}
average = total / 10;
cout << "The average payout was $" << average << endl;
return 0;
}
displys
H
You win $ 2
TYou win $ 4
H
You win $ 2
TYou win $ 4
TYou win $ 8
H
You win $ 2
H
You win $ 2
TYou win $ 4
TYou win $ 8
TYou win $ 16
The average payout was $1.6
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
int main()
{
std::srand( std::time(0) ) ;
constint N = 10 ;
double total = 0 ;
for( int i = 0 ; i < N ; ++i )
{
int amount_won = 2 ; // amount won is at least two (for the terminating H)
while( std::rand() % 1000 < 500 ) // half the time, till a head comes up
{
amount_won += 2 ; // add $2 for every T
std::cout << 'T' ;
}
std::cout << "H You win $" << amount_won << ".00\n" ;
total += amount_won ;
}
std::cout << "\naverage: $" << std::fixed << std::setprecision(2) << total / N << '\n' ;
}