| | |
Dice program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 11 #define TOTAL 36000 int main(void) { int cnt = 0; int die1, die2, sum; int array[SIZE]; srand(time(NULL)); for (cnt = 0; cnt < SIZE; cnt++) array[cnt] = 0; for (cnt = 0; cnt < TOTAL; cnt++) { die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; sum = die1 + die2; if (sum == 2) array[0] = array[0] + 1; if (sum == 3) array[1] = array[1] + 1; if (sum == 4) array[2] = array[2] + 1; if (sum == 5) array[3] = array[3] + 1; if (sum == 6) array[4] = array[4] + 1; if (sum == 7) array[5] = array[5] + 1; if (sum == 8) array[6] = array[6] + 1; if (sum == 9) array[7] = array[7] + 1; if (sum == 10) array[8] = array[8] + 1; if (sum == 11) array[9] = array[9] + 1; if (sum == 12) array[10] = array[10] + 1; } for (cnt = 0; cnt < SIZE; cnt++) printf("Number of %d's: %d\n", cnt+2, array[cnt]); return 0; }
hmm.. try this:
for (cnt = 0; cnt < TOTAL; cnt++)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
++array[sum-2];
}•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
•
•
•
•
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]; }
Outside of incrementing the array I don't see how it stores the sum :eek:
I'm not sure what ye' were trying to accomplish but I think
basically does the same things as all these if statements
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.
C Syntax (Toggle Plain Text)
++array[sum-2] ;
basically does the same things as all these if statements
C Syntax (Toggle Plain Text)
if (sum == 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.
![]() |
Similar Threads
- C++ Random Numbers (C++)
- Need Help on my Program (Python)
- dice program..... (Shell Scripting)
- rolling dice (C)
- Building an array for dice program (C#)
- Random numbers...really random?? (Java)
Other Threads in the C Forum
- Previous Thread: Can't get around this crash
- Next Thread: Opening Image file?
Views: 2328 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf openwebfoundation overwrite pause pdf pointer posix process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student system testing threads turboc unix urboc user variable whythiscodecausesegmentationfault windowsapi





