We help IT Professionals succeed at work.

coin flip

rebelcowgirl_73
on
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

Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
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
Senior Software Engineer (Avast)
CERTIFIED EXPERT
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

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
evilrix,
Looks like you are explicity solving the homework...
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

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..."
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
...hence I said, "wanted" and not "want"!

Author

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!

Author

Commented:
^^^^meant 0 for tails !!!!!!
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

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.
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

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..."
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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

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.

Author

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!  
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
@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.
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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

Author

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
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:

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
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> I've got "Yes, Dear" down to an art form
Join the club :)
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:

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.    :^}

evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> and that I was already an expert
Maybe there should be a marriage zone?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.