Data
is #4 from the from the 2022 AP Computer Science A Free Response problems.
https://apcentral.collegeboard.org/pdf/ap22-frq-computer-science-a.pdf?course=ap-computer-science-a
Part (a) repopulate
method
public void repopulate()
{
for(int r = 0; r < grid.length; r++)
{
for(int c = 0; c < grid[0].length; c++)
{
int rand = (int) (Math.random() * MAX) + 1;
while(rand % 10 != 0 || rand % 100 == 0)
rand = (int) (Math.random() * MAX) + 1;
grid[r][c] = rand;
}
}
}
For more on working with Math.random()
see Generating numbers with Math.random().
Part (b) countIncreasingCols
method
public int countIncreasingCols()
{
int increasingCols = 0;
for(int c = 0; c < grid[0].length; c++)
{
boolean isIncreasing = true;
for(int r = 1; r < grid.length; r++)
if(grid[r-1][c] > grid[r][c])
isIncreasing = false;
if(isIncreasing)
increasingCols++;
}
return increasingCols;
}
2022 AP CS Exam Free Response Solutions
Additional 2D array resources
- Intro to 2D arrays
- 2D array exercises
- Mazer FR
- Droppy FR
- Flight FR
- MatrixManipulator exercise
- DeterminantFinder exercise
Help & comments
Get help from AP CS Tutor Brandon Horn