1
votes

I was wondering if someone could help me come to a conculiton with this problem...

"Print all ordered 3 tuples (i,j,k) i in 0..(n-1), j in 0 .. (n-1), k in 0..(n-1), in lexicographical order, one 3 tuple per line. Lexicographical order (think about words in a dictionary), orders sequences: the first UNEQUAL element in the two sequences determines the order, e.g.: (0,1,5,3) < (0,1,6,0).

Format: open-paren number comma number comma number close-paren. Use the print3Tuple() method provided to print, so that we all print in the same format. (Think about this: how would you have to change your code to print 4-tuples, 5-tuples, k-tuples?) Example: for n=2, your output should be"

so i have two methods and i'm stuck on my loops...

public void print3Tuple(int a, int b, int c) {
    System.out.println("(" +a+ "," +b+ "," +c+ ")" );
}

and then the one i'm working in

public void print3Tuples(int n) {
    // Replace this body with your solution
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = j; k < n; k++) {
                print3Tuple(i, j, k);
            }
        }
    }

which prints this (0,0,0) (0,0,1) (0,1,1) (1,1,1)

and i need this

(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1)

i'm stuck any help would be appreciated.

1
Please tell us the details of your problem. How exactly are you "stuck"? - Hovercraft Full Of Eels
I don't know whats going wrong with my loops or how i should fix them to have them print as such.. (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1) - user3754616
@HovercraftFullOfEels - user3754616

1 Answers

3
votes

Change the initial value of the loop variable j and k to 0:

public static void print3Tuples(int n) {
    // Replace this body with your solution
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                print3Tuple(i, j, k);
            }
        }
    }
}