15,664,272 members
See more:
Hi all, I am trying to write a function of 5 dice rolls where it should count the number of times whether 3 same numbers appear eg. (5,5,3,1,5)

Before that, I already have a function for pairs which works. But the following function which I wrote for threeSameNo, it seems to work for pairs too (4,2,4,1,2) or sometimes, it does works where there is indeed 3 same numbers, and times it did not work when re-running the program even if there are 3 same numbers.

Am I doing wrong somewhere?

FYI, getValue is a function I wrote for getting the value of each dice that is stores within an array.

What I have tried:

C++
```void DiceRoll::threeSameNo()
{
int counter = 0;

for(int i = 0 ; i < 5 ; i++)
{
for(int j = i + 1 ; j < 5 ; j++)
{
for(int k = j + 1 ; k < 5 ; k++)
{
if (dArray[i].getValue() == dArray[j].getValue() == dArray[k].getValue())
{
counter++;
cout << "Three of A Kind appear " << endl;
}
}
}
}

}```
Posted
Updated 3-Apr-16 21:23pm

## Solution 1

Simple: stop looking at the numbers!
Copy the data into a second array.
Sort it.
Look through, comparing each number with the previous two...
Sorting is built in and pretty efficient, and after that it's a single pass check to find the repeats.

Alternatively, since you only have 6 numbers to work from: set up a second array of six integers - call it the counts array. Zero the new array. Loop through the dice rolls array, and increment the appropriate index in the counts array for each roll value.
A quick pass through the counts array afterwards tells you how many times each value has been rolled.

v2

## Solution 2

Quote:
if (dArray[i].getValue() == dArray[j].getValue() == dArray[k].getValue())

The above line is flawed (because `==` either evaluates to `0` or `1`).
It should be
C++
`(dArray[i].getValue() == dArray[j].getValue() && dArray[j].getValue() == dArray[k].getValue())`

Try, for instance
C++
```cout << (5 == 5) << endl;

cout << (5 == 5 == 5) << endl;```