944,026 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4950
  • C RSS
Mar 23rd, 2006
0

Dice program

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 23rd, 2006
0

Re: Dice program

hmm.. try this:
for (cnt = 0; cnt < TOTAL; cnt++)
{	
   die1 = rand() % 6 + 1;
   die2 = rand() % 6 + 1;
     sum = die1 + die2;
     ++array[sum-2];
}
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Mar 23rd, 2006
0

Re: Dice program

Quote 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:
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 23rd, 2006
0

Re: Dice program

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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Mar 23rd, 2006
0

Re: Dice program

Oh ok thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

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: Can't get around this crash
Next Thread in C Forum Timeline: Opening Image file?





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


Follow us on Twitter


© 2011 DaniWeb® LLC