4.05 Implementing Array Algorithms
Implement array algorithms to calculate totals, find extremes, count matches, and transform stored values.
- Implementing Array Algorithms
- 1. Reference Guide
- Key Topics
- 3. College Board Requirements
- 4. Lesson Plan
- Presentation & Slide Deck
Implementing Array Algorithms
Unit 4: Data Collections
This notebook serves as a complete lesson plan, presentation guide, live coding scratchpad, and homework lab for Topic 4.5.
1. Reference Guide
Key Topics
| Term | Definition | Example / Context |
|---|---|---|
| Accumulator | A variable declared before a loop used to build up a result, such as a sum or count. | int sum = 0; before looping to add arr[i] to sum. |
| Min / Max Algorithm | An algorithm that assumes the first element is the extreme value, then updates if a more extreme value is found. | int max = arr[0]; rather than starting at 0 to handle negative values. |
| Flag Variable | A boolean variable used to record whether a certain condition was met during the traversal. | boolean hasZero = false; updated to true if arr[i] == 0 is found. |
| Shifting Elements | Moving all elements in an array one index left or right, often requiring a temporary variable. | Moving arr[i+1] to arr[i] inside a loop. |
- Contextual Rule: Standard array algorithms are the building blocks of most data processing tasks. Always initialize search variables correctly before the loop, and return the final result after the loop finishes executing.
3. College Board Requirements
- AP CSA Unit 4, Topic 4.5: Standard Array Algorithms.
- There are standard algorithms that utilize array traversals to: determine a minimum or maximum value, compute a sum, count elements, and determine if at least one element meets criteria.
- There are standard algorithms for shifting or rotating elements left or right.
4. Lesson Plan
Learning Objective: Implement standard array algorithms to process collections of data, including finding extreme values, summing, and counting.
Success Criteria:
You can write a loop to traverse an array and correctly apply accumulator, flag, and
min/max logic to process the data without throwing an
ArrayIndexOutOfBoundsException.
Presentation & Slide Deck
-
Slide 1: Welcome & Warm-Up
- Title: Standard Array Algorithms: The Toolkit
- Warm-Up Prompt: "If I ask you to find the tallest student in this room, what are the exact steps your brain takes to figure it out?"
- Teacher Script: "Your brain automatically scans the room, picks a starting person, and then compares everyone else to that first person. If someone is taller, they become the new 'tallest'. Today, we are going to teach Java how to do exactly what your brain just did."
```java
// CODE_RUNNER: Run it, then change one score and predict the new output
// Buggy Implementation - Live Demo
int[] arr = {10, 20, 30, 40};
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i + 1]; // ArrayIndexOutOfBoundsException when i = arr.length - 1!
}
```
### Slide 3: Corrected Implementation
```java
// CODE_RUNNER: Run it, then change one score and predict the new output
// Corrected Shift Left Implementation
int[] arr = {10, 20, 30, 40};
int temp = arr[0]; // Save the first element
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1]; // Shift remaining elements left
}
arr[arr.length - 1] = temp; // Place first element at the end
// Print result: [20, 30, 40, 10]
```
---
## 📄 Student Homework Assignment Handout
**Title:** Temperature Data Processor Lab
**Instructions:**
Complete the static methods in the `TemperatureAnalyzer` class below using standard 1D array traversal algorithms.
```java
// CODE_RUNNER: Run it, then change one score and predict the new output
public class TemperatureAnalyzer {
/**
* Counts how many times two consecutive days both had
* temperatures strictly greater than 90 degrees.
*/
public static int countHeatWaves(int[] temps) {
int count = 0;
// TODO: Implement using a loop bound of temps.length - 1
for (int i = 0; i < temps.length - 1; i++) {
if (temps[i] > 90 && temps[i + 1] > 90) {
count++;
}
}
return count;
}
/**
* Reverses the elements of the temps array in-place
* using a while loop and a swap mechanism.
*/
public static void reverseLog(int[] temps) {
int left = 0;
int right = temps.length - 1;
while (left < right) {
int temp = temps[left];
temps[left] = temps[right];
temps[right] = temp;
left++;
right--;
}
}
}
```
[All Java lessons](/OCS-Kit-Pages/lessons/java/)