coin toss problem

Reply

Join Date: Sep 2007
Posts: 2
Reputation: rdanda is an unknown quantity at this point 
Solved Threads: 0
rdanda's Avatar
rdanda rdanda is offline Offline
Newbie Poster

coin toss problem

 
0
  #1
Sep 15th, 2007
Yes, this is homework, but I am trying. The problem is:

Simulate coin tossing
Program must print Heads or Tails. Toss 100 times and count the number of times each side of the coin appears. Call a function "flip" that takes no arguments and returns 0 for tails and 1 for heads.

I know I have to use random, I think I have the logic for the function correct. What I'm trying to accomplish is flip the coin, if the coin is 0 then inclement the counter for tails, or if its not 0 inclement the heads counter and cout the appropriate response. My int Main is basically all messed up because based on the examples in my text book it appears that I should have my cout statements in main, but they are also in the function on some examples. Some of the commented out lines are my attempt to debug this thing. I spent four hours getting to this point and I'm not sure where to go. I don't want to just google the answer, I really want to learn this, which is difficult for a 43 y/o system admin trying to increase his knowledge base. C++ seems really neat and I look forward to getting a better understanding of it all. I seem to remember doing these problems when I took Java back in 1998, but, its a perishable skill apparently!!

Regards

Richard

  1. // Chapter3_3_34.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <iomanip>
  8. using std::cin;
  9. using std::endl;
  10. using namespace std;
  11. int flip(void);
  12.  
  13. int main();
  14.  
  15. //start random generator
  16. // srand( time( 0 ) );
  17.  
  18. // result = flip;
  19.  
  20. //start defenition of flip function
  21.  
  22. int flip( void )
  23. {
  24. int heads = 1; //count the heads result of the flip
  25. int tails = 0; //count the tails result of the flip
  26. int headsCounter = 0; //start the counter at 0
  27. int tailsCounter = 0; //start the counter at 0
  28. int flip; //the coin toss
  29.  
  30. flip = rand() % 2; //flip the coin
  31. if flip == 1
  32. headsCouter++;
  33. else tailsCounter++;
  34. cout << "You flipped heads: " << headsCounter "times" << endl;
  35. cout << "You flipped tails: " << tailsCounter "times" << endl;
  36.  
  37. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: coin toss problem

 
0
  #2
Sep 15th, 2007
>My int Main is basically all messed up because based on the examples in my text book it
>appears that I should have my cout statements in main, but they are also in the function on some examples.
Your main is basically messed up because it's a declaration and not a definition. You need to have a body for any function you intend to run. Something like this:
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. void flip();
  6.  
  7. int main()
  8. {
  9. std::srand ( static_cast<unsigned> ( std::time ( 0 ) ) );
  10. flip();
  11. }
  12.  
  13. void flip()
  14. {
  15. // Your coin flipping logic
  16. }
Note that srand needs to be called only one time, or very very infrequently. With modern random number generators, a single seed goes a long way.

As for your coin flipping logic, it looks largely decent. The syntax for your conditional is wrong because you need to surround the condition in parens:
  1. flip = rand() % 2;
  2.  
  3. if ( flip == 1 )
  4. headsCounter++;
  5. else
  6. tailsCounter++;
Finally, you should be placing this in a loop. Right now you're only testing one coin toss, so I'd recommend an argument to the function that specifies how many tosses you want, and a counting loop that contains the tossing code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: coin toss problem

 
0
  #3
Sep 15th, 2007
>Call a function "flip" that takes no arguments and returns 0 for tails and 1 for heads

I think narue's function declaration needs to be
int flip(), to adhere to the OP's homework assignment.

I would plan how to do this on paper, drawing a flow chart first. Then code the program without functions for brevity say. Once confident then use a function.
Last edited by iamthwee; Sep 15th, 2007 at 11:42 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: rdanda is an unknown quantity at this point 
Solved Threads: 0
rdanda's Avatar
rdanda rdanda is offline Offline
Newbie Poster

Re: coin toss problem

 
0
  #4
Sep 15th, 2007
excellent!! Thanks for the help. I'll give it a shot when I get home from work.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC