c++ cointoss help neede plzzzzzzz

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 13
Reputation: kele1 is an unknown quantity at this point 
Solved Threads: 0
kele1 kele1 is offline Offline
Newbie Poster

c++ cointoss help neede plzzzzzzz

 
0
  #1
Sep 20th, 2009
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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. //#include <stdafx.h>
  5. #include <iostream>
  6. #include <cstdlib>
  7.  
  8. using std::cout;
  9. using std::rand;
  10.  
  11. int flip();
  12.  
  13. int main(void)
  14. {
  15.  
  16. int heads = 1;
  17. int tails = 0;
  18. int coinToss = 100;
  19. int face;
  20.  
  21. for (int toss = 0; toss < coinToss; toss++)
  22. {
  23. flip();
  24. if (toss == 1)
  25.  
  26. heads++;
  27.  
  28. else
  29.  
  30. tails++;
  31.  
  32. }
  33. printf("%s%5s\n", "face", " heads or tails");
  34. printf(" heads%5d\n", heads);
  35. printf(" tails%5d\n", tails);
  36.  
  37.  
  38. int flip()
  39. {
  40. return rand() % 2;
  41.  
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: c++ cointoss help neede plzzzzzzz

 
0
  #2
Sep 20th, 2009
Great, you've got some code, and what exactly is your question?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: kele1 is an unknown quantity at this point 
Solved Threads: 0
kele1 kele1 is offline Offline
Newbie Poster

Re: c++ cointoss help neede plzzzzzzz

 
0
  #3
Sep 20th, 2009
i keep getting errors when i execute it and i don't get what i want. also it doesn't call the flip function that gives me 0 for tail and 1 for heads. i need to know if the code is correct.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: c++ cointoss help neede plzzzzzzz

 
1
  #4
Sep 20th, 2009
You have forgotten to use the return value of flip(). If you did this
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,451
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 188
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: c++ cointoss help neede plzzzzzzz

 
0
  #5
Sep 20th, 2009
First seed your rand function.
  1. srand(time(0));

The this code :
  1. for (int toss = 0; toss < coinToss; toss++)
  2. {
  3. flip();
  4. if (toss == 1)
  5.  
  6. heads++;
  7.  
  8. else
  9.  
  10. tails++;
  11.  
  12. }

is wrong.

You need to assign the value return to a variable and then use it to compare.
  1. for(int i = 0; i < 100; i++)
  2. {
  3. int side = flip();
  4. if(side == 0) tails++;
  5. else heads++;
  6. }
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: c++ cointoss help neede plzzzzzzz

 
0
  #6
Sep 20th, 2009
also you are starting the variable heads at one which will throw off you answer. you should initialize both heads and tails as 0.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Reply

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




Views: 269 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC