944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4951
  • C++ RSS
Sep 15th, 2007
0

coin toss problem

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdanda is offline Offline
2 posts
since Sep 2007
Sep 15th, 2007
0

Re: coin toss problem

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 15th, 2007
0

Re: coin toss problem

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 15th, 2007
0

Re: coin toss problem

excellent!! Thanks for the help. I'll give it a shot when I get home from work.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdanda is offline Offline
2 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Device handling with C++
Next Thread in C++ Forum Timeline: Help me! C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC