Your question, your audience. Choose who sees your identity—and your question—with question security.
int flip () //start of flip function
{
int heads = 1; //each head returns a 1
int tails = 0; //each tail returns a 0
int counterHeads = 0; //initialize counter for heads at 0
int counterTails = 0; //initialize counter for tails at 0
int flip; //coin toss
int totalFlips = 100; //flip coin 100 times
for (int i = 1; i <= totalFlips; i++){
flip = rand() % 2; //flip the coin
if (flip == 1){
counterHeads++;
cout << setw(10) << "Heads";
}
else{
counterTails++;
cout << setw(10) <<"Tails";
}
}
cout << endl;
cout << "Heads was flipped " << counterHeads << endl;
cout << "Tails was flipped " << counterTails << endl;
return 0;
}//end flip function
int main ()
{
flip();
return 0;
}//end main function
________________________Original Code________________________________
int flip () //start of flip function
{
if (int flip = 1)
return 1;
else
return 0;
}//end flip function
int main ()
{
int totalFlips = 100; //flip coin 100 times
int counterHeads = 0; //initialize counter for heads at 0
int counterTails = 0; //initialize counter for tails at 0
int flip;
for (int i = 1; i <= totalFlips; i++)
{
flip = rand() % 2; //flip the coin
if (flip() == 1)
{
counterHeads++;
cout << "Heads";
}
else
{
counterTails++;
cout << "Tails";
}
}
cout << endl;
cout << "Heads was flipped " << counterHeads << endl;
cout << "Tails was flipped " << counterTails << endl;
return 0;
}//end main function
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
bool flip () //start of flip function
{
return 1 == (rand() % 2);
}//end flip function
int main ()
{
srand(static_cast<int>(time(NULL)));
int counterHeads = 0; //initialize counter for heads at 0
int counterTails = 0; //initialize counter for tails at 0
for (int i = 1; i <= 100; i++)
{
if (flip() == 1)
{
counterHeads++;
//cout << "Heads";
}
else
{
counterTails++;
//cout << "Tails";
}
}
cout << "Heads was flipped " << counterHeads << endl;
cout << "Tails was flipped " << counterTails << endl;
return 0;
}//end main function
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.