AP CSA FRQ Guide
2022 FRQ 4: Data
Write two methods for a 2D integer grid. The first fills every grid cell with a random valid value that is divisible by 10 but not by 100. The second counts how many columns are in nondecreasing order from top to bottom.
2D Arrays and Random Generation22 minute target2D arraysMath.randomnested loopsmodulocolumn traversal
Skills Tested
What this FRQ is really practicing
2D arraysMath.randomnested loopsmodulocolumn traversal
Review these topics before attempting the problem.
Starter Approach
How to begin with your own plan
- 1Part A: traverse every row and column of grid.
- 2Generate random values between 1 and MAX inclusive until the value is divisible by 10 and not divisible by 100.
- 3Assign a valid random value to every grid element.
- 4Part B: examine each column from top to bottom.
- 5Count a column when every value after the first is greater than or equal to the value above it.
Attempt the problem before opening the solution.
Common Mistakes
Mistakes to watch for while writing
Generating only multiples of 10 by multiplying a random integer by 10 but accidentally allowing values above MAX.
Forgetting to reject multiples of 100.
Checking rows for increasing order instead of columns.
Using strictly greater than when equal adjacent values are allowed.
Resetting the column-valid flag in the wrong loop.
Self Check
Review questions before you submit
Defines repopulate with void return type and no parameters.
Part A modifies the grid directly.
Uses nested loops to visit every grid cell.
Loop over grid.length and grid[0].length.
Uses Math.random to generate candidate values.
Generate a random integer in the range 1 through MAX.
Generates values from 1 through MAX inclusive.
A common pattern is (int)(Math.random() * MAX) + 1.
Requires generated values to be divisible by 10.
Use modulo 10.
Rejects generated values divisible by 100.
Use modulo 100 and reject zero remainders.
Assigns a valid value into each grid cell.
Each grid[row][col] should receive the generated valid value.
Defines countIncreasingCols with int return type and no parameters.
Part B returns a count of columns.
Practice Links
