943,777 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 542
  • C++ RSS
Mar 18th, 2009
0

for loop

Expand Post »
greetings everyone,

i seem to be having an issue with a function that has a reference n a value to do a loop. the nature of the prog, is to roll a dice (n) times n then count the number of time srand rolls an even num.

for the most part i think it is working--no errors, but inside a function im calling another function n then asking it to loop. And that is where im stuck @.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int simDiceThrow(int, int&);
  9. int diceThrow(int&);
  10. bool isEvenSided(int&);
  11.  
  12. const long MAX=6, MIN=1;
  13.  
  14. int main ()
  15. {
  16. int diceA,
  17. total=0,
  18. totalOne=0,
  19. pvalue,
  20. maxNumber=0;
  21.  
  22. srand((unsigned)time(NULL));
  23.  
  24.  
  25. cout <<" Dice Counter\n\n";
  26.  
  27. maxNumber = 100;
  28.  
  29. simDiceThrow(maxNumber, pvalue);<div> <div class="quote"> <div class="quote_header"> Quote ... </div> <blockquote class="quote_body"> not sure if error is here as the MaxNum should be getting passed to the loop inside the function </blockquote> </div> </div> if (isEvenSided(pvalue) != false) {total++;}
  30.  
  31. maxNumber = 1000;
  32.  
  33. simDiceThrow(maxNumber, pvalue);<div> <div class="quote"> <div class="quote_header"> Quote ... </div> <blockquote class="quote_body"> im wondering how will the if statement run if there is no loop outside?? </blockquote> </div> </div> if (isEvenSided(pvalue) != false) {totalOne++;}
  34.  
  35. cout <<"100 x dice thrown "
  36. <<" " <<total <<" " <<pvalue;
  37.  
  38. cout <<"\n1000 x dice thrown"
  39. <<" " <<totalOne <<" " <<pvalue;
  40.  
  41.  
  42. _flushall();
  43. cin.get();
  44.  
  45. return 0;
  46.  
  47. }
  48. int diceThrow (int &diceA)
  49. {
  50. return (diceA = (rand() % (MAX - MIN +1))+ MIN);
  51.  
  52. }
  53.  
  54. bool isEvenSided (int &pvalue)
  55. {
  56. return ((pvalue%2)== 0);
  57. }
  58.  
  59. int simDiceThrow(int maxNumber, int &diceA)
  60. {
  61. int pvalue;
  62.  
  63. for (int roll = 0; roll < maxNumber; roll++) {pvalue = diceThrow(diceA);}
  64. return pvalue;
  65. }

any suggestions are greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fx7202 is offline Offline
3 posts
since Mar 2009
Mar 19th, 2009
0

Re: for loop

C++ Syntax (Toggle Plain Text)
  1. if (isEvenSided(pvalue) != false) {total++;}

How can you compute the total outside the loop ? you should
pass the total also as a reference. And inside the function you
can make it to zero first and add it if even.

First draw a structure chart. Then you can clearly model this.
Last edited by NicAx64; Mar 19th, 2009 at 2:01 am.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 19th, 2009
0

Re: for loop

alright so after getting some dinner, i decided to redo the whole thing. from the drawing board i came up with the following code, but something is fishie about the count. it runs the loop for the srand and from what i can tell moves the counter but the count is not always right.

C++ Syntax (Toggle Plain Text)
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. int simDiceThrow(int, int&);
  9. int diceThrow();
  10. bool isEvenSided(int&);
  11.  
  12. int main ()
  13. {
  14. int total=0,
  15. totalOne=0,
  16. maxNumber;
  17.  
  18. srand((unsigned)time(NULL));
  19.  
  20.  
  21. cout <<" Dice Counter\n\n";
  22.  
  23. maxNumber = 3;
  24. simDiceThrow(maxNumber, total);
  25.  
  26. cout <<"total even for 100: " <<total <<endl;
  27.  
  28. maxNumber = 5;
  29. simDiceThrow(maxNumber, totalOne);
  30.  
  31. cout <<"total even for 1000: " <<totalOne <<endl;
  32.  
  33.  
  34. cin.get();
  35. cin.get();
  36.  
  37. return 0;
  38.  
  39. }
  40. int diceThrow ()
  41. {
  42. int diceA = (rand()% 6+ 1);
  43. return diceA;
  44. }
  45.  
  46. bool isEvenSided (int &pvalue)
  47. {
  48. return (pvalue %2 == 0);
  49. }
  50.  
  51. int simDiceThrow(int maxNumber, int &total)
  52. {
  53. int pvalue;
  54.  
  55. for (int roll = 1; roll <= maxNumber; roll++)
  56. {
  57. cout <<setw(5) <<diceThrow() <<endl;
  58.  
  59. pvalue=diceThrow();
  60.  
  61. if (isEvenSided(pvalue)) {total++;}
  62.  
  63. }
  64. }

that produces some like this:
Quote ...
Dice Counter

1
3
2
total even for 3: 1 //this one is correct
6
5
4
4
3
total even for 5: 4 //this one isnt, i am thinking something a zero but dont know where to eliminate such prob
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fx7202 is offline Offline
3 posts
since Mar 2009
Mar 19th, 2009
0

Re: for loop

In your simDiceThrow() loop you output the dice values, but then calculate the number of even values by throwing the dice again. So the number of even values has no relation to the numbers previously output. The logic you actually need is to throw the dice n times, remembering each value thrown. Calculate the number of even values, then output the number set and total.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Mar 19th, 2009
0

Re: for loop

good you have taken some courage .
if you still can't underestand why look at this code and
think why ?


C++ Syntax (Toggle Plain Text)
  1. int simDiceThrow(int maxNumber, int &total)
  2. {
  3. int pvalue;
  4.  
  5. for (int roll = 1; roll <= maxNumber; roll++)
  6. {
  7. //cout <<setw(5) <<diceThrow() <<endl;
  8.  
  9. pvalue=diceThrow();
  10. cout << setw(5) << pvalue << endl ;
  11.  
  12. if (isEvenSided(pvalue)) {total++;}
  13.  
  14. }
  15. }
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 19th, 2009
0

Re: for loop

i now see where the error what and why it was, so here is my understanding of it.

num gets generated it then gets passed over to the bool and gets counted if return is true. my first issue was that the bool was not in the loop, then after that was corrected, the value was not passed over to the bool for count. here is the updated function. works great. thank you guys for pushing.

C++ Syntax (Toggle Plain Text)
  1. int simDiceThrow(int maxNumber, int &total)
  2. {
  3. for (int roll = 1; roll <= maxNumber; roll++)
  4. {
  5. int evalue=diceThrow();
  6.  
  7. cout <<setw(2) <<evalue;
  8. if (roll %25 == 0) cout <<endl; //spacing
  9.  
  10. if (isEvenSided(evalue)) {total++;}
  11. }
  12. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fx7202 is offline Offline
3 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Array subscript error: invalid types char[int] (?)
Next Thread in C++ Forum Timeline: Dynamic Array to Static Array





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


Follow us on Twitter


© 2011 DaniWeb® LLC