Solved

# Given "n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations

Posted on 2015-02-17
118 Views
Hi,
Write a Java program to print all possible combinations in below case
"n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations
for example.
2 dices with 3 sides should print out this
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
I gave it a sincere try before posting this. My code doesn't work if number of dices is more than 2 which is not right. Thanks in advance for looking.
0
Question by:koppcha
• 4
• 2
• 2
• +3

LVL 35

Expert Comment

Also, I assume that this is for a homework/assignment exercise, but can you please confirm if that is the case? This allows everyone to provide the correct level of assistance.
0

LVL 8

Author Comment

This is definitely not homework/assignment.  It's been long time since I have been to school(which could be the reason I spent half day figuring this out )

``````import java.util.Arrays;

public class DiceHistogram {
public void getHistogram(int dices, int sides) {
int diceArray[] = new int[dices];

//Initialize Array
for (int i = 0; i< diceArray.length; i++) {
diceArray[i] = 1;
}

//System.out.println(Arrays.toString(diceArray));
for(int k=dices-2; k>=0; k--) {
for(int j=1; j<= sides; j++) {
diceArray[k] = j;
for(int i=1; i<=sides; i++ ) {
diceArray[dices-1] = i;
System.out.println(Arrays.toString(diceArray));

}

}
}

}

//for ( int j)

public static void main(String[] args) {
DiceHistogram dh = new DiceHistogram();
dh.getHistogram(2, 3);
}

}
``````
0

LVL 32

Expert Comment

Often, when the term "combinations" is used in an exercise, the order does not matter, so (2,1) is considered the same as (1,2).
0

LVL 8

Author Comment

In this case they should be considered different. "Combination" is my own way of explaining it not part of original problem. Example should be taken for reference. They are considered different.
0

LVL 84

Expert Comment

Can we assume that pow(m,n) fits in an int?
0

LVL 26

Accepted Solution

dpearson earned 300 total points
This is actually a fairly tricky problem.  You can't just use one or two or even 5 loops to do it, but you really need 1 loop for each dice - which is a parameter to the problem.

That sort of problem is usually easiest to solve with recursion - where a function calls to itself.

Something like this.

Recursive functions tend to be not a lot of code, but can be hard to understand if you've not seen them before.  I added some comments to help explain what's going on.

Doug

``````	public static List<String> dice(int nDice, int nSides) {
List<String> combinations = new ArrayList<>();

if (nDice == 1) {
// For one dice we get a list with just 1 entry
// 1
// 2
// 3
for (int side = 1; side <= nSides; side++) {
}
} else {
// When we have more than one dice we add the values for this dice
// to all possible combinations from "nDice-1"
// So add "1," to all subsequent combinations (e.g. "1,1", "1,2", "2,1", "2,2" etc.)
List<String> following = dice(nDice-1, nSides) ;
for (int side = 1; side <= nSides; side++) {
for (String follow : following) {
combinations.add(side + ", " + follow);
}
}
}

return combinations ;
}

public static void listCombinations() {
List<String> combinations = dice(4, 3) ;

for (String combo : combinations) {
System.out.println("(" + combo + ")") ;
}
}
``````
0

LVL 8

Author Comment

Hi Dpearson,
I tried your program with different inputs and it works as expected. I am still trying to understand. will get back to you. Thanks for your help.

Hi Ozo,
Yes I think it's fair assumption.
0

LVL 84

Assisted Solution

ozo earned 100 total points
if pow(sides,dices) fits in an int, another method might be
static void getHistogram(int dices, int sides) {
for( int i=(int)Math.pow(sides,dices);--i>=0;){
System.out.print("(");
int n=i;
for( int d=0;d<dices;++d ){
if( d>0 ){ System.out.print(","); }
System.out.print(sides-(n%sides));
n/=sides;
}
System.out.println(")");
}
}
0

LVL 27

Assisted Solution

rrz earned 100 total points
http://www.mathsisfun.com/combinatorics/combinations-permutations.html
Here is my version.  What you call a side, I call a face.
``````import java.util.*;
public class Dice{
public static void main(String[] args) {
int numberOfDice = 3;
int numberOfFaces = 3;
int numberOfOutcomes = (int)Math.pow(numberOfFaces,numberOfDice);
int[] roll = new int[numberOfOutcomes]; //all initially 0 (roll to the next face)
StringBuilder[] outcomes = new StringBuilder[numberOfOutcomes];
for(int j=0; j<numberOfOutcomes; j++){
outcomes[j] = new StringBuilder("(");
}
for(int k = 1; k <= numberOfDice; k++){
int numberOnFace = 1;
for(int y = 0; y < numberOfOutcomes; y++){
if(roll[y] == 0){
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
numberOnFace++;
roll[y] = 1; // don't roll, just stay on the same face
if(numberOnFace == numberOfFaces + 1){
numberOnFace = 1;
roll[y] = 0; // roll to the next face
}
}else{
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
}
}
}
for(int z = 0; z < numberOfOutcomes; z++){
System.out.println(outcomes[z].toString() + ")");
}
}
}
``````
0

LVL 26

Expert Comment

I am still trying to understand. will get back to you. Thanks for your help.

The key for a recursive solution is to understand that the larger problem can be seen as a repetition of a smaller problem.

E.g. If I gave you a list of all combinations of 2 dice with 3 sides:

(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)

then you can use this list to create the list of all combinations of 3 dice by adding "1, ...", "2, ..." and "3, ..." to the front of each of the earlier combinations:

1 +  (1,1) => (1,1,1)
1+  (1,2) => (1,1,2)
1 + (1,3) => (1,1,3)
1 + (2,1) => (1,2,1)
...
3 + (1,1) => (3,1,1)
3 + (1,2) => (3,1,2)
...
etc..

Once you recognize the problem has that property, then you can write a function that calls to itself to get it to compute this.

Anyway - hope you can figure it out :)

Doug
0

LVL 8

Author Comment

Hi Doug,
I got it now. Thanks for the help.

Ozo/rrz,
Thanks guys. Your code works too.

Doug's solution is easier for me to understand.
I am going to close this question now.
0

## Featured Post

### Suggested Solutions

hash value 2 38
how to use external config file with Spring MVC 4 58
changeXy challenge 13 56
network + 7 72
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…