Link to home
Start Free TrialLog in
Avatar of rebelcowgirl_73
rebelcowgirl_73

asked on

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

SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
evilrix,
Looks like you are explicity solving the homework...
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..."
...hence I said, "wanted" and not "want"!
Avatar of rebelcowgirl_73
rebelcowgirl_73

ASKER

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!
^^^^meant 0 for tails !!!!!!
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.
>>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.
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.
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!  
@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.
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
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

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
>> I've got "Yes, Dear" down to an art form
Join the club :)

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

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