Just wanted to know, I'm just doing a little dice rolling thing thats storing dice values according to how many times they come up every so often, just wondering if instead of making 50 lines of code doing
if(sum==1)
{
ones+=1;
}
else if(sum==2)
{...
and so on,
I'm thinking of arrays and stuff but cant come up with anything, I like my code to be short and effective, I HATE if-thens if used like I have it above and would like to use a more clever way.

Recommended Answers

All 7 Replies

Just wanted to know, I'm just doing a little dice rolling thing thats storing dice values according to how many times they come up every so often, just wondering if instead of making 50 lines of code doing
if(sum==1)
{
ones+=1;
}
else if(sum==2)
{...
and so on,
I'm thinking of arrays and stuff but cant come up with anything, I like my code to be short and effective, I HATE if-thens if used like I have it above and would like to use a more clever way.

How about something like this, assuming 2 six-sided dice.

int numInstances[12];
// initialize array to 0.

for (int i = 0; i < NUM_ROLLS)
{
     // code to calculate sum
     numInstances[sum]++;
}

So numInstances[7] would be the number of times 7 showed up when you rolled the dice NUM_ROLLS times. No if statements at all.

yup yup of course in that case, if you ever computed a sum of 12 (two 6's), numInstances[sum] would actually be out of bounds (no error though, so be careful). So...you would have to store it in array index sum-1 in this case. Also remember that numInstances[0] would actually never have any other value other than 0, since you can never roll a 1 with 2 die...

commented: Good catch! :) +5

yup yup of course in that case, if you ever computed a sum of 12 (two 6's), numInstances[sum] would actually be out of bounds (no error though, so be careful). So...you would have to store it in array index sum-1 in this case. Also remember that numInstances[0] would actually never have any other value other than 0, since you can never roll a 1 with 2 die...

Wow, what a rookie maneuver! Yes, of course you are correct. You need to define it up to 13. I was figuring indexes 0 and 1 just wouldn't be used, but I totally flaked out on the out of bounds error. Good catch. I'm so :$ .

>>numInstances[7] would be the number of times 7 showed up
shouldn't numInstances[6] be the number of times 7 was rolled? Like n1337 said, you should do:

numInstances[sum-1]++;

otherwise, you'd be wasting numInstances[0], and when you roll a 12, you'd have nowhere to put it (since the highest index of the array is 11)

Also - this method works in this situation, but just some pointers (not int* x; pointers):

when an if statement has only one line of code, you don't need curly brackets. So you can do this:

if(sum==1)
    ones+=1;

Also, you could use a switch statement:

switch(sum)
{
case 1:
    ones+=1;
    break;
case 2:
    twos+=1;
    break;
case 3:
   //...
}

>>numInstances[7] would be the number of times 7 showed up
shouldn't numInstances[6] be the number of times 7 was rolled? Like n1337 said, you should do:

numInstances[sum-1]++;

otherwise, you'd be wasting numInstances[0], and when you roll a 12, you'd have nowhere to put it (since the highest index of the array is 11)

Also - this method works in this situation, but just some pointers (not int* x; pointers):

when an if statement has only one line of code, you don't need curly brackets. So you can do this:

if(sum==1)
    ones+=1;

Also, you could use a switch statement:

switch(sum)
{
case 1:
    ones+=1;
    break;
case 2:
    twos+=1;
    break;
case 3:
   //...
}

Yes, I realize you don't need the brackets if the statement is one line. I had written two lines originally, then erased one. It probably looks better to erase the brackets when there is only one line.

Regarding the fact that you are wasting the 0 index, yes you are, but you get a slight increase in performance because you aren't subtracting 1 each time (in this case, you could actually subtract 2 since 0 and 1 aren't used). To me, it's worth setting aside two extra bytes for that slight performance boost. Of course I totally spaced on the out-of-bounds error.

Regarding the switch-case comment, it's certainly better than a whole bunch of if statements, but I think this problem is perfect for an array solution using sum as the index. Not only do you cut down on the lines of code here, but if you use variables twos, threes, fours, etc., you are consistently typing those in. Your potential for loops goes way down and you have to pass eleven parameters to any function that wants to use them.

DisplayResults(twos, threes, fours, fives, sixes, sevens, eights, nines, tens, elevens, twelves);

as opposed to

DisplayResults(numInstances);

@VernonDozier
I was talking to the OP about the curly bracket thing and everything following that, not you (sorry if that was unclear).

As I said, "this method works in this situation". I was just giving him some tips for condensing if-else chains in other, hypothetical situations where an array wouldn't solve his problem.

Also, I didn't see your second post while I was posting, so sorry for bringing up the out-of-bounds thing again.

@VernonDozier
I was talking to the OP about the curly bracket thing and everything following that, not you (sorry if that was unclear).

As I said, "this method works in this situation". I was just giving him some tips for condensing if-else chains in other, hypothetical situations where an array wouldn't solve his problem.

Also, I didn't see your second post while I was posting, so sorry for bringing up the out-of-bounds thing again.

No worries. Actually, looking back at my original post, it is clear you weren't talking about me regarding the brackets. I got confused with some posts I made in another thread where I had one line inside brackets.

Be a part of the DaniWeb community

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