Hello everyone

I have a task of changing values in an int[][], for example if I input 1 it is changed to 0 and vice versa. So far I tried the following for a hundred times amending the xs and ys but with no happy ending.

public void reverseOrder(int x , int y) {
        if (position[x][y] == 1) {
            position[y][x] = 0;
        } else if (position[x][y] == 0) {
            position[y][x] = 1;
        }        
    }

Some advice is much appreciated.

Johan

can you extend on the desired behavior?

because i can see what your method does, and the code seems good, but it does not look like it does what you would want it to do correct?

you say

if I input 1 it is changed to 0 and vice versa

, but right now the 1 and 0s are hardcoded and all you seem to input is the x and y.

we also don't know what's in position, so it's a bit hard to test ourselves.
do you have to do this using a method, btw? I think two nested for loops would be easier for a task like this.

Hi Philippe and Stultuske

Thanks. I had to print for example a box. Something like this

1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

and then invert the bits like 0 to 1 and vice versa. I found the solution to this problem so thanks anyways. Had to adjust the printing method as well.

Regards


Johan

if you are done please share your answers and set the thread as solved,
otherwise congratulations,

but the code you provided in your first post was far from doing what you just described, are you sure it wasnt just a lucky test, have you tested extensively?

i'm saying this because you were checking [x][y] and changing [y][x]...

Hi Philip,

It goes something like this:

public void reverseOrder(int x, int y) {
        if (position[x][y] == 1) {
            position[x][y] = 0;
        } else if (position[x][y] == 0) {
            position[x][y] = 1;
        }
    }

There s also a method to print the point position.

Thanks and regards

Johan

Edited 3 Years Ago by Dani: Formatting fixed

you could also have done it with a single line :

public void reverseOrder(int x, int y) {
    position[x][y] = ( position[x][y] == 0 ? 1  : 0 );
}

or if you had simply had an array of bool instead and modified the display method to display 0 for false and 1 for true :

public void reverseOrder(int x, int y) {
    position[x][y] = !position[x][y];
}

Edited 4 Years Ago by Philippe.Lahaie: n/a

This question has already been answered. Start a new discussion instead.