Successors
is #4 from the from the 2017 AP Computer Science A Free Response problems.
https://secure-media.collegeboard.org/ap/pdf/ap-computer-science-a-frq-2017.pdf
Part (a) findPosition
method
public static Position findPosition(int num, int[][] intArr)
{
for(int r = 0; r < intArr.length; r++)
for(int c = 0; c < intArr[0].length; c++)
if(intArr[r][c] == num)
return new Position(r, c);
return null;
}
Part (b) getSuccessorArray
method
public static Position[][] getSuccessorArray(int[][] intArr)
{
Position[][] successors = new Position[intArr.length][intArr[0].length];
for(int r = 0; r < successors.length; r++)
for(int c = 0; c < successors[0].length; c++)
successors[r][c] = findPosition(intArr[r][c] + 1, intArr);
return successors;
}
2017 AP CS Exam Free Response Solutions
Additional 2D array resources
Help & comments
Get help from AP CS Tutor Brandon Horn