![]() |
| ||
| coin toss problem 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 // Chapter3_3_34.cpp : Defines the entry point for the console application. |
| ||
| 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: #include <cstdlib>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: flip = rand() % 2;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. |
| ||
| 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. |
| ||
| Re: coin toss problem excellent!! Thanks for the help. I'll give it a shot when I get home from work. |
| All times are GMT -4. The time now is 5:17 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC