943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 567
  • C++ RSS
Sep 20th, 2009
0

c++ cointoss help neede plzzzzzzz

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kele1 is offline Offline
13 posts
since Sep 2009
Sep 20th, 2009
0

Re: c++ cointoss help neede plzzzzzzz

Great, you've got some code, and what exactly is your question?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Sep 20th, 2009
0

Re: c++ cointoss help neede plzzzzzzz

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kele1 is offline Offline
13 posts
since Sep 2009
Sep 20th, 2009
1

Re: c++ cointoss help neede plzzzzzzz

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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Sep 20th, 2009
0

Re: c++ cointoss help neede plzzzzzzz

First seed your rand function.
C++ Syntax (Toggle Plain Text)
  1. srand(time(0));

The this code :
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 100; i++)
  2. {
  3. int side = flip();
  4. if(side == 0) tails++;
  5. else heads++;
  6. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 20th, 2009
0

Re: c++ cointoss help neede plzzzzzzz

also you are starting the variable heads at one which will throw off you answer. you should initialize both heads and tails as 0.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009

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: Usefulness of size_t
Next Thread in C++ Forum Timeline: How to return a value from a class like the string class





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


Follow us on Twitter


© 2011 DaniWeb® LLC