| | |
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
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)
// Chapter3_3_34.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using std::cin; using std::endl; using namespace std; int flip(void); int main(); //start random generator // srand( time( 0 ) ); // result = flip; //start defenition of flip function int flip( void ) { int heads = 1; //count the heads result of the flip int tails = 0; //count the tails result of the flip int headsCounter = 0; //start the counter at 0 int tailsCounter = 0; //start the counter at 0 int flip; //the coin toss flip = rand() % 2; //flip the coin if flip == 1 headsCouter++; else tailsCounter++; cout << "You flipped heads: " << headsCounter "times" << endl; cout << "You flipped tails: " << tailsCounter "times" << endl; }
>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:
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:
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.
>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)
#include <cstdlib> #include <ctime> #include <iostream> void flip(); int main() { std::srand ( static_cast<unsigned> ( std::time ( 0 ) ) ); flip(); } void flip() { // Your coin flipping logic }
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)
flip = rand() % 2; if ( flip == 1 ) headsCounter++; else tailsCounter++;
I'm here to prove you wrong.
>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.
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.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Coin Toss Simulation, getting all Tails and one Heads (C++)
- One problem after another (Troubleshooting Dead Machines)
- Max Consecutive Heads/Tails (Visual Basic 4 / 5 / 6)
- Desperate and eagar to learn!!! (C++)
Other Threads in the C++ Forum
- Previous Thread: Device handling with C++
- Next Thread: Help me! C++
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






