coin flip

I had to  do an assignment where I  had to develop a program that flipped a coin 100 times.  I got it wrong, the instructor said in response to my assignment: "the requirements were to have a seperate flip() function with no arguments that only returned 0 for tails and 1 for heads: instead your flip function contains the loop, you should have the loop in main and it should call the flip function.  I have tried redoing it, and it works but now I am not calling the flip function at all, could someone see my error.  I am attaching both the original I did, as well as what I was trying to correct.
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

Open in new window

rebelcowgirl_73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
Hi rebelcowgirl_73,

It looks like both implementations fail the requirement that flip () return heads/tails.  The topmost version of flip() always returns 0.  It should return 0 or 1.

Main should be a simple loop:

main ()
{
  int flipcount;
  int heads = 0;
  int tails = 0;

  for (flipcount = 0; flipcount < 100;  ++flipcount)
    if (flip ())
      ++heads  ;
    else
      ++ tails;
}

Start here, and it shoud fall easily into place.

Good Luck,
Kent
0
evilrixSenior Software Engineer (Avast)Commented:
You probably wanted something like this...
#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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
evilrix,
Looks like you are explicity solving the homework...
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

evilrixSenior Software Engineer (Avast)Commented:
It's not homework -- read the Q again!
"I had to  do an assignment where I  had to develop a program that flipped a coin 100 times.  I got it wrong..."
0
evilrixSenior Software Engineer (Avast)Commented:
...hence I said, "wanted" and not "want"!
0
rebelcowgirl_73Author Commented:
thanks so much, so basically I was on the right path, just needed to put the flip = (rand () % 2) into the flip function to determine if it would be a 1 for heads and a 2 for tails.  Sad thing is I had a working program but the requirements didn't allow the for statement to be in the flip function.  Thanks for the clarification!
0
rebelcowgirl_73Author Commented:
^^^^meant 0 for tails !!!!!!
0
evilrixSenior Software Engineer (Avast)Commented:
You wasn't far out at all. Basically, the code I posted was your first version with a couple of minor changes. Compare them, try so see what I did differently. It was a good attempt.

-Rx.
0
Jaime OlivaresSoftware ArchitectCommented:
>>It's not homework -- read the Q again!
>>"I had to  do an assignment where I  had to develop a program that flipped a coin 100 times.  I got it wrong..."
Assignment=homework in most english "dialects"
If you look at author's profile you will notice it is a homework (other Qs: gcd, factors, etc).
So, just want to comment this: we are not dirty lawyers trying to legally evade the law. At least we have to ask if this is a homework, to guide the asker as KDO did.
0
evilrixSenior Software Engineer (Avast)Commented:
The Q stated it WAS an assignment --past tense, so by definition it not homework! All I did was take the OPS code and fix a small defect. I'd hardly call that providing a full code answer. If you look at what I posted it is basically the OPs code with a minor fix. I am not going to debate this further with you (I see no need to defend my actions since they were quite appropriate). If you feel I have broken EE rules by all means feel free to report it to CS.
0
rebelcowgirl_73Author Commented:
I understand jaime, and I always state if it is or not.  I like to get help not the answers.  I showed all work to show I was trying to discover another possible answer.  But I was not trying to get someone to do it for me!  
0
Jaime OlivaresSoftware ArchitectCommented:
@rebel,
your question is totally ok.

>>The Q stated it WAS an assignment --past tense, so by definition it not homework!
Yeah, yeah, you should be a good lawyer.
0
Kent OlsenDBACommented:
Hi jaime_olivares,

>> Yeah, yeah, you should be a good lawyer.

That's cruel.  :)  suggesting that someone would be a good lawyer.  :)

--  99.7% of all lawyers give the other .3% a bad name.



Hi rebelcowgirl_73,

Interesting name.  Is there a story here?


Kent
0
rebelcowgirl_73Author Commented:
Yes there is a story behind it, not really enteresting tho!  Got the name in my twentys for being a rebel cowgirl!  Always doing things my way!  lol
0
Kent OlsenDBACommented:

Rebels are always welcome around here.  :)  Of course, there aren't any others here yet.  Infinity08, Evilrix, me.  We're all good citizens, observe the Boy Scout rules, vote in every election, cross only at the crosswalks (and with the signal), etc.

I can't speak for the others, but in my case it's probably from too many years of being married.  I've got "Yes, Dear" down to an art form.   :^}


Kent
0
evilrixSenior Software Engineer (Avast)Commented:
>> I've got "Yes, Dear" down to an art form
Join the club :)
0
Kent OlsenDBACommented:

Too late to do me any good, a buddy explained that two magic words are the secret to a successful marriage.  I just assumed that "yes, dear" was the magic phrase and that I was already an expert.  But clearly I was mistaken.

"Fine, whatever" is all that you need.  It doesn't matter what the statement is.

"I'm going shopping.  Do you want to come along?"
"Fine, whatever".

"I don't feel like cooking tonight.  Let's eat out"
"Fine, whatever".

"I think that we should paint the living room green."
"Fine, whatever".

"My mother is coming to visit for a month."
"Fine, whatever".


See.  It just works.    :^}

0
evilrixSenior Software Engineer (Avast)Commented:
>> and that I was already an expert
Maybe there should be a marriage zone?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.