Dice program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Dice program

 
0
  #1
Mar 23rd, 2006
OK I wrote this dice program up and it works really well. Just curious though if there was a way to reduce the number of "if" statements.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 11
  6. #define TOTAL 36000
  7.  
  8. int main(void)
  9. {
  10. int cnt = 0;
  11. int die1, die2, sum;
  12. int array[SIZE];
  13.  
  14. srand(time(NULL));
  15.  
  16. for (cnt = 0; cnt < SIZE; cnt++)
  17. array[cnt] = 0;
  18.  
  19. for (cnt = 0; cnt < TOTAL; cnt++)
  20. {
  21. die1 = rand() % 6 + 1;
  22. die2 = rand() % 6 + 1;
  23. sum = die1 + die2;
  24.  
  25. if (sum == 2)
  26. array[0] = array[0] + 1;
  27. if (sum == 3)
  28. array[1] = array[1] + 1;
  29. if (sum == 4)
  30. array[2] = array[2] + 1;
  31. if (sum == 5)
  32. array[3] = array[3] + 1;
  33. if (sum == 6)
  34. array[4] = array[4] + 1;
  35. if (sum == 7)
  36. array[5] = array[5] + 1;
  37. if (sum == 8)
  38. array[6] = array[6] + 1;
  39. if (sum == 9)
  40. array[7] = array[7] + 1;
  41. if (sum == 10)
  42. array[8] = array[8] + 1;
  43. if (sum == 11)
  44. array[9] = array[9] + 1;
  45. if (sum == 12)
  46. array[10] = array[10] + 1;
  47. }
  48.  
  49. for (cnt = 0; cnt < SIZE; cnt++)
  50. printf("Number of %d's: %d\n", cnt+2, array[cnt]);
  51.  
  52. return 0;
  53. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training

Re: Dice program

 
0
  #2
Mar 23rd, 2006
hmm.. try this:
for (cnt = 0; cnt < TOTAL; cnt++)
{	
   die1 = rand() % 6 + 1;
   die2 = rand() % 6 + 1;
     sum = die1 + die2;
     ++array[sum-2];
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Dice program

 
0
  #3
Mar 23rd, 2006
Originally Posted by Clinton Portis
hmm.. try this:
for (cnt = 0; cnt < TOTAL; cnt++)
{	
   die1 = rand() % 6 + 1;
   die2 = rand() % 6 + 1;
     sum = die1 + die2;
     ++array[sum-2];
}
Erm what exactly does ++array[sum-2] do?

Outside of incrementing the array I don't see how it stores the sum :eek:
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training

Re: Dice program

 
0
  #4
Mar 23rd, 2006
I'm not sure what ye' were trying to accomplish but I think
  1.  
  2. ++array[sum-2] ;

basically does the same things as all these if statements
  1. if (sum == 2)
  2. array[0] = array[0] + 1;

which appear to just be adding 1 to whatever is stored in your array element.. which appears to always be 2 less than your sum value.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Dice program

 
0
  #5
Mar 23rd, 2006
Oh ok thanks.
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: 2328 | Replies: 4
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC