6.4. Array Algorithms (FRQs)¶
In this lesson, you will study different Free Response Questions and responses that develop algorithms using arrays.
Here are some common algorithms that you should be familiar with for the AP CSA exam:
Determine the minimum or maximum value in an array
Compute a sum, average, or mode of array elements
Search for a particular element in the array
Determine if at least one element has a particular property
Determine if all elements have a particular property
Access all consecutive pairs of elements
Determine the presence or absence of duplicate elements
Determine the number of elements meeting specific criteria
Shift or rotate elements left or right
Reverse the order of the elements
Here are two common array traversal loops that can be used for these algorithms:
for (int value : array)
{
if (value ....)
...
}
for(int i=0; i < array.length; i++)
{
if (array[i] ....)
...
}
The code below finds the minimum (smallest element) in an array. Try it in the Java visualizer with the CodeLens button. Can you change it to find the maximum element instead? Can you also compute the average of the elements?
Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
The code below rotates array elements to the left. Note that you need to use an indexed loop for this because you need to change the array and access two elements at different indices. Try it in the Java visualizer with the CodeLens button. Can you change it to rotate the elements to the right instead? Hint: use a backwards loop.
We encourage you to work in pairs or groups to tackle the following challenging FRQ problems and take them one step at a time. These will get easier with practice!