The Skyview
problem from the 2013 AP Computer Science Exam is typical of free response problems that test 2 dimensional arrays.
SkyView
is #4 from the from the 2013 AP Computer Science A Free Response problems.
https://secure-media.collegeboard.org/digitalServices/pdf/ap/apcentral/ap13_frq_comp_sci.pdf
Part (a) SkyView
constructor
public SkyView(int numRows, int numCols, double[] scanned)
{
view = new double[numRows][numCols];
int scannedIndex = 0;
for(int row = 0; row < numRows; row++)
{
for(int col = 0; col < numCols; col++)
{
if(row % 2 == 0)
view[row][col] = scanned[scannedIndex];
else
view[row][numCols - 1 - col] = scanned[scannedIndex];
scannedIndex++;
}
}
}
Transferring elements from one array to another requires traversing both structures. SkyView
traverses the 1D array (scanned
) in conventional order from front to back. SkyView
uses conventional nested loops to traverse the 2D array (view
) as well.
Within each row of the 2D array, the position of the next element is determined based on whether row
is even or odd. If row
is even, the row is traversed in conventional order. If row
is odd, the loop proceeds in conventional order but the elements are placed into the row from right to left.
The expression numCols - 1 - col
evaluates to the index col
spots from the end of the row. numCols - 1
is the index of the last element in the row. (The AP CS A Exam has long featured this as numCols - col - 1
, which is mathematically equivalent.)
See 2D array exercises for additional traversals and other challenges.
Part (a) SkyView constructor (alternate solution)
public SkyView(int numRows, int numCols, double[] scanned)
{
view = new double[numRows][numCols];
int scannedIndex = 0;
for(int row = 0; row < numRows; row++)
{
if(row % 2 == 0)
{
for(int col = 0; col < numCols; col++)
{
view[row][col] = scanned[scannedIndex];
scannedIndex++;
}
}
else
{
for(int col = numCols - 1; col >= 0; col--)
{
view[row][col] = scanned[scannedIndex];
scannedIndex++;
}
}
}
}
It is possible to reverse the order of the loop that traverses each row (the loop with col
as the control variable). This is an alternative to calculating the position within the inner loop.
Part (b) getAverage
method
public double getAverage(int startRow, int endRow, int startCol, int endCol)
{
double sum = 0;
int count = 0;
for(int row = startRow; row <= endRow; row++)
{
for(int col = startCol; col <= endCol; col++)
{
sum += view[row][col];
count++;
}
}
return sum / count;
}
Conventional nested loops are used to traverse the 2D array. The bounds of each loop are the starting and ending indexes specified by the parameters.
As an alternative to maintaining count
it is possible to compute the number of elements as (endRow - startRow + 1) * (endCol - startCol + 1)
.
2013 AP CS Exam Free Response Solutions
- MusicDownloads Free Response Solution
- TokenPass Free Response Solution
- GridWorldUtilities & JumpingCritter Free Response Solution
Additional 2D array resources
Help & comments
Get help from AP CS Tutor Brandon Horn