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.

``````#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;
[b]++array[sum-2];[/b]
}``````

hmm.. try this:

``````for (cnt = 0; cnt < TOTAL; cnt++)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
[b]++array[sum-2];[/b]
}``````

Erm what exactly does ++array[sum-2] do?

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

``++array[sum-2] ;``

basically does the same things as all these if statements

``````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.

Oh ok thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.