| | |
c++ cointoss help neede plzzzzzzz
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
i'm supposed to write a program that simulates cointoss. for each toss the program should print heads or tails and it would toss 100 times and counts the number off times each side of the coin appears. it should also call a seperate function 'flip' that takes no argument and returns 0 for tails and 1 for heads. pease help meeee its due today. this is what i have so far:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> //#include <stdafx.h> #include <iostream> #include <cstdlib> using std::cout; using std::rand; int flip(); int main(void) { int heads = 1; int tails = 0; int coinToss = 100; int face; for (int toss = 0; toss < coinToss; toss++) { flip(); if (toss == 1) heads++; else tails++; } printf("%s%5s\n", "face", " heads or tails"); printf(" heads%5d\n", heads); printf(" tails%5d\n", tails); int flip() { return rand() % 2; } }
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
You have forgotten to use the return value of flip(). If you did this
If really helps lots to run you code on paper, with a list of variables when the code is small and you are beginning. They you would see why you only get one head.
It also helps to printout the results every turn.
if (flip()==1) . I think you would be a lot better than looking at your loop count varible. If really helps lots to run you code on paper, with a list of variables when the code is small and you are beginning. They you would see why you only get one head.
It also helps to printout the results every turn.
experience is the most expensive way to learn anything
First seed your rand function.
The this code :
is wrong.
You need to assign the value return to a variable and then use it to compare.
C++ Syntax (Toggle Plain Text)
srand(time(0));
The this code :
C++ Syntax (Toggle Plain Text)
for (int toss = 0; toss < coinToss; toss++) { flip(); if (toss == 1) heads++; else tails++; }
is wrong.
You need to assign the value return to a variable and then use it to compare.
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < 100; i++) { int side = flip(); if(side == 0) tails++; else heads++; }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
![]() |
Similar Threads
- reformat or buy new computer? (Windows NT / 2000 / XP)
- Help me with "Math Library" Related coding =) (C++)
- passing arrays to classes? (Java)
- can anyone assiat in getting my function to work? (C)
- I need an ide for php (PHP)
- help neede about converting characters (C++)
- Lots of problems; probably spyware and virus - please help (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Usefulness of size_t
- Next Thread: How to return a value from a class like the string class
Views: 269 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






