#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# coin flip in C++

Member Posts: 5
write a program that simulates coin tossing.for each toss of coin the program should print HEADS or TAILS.let the program toss the coin hundred times and count the number of times each side of coin appears.print the results.the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads.Note if the program realistically simulates the coin tossing,then each side of coin should appear approximately half the time.

• Member Posts: 770 ✭✭✭
: write a program that simulates coin tossing.for each toss of coin the program should print HEADS or TAILS.let the program toss the coin hundred times and count the number of times each side of coin appears.print the results.the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads.Note if the program realistically simulates the coin tossing,then each side of coin should appear approximately half the time.
:
:

So what's the problem for you? How far have you gotten? What parts are you having difficulty with?
• Member Posts: 5
: : write a program that simulates coin tossing.for each toss of coin the program should print HEADS or TAILS.let the program toss the coin hundred times and count the number of times each side of coin appears.print the results.the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads.Note if the program realistically simulates the coin tossing,then each side of coin should appear approximately half the time.
: :
: :
:
: So what's the problem for you? How far have you gotten? What parts are you having difficulty with?
:
the output is not printing out the way I want it to

this is the code so far

#include
#include
#include

using std::cout;
using std::cin;
using std::endl;
using std::setw;

int flip_coin();

int main ()
{
unsigned seed;
int flip =1;
cout<<"Enterseed:";
cin>> seed;
srand ( seed);

for(flip =1; flip <=100; flip++)
{

cout<<flip<< flip_coin()<<endl;

}
return 0;

}

int flip_coin(void)
{

return rand()%2;
}

It is the main function I think is the problem.
I want to print out the 1's rsult undr heads and 0's under the tails.
• Member Posts: 770 ✭✭✭
[b][red]This message was edited by HK_MP5KPDW at 2004-11-15 11:20:31[/red][/b][hr]
: : : write a program that simulates coin tossing.for each toss of coin the program should print HEADS or TAILS.let the program toss the coin hundred times and count the number of times each side of coin appears.print the results.the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads.Note if the program realistically simulates the coin tossing,then each side of coin should appear approximately half the time.
: : :
: : :
: :
: : So what's the problem for you? How far have you gotten? What parts are you having difficulty with?
: :
: the output is not printing out the way I want it to
:
: this is the code so far
:

[blue]Use code tags when posting code! If you want an individual listing per each roll (as it looks you've currently got your loop set up to do) of whether we have a heads/tails situation nicely alligned under their respective "Heads"/"Tails" column, then you will need to first determine if the result from calling that function is heads/tails and then format your output accordingly. If heads, then you would output the data followed by a newline character. If tails, then you would need to do something along the lines of a [italic]setw[/italic] for some appropriate length to get your output to line up underneath the "Tails" column.[/blue]

[code]#include
#include
#include

using std::cout;
using std::cin;
using std::endl;
using std::setw;

int flip_coin();

int main ()
{
unsigned seed;
int flip =1;
cout<<"Enterseed:";
cin>> seed;
srand ( seed);

for(flip =1; flip <=100; flip++)
{
[red]// Get the result from the call to flip_coin first and
// then determine how to format your output and display the result
// based on that.[/red]
cout<<flip<< flip_coin()<<endl;
}
return 0;

}

int flip_coin(void)
{
return rand()%2;
}[/code]

:
: It is the main function I think is the problem.
: I want to print out the 1's rsult undr heads and 0's under the tails.
:

• Member Posts: 5
: [b][red]This message was edited by HK_MP5KPDW at 2004-11-15 11:20:31[/red][/b][hr]
: : : : write a program that simulates coin tossing.for each toss of coin the program should print HEADS or TAILS.let the program toss the coin hundred times and count the number of times each side of coin appears.print the results.the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads.Note if the program realistically simulates the coin tossing,then each side of coin should appear approximately half the time.
: : : :
: : : :
: : :
: : : So what's the problem for you? How far have you gotten? What parts are you having difficulty with?
: : :
: : the output is not printing out the way I want it to
: :
: : this is the code so far
: :
:
: [blue]Use code tags when posting code! If you want an individual listing per each roll (as it looks you've currently got your loop set up to do) of whether we have a heads/tails situation nicely alligned under their respective "Heads"/"Tails" column, then you will need to first determine if the result from calling that function is heads/tails and then format your output accordingly. If heads, then you would output the data followed by a newline character. If tails, then you would need to do something along the lines of a [italic]setw[/italic] for some appropriate length to get your output to line up underneath the "Tails" column.[/blue]
:
: [code]#include
: #include
: #include
:
: using std::cout;
: using std::cin;
: using std::endl;
: using std::setw;
:
: int flip_coin();
:
: int main ()
: {
: unsigned seed;
: int flip =1;
: cout<<"Enterseed:";
: cin>> seed;
: srand ( seed);
:
:
: for(flip =1; flip <=100; flip++)
: {
: [red]// Get the result from the call to flip_coin first and
: // then determine how to format your output and display the result
: // based on that.[/red]
: cout<<flip<< flip_coin()<<endl;
: }
: return 0;
:
: }
:
: int flip_coin(void)
: {
: return rand()%2;
: }[/code]
:
: :
: : It is the main function I think is the problem.
: : I want to print out the 1's rsult undr heads and 0's under the tails.
: :
:
:
:
:
thanks for ur help. I was looking at the cion filp program now in arrays. if i need help I'll hit you up
ps. good looking out