0
public static boolean ContainedArr(boolean[][] photo, boolean[][] sub) {    //find if sub is sub array in photo 
    if (sub.length>photo.length ||sub[0].length>photo[0].length) {  //sub bigger than photo ->return false
        return false;
    }
    for (int i=0;i<photo.length-sub.length+1;i++) { //i runs on rows of photo
        for (int j=0;j<photo[0].length-sub[0].length+1;j++) {   //j runs in cols of photo
            if (photo[i][j]==sub[0][0]) {
                boolean flag =true;
                int row=0,col=1;        //of sub
                while (flag) {
                    if (row==sub.length-1&&col==sub[0].length-1)    
                        return true;
                    if (sub[row][col]==photo[i+row][j+col]) {
                        if (col==sub[0].length-1) {         //last col
                            col =0;
                            row++;
                        }
                        else {
                            col++;
                        }
                    }
                    else
                        flag =false;
                }
            }
        }
    }
    return false;
}

i just didnt understand the logic of the structure and the iteration process of this function starts from : if (row==sub.length-1&&col==sub[0].length-1 to the end.

3
  • This part tests if sub is a subpicture of photo at position i,j. Commented Apr 29, 2024 at 11:29
  • @Jean-BaptisteYunès, yes i realized that. butwhat i didnt figure is what this if statement of the while loop are testing,and in general the whole code since this part was kind a vague for me, especially in this sections: when the function getin to the while loop, the first if statement checks if the length of the inner sub are valid or its check it including the values of subarray. i struggle here because of the statment==sub.length, so in first take, intuition is to check the validity of the length, but after that comes the return true.. :/ the ture was not supposed to be after the if2? Commented Apr 30, 2024 at 8:00
  • That could have been written with two for loops, but it has been made of a single while loop manipulating the indexes by hand. That's all. Look at if (col==sub[0].length-1) { col =0; row++; } else { col++; } which increment row and col appropriately. Commented May 2, 2024 at 12:00

1 Answer 1

0

Roughly the computation of the while loop could have been written as a separate function:

boolean isInnerPicture(...) {
    for (row=0; row<sub.length; row++) {
        for (col=0; col<sub[0].length; col++) {
            if (sub[row][col]!=photo[i+row][j+col]) {
                return false;
            }
        }
    }
    return true;
}

then used this way:

for (i...) {
    for (j...) {
        if (isInnerPicture(...)) {
            return true;
    }
}
return false;
Sign up to request clarification or add additional context in comments.

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.