for loop

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 3
Reputation: fx7202 is an unknown quantity at this point 
Solved Threads: 0
fx7202 fx7202 is offline Offline
Newbie Poster

for loop

 
0
  #1
Mar 18th, 2009
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 @.

  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 class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <blockquote style="font-size:11px">not sure if error is here as the MaxNum should be getting passed to the loop inside the function</blockquote> </div> if (isEvenSided(pvalue) != false) {total++;}
  30.  
  31. maxNumber = 1000;
  32.  
  33. simDiceThrow(maxNumber, pvalue); <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <blockquote style="font-size:11px">im wondering how will the if statement run if there is no loop outside??</blockquote> </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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: for loop

 
0
  #2
Mar 19th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: fx7202 is an unknown quantity at this point 
Solved Threads: 0
fx7202 fx7202 is offline Offline
Newbie Poster

Re: for loop

 
0
  #3
Mar 19th, 2009
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.

  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:
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 142
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 34
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: for loop

 
0
  #4
Mar 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 311
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: for loop

 
0
  #5
Mar 19th, 2009
good you have taken some courage .
if you still can't underestand why look at this code and
think why ?


  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. }
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: fx7202 is an unknown quantity at this point 
Solved Threads: 0
fx7202 fx7202 is offline Offline
Newbie Poster

Re: for loop

 
0
  #6
Mar 19th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC