2

Thank you for taking the time to read,

I have a 2d matrix of booleans and im trying to change each of the border values to false. I have this working for the top and left-most borders but cant seem to figure out a working code for the bottom and right-most, any help would be appreciated, these are the codes for the upper and left-most:

// Changes border values to false

    for (int row = 0; row <matrix.length; row++) {
        int col = 0;
        matrix[row][col] = false;
    }

    for (int col = 0; col <matrix.length; col++) {
        int row = 0;
        matrix[row][col] = false;
    }
1
  • Square matrix? Homework? Commented Sep 6, 2011 at 2:13

3 Answers 3

5

Make the row and col values the last values instead of zero;

for the last column:

for (int row = 0; row <matrix.length; row++) {
    int col = matrix[0].length-1;
    matrix[row][col] = false;
}

and last row:

for (int col = 0; col <matrix[0].length; col++) { 
    int row = matrix.length-1;
    matrix[row][col] = false;
}

Or better aproach to avoid creating new instances of cols and rows inside the loops;

for (int row = 0; row <matrix.length; row++) {
    matrix[row][matrix[0].length-1] = false;
}
for (int col = 0; col <matrix[0].length; col++) { 
    matrix[row][matrix.length-1] = false;
}

For col, the last value must be the length of a row, so I write matrix[0].length it might be any other suitable number too instead of 0, but out of bounds.

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

2 Comments

Avoid useless col and row instanciations inside the loops. Do it once outside.
You are right, it was just to make him understand with his own code.
0

Well, the easiest way I can think of is to just set row to the max value of rows through the loop (looks to be matrix.length - 1 each time) as well. So, for example:

for (int row = 0; row < matrix.length; row++) {
    matrix[row][0] = false;
    matrix[row][matrix.length - 1] = false;
}

and do the same for your columns.

4 Comments

Thank you for the help, I very much appreciate such a quick reply
No problem, though @Yasin beat me to it. I think mine is a more efficient way of doing it (since you are doing both at the same time, and not create extra variables) but both are correct.
@Justin, you should choose an answer at least :)
oop sorry, first time ever using this site
0

Or something like that;

 for(int c=0;c<matrix.length;c++){
              System.out.println("");
              for(int r=0;r<matrix[0].length;r++){
                if(c==0||r==0||c==matrix.length-1||r==matrix[0].length-1){
                    matrix[c][r] = false;
                }else{
                    matrix[c][r] = true;
                }
                System.out.print(" " + matrix[c][r]);
              }
          }

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.