0

so my problem is that i cannot get the elements of a 2d array to switch like i would be able to do in a single variable array. Instead of switching the elements they are just being continuously rewritten...

for (int column = 0; column < m[0].length; column++) {
    shufcol = (int)(Math.random()*4);
    temp = column;
    System.out.println(shufcol);

    for(int row = 0; row < m.length; row++) {
        temp = row;
        m[row][temp]=m[row][column];
        m[row][column]= m[row][shufcol];
        m[row][temp] = m[row][shufcol];
    }
}

input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}

output array {{2 2 3 2} {5 5 6 5} {8 8 9 8}}

If your curious about the math.random, that is just to generate a random column between 0 to 3 to switch with. Again the issue is why is it only rewriting elements and not switching them?

2
  • 3
    What do you mean by switching the elements? And...rewritten? Commented Mar 27, 2011 at 19:26
  • So what would be a desired output? ^^ Commented Dec 9, 2016 at 1:51

3 Answers 3

1

I don't fully understand what you want to achieve at the end (because you haven't told), but I think, if you reread this piece of code:

temp = row;
m[row][temp]=m[row][column];
m[row][column]= m[row][shufcol];
m[row][temp] = m[row][shufcol];

several times and try to execute it with piece of paper and pen you'll find the mistake.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understood, this will switch the values in column and shufcol for all rows (I didn't test it):

for (int column = 0; column < m[0].length; column++) {
    shufcol = (int)(Math.random()*4);
    System.out.println(shufcol);

    for(int row = 0; row < m.length; row++) {
        temp = m[row][shufcol];
        m[row][shufcol] = m[row][column];
        m[row][column] = temp;
    }
}

Comments

0

This will switch elements inside their rows:

    //input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
    int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};


    for (int column = 0; column < m[0].length; column++) {
        int shufcol = (int)(Math.random()*4);
        int shufrow = (int)(Math.random()*3); //need row to switch with, too
        for(int row = 0; row < m.length; row++) {
            if(row == shufrow){
                int tempField =m[row][column];
                m[row][column]= m[row][shufcol];
                m[row][shufcol] = tempField;
                System.out.println("In row " + row + " : column " + column + " was switched with column " + shufcol + "!");
                break; //just switch once per row, easier debugging ^^-d
            }
        }
    }

    //print output array
    for(int row = 0; row < m.length; row++) {
        String line = "\n";
        for (int column = 0; column < m[0].length; column++) {
            line += m[row][column] + " ";
        }
        System.out.print(line);
    }

Output:

In row 2 : column 0 was switched with column 2!
In row 0 : column 1 was switched with column 2!
In row 1 : column 2 was switched with column 3!
In row 0 : column 3 was switched with column 2!

0 2 3 1 
1 4 6 5 
8 7 0 9 

Since i am not totally sure what you want to do, here is switching elements in the whole array randomly:

    //input array (3X4) {{0 1 2 3} {1 4 5 6} {0 7 8 9}}
    int[][] m = {{0, 1, 2, 3}, {1, 4, 5, 6}, {0, 7, 8, 9}};

    for (int column = 0; column < m[0].length; column++) {
        int shufcol = (int)(Math.random()*4);
        int shufrow = (int)(Math.random()*3); //need row to switch with, too
        //there is no point in duplicating the count variable of a loop!
        System.out.println(shufcol);

        for(int row = 0; row < m.length; row++) {
            //check that random field is not current field!
            if(row != shufrow && column != shufcol){
                //switch current with random
                int temp = m[row][column]; // le backuppe
                m[row][column] = m[shufrow][shufcol]; // write le new value of "random source field" to current field
                m[shufrow][shufcol] = temp; // write value of current field to "random source field"
                System.out.println("switched [" + row + "][" + column + "] with [" + shufrow + "]["+ shufcol + "]");
                break; // use break to switch only once per row, easier debugging ^^-d
            }
            else {
                System.out.println("will not switch with self!");
            }

        }
    }

    //print output array
    for(int row = 0; row < m.length; row++) {
        String line = "\n";
        for (int column = 0; column < m[0].length; column++) {
            line += m[row][column] + " ";
        }
        System.out.print(line);
    }

Yielding the following output:

2
switched [0][0] with [2][2]
0
switched [0][1] with [1][0]
0
switched [0][2] with [2][0]
2
switched [0][3] with [1][2]

8 1 0 5 
1 4 3 6 
2 7 0 9 

Hope this helps! ^^-d

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.