Unit 2.9: Developing Algorithms Using Selection and Iteration
Developing algorithms is a core skill in computer science. By combining selection (if statements) and iteration (loops), you can solve a wide variety of complex problems.
Standard Algorithms
The AP CSA framework identifies several "standard" algorithms that every student should be able to implement.
- Standard algorithms are reusable patterns for solving common data problems. Mastering these patterns allows you to build complex logic by combining simpler, well-understood steps.
These are the building blocks for more advanced programming.
1. Divisibility and Frequency
This algorithm uses the remainder operator (%) to check if a number is divisible by another and uses a counter variable to track how often a condition is met.
Task: Counting frequencies using the remainder operator.
public class DivisibilityCount {
public static void main(String[] args) {
int count = 0;
// Goal: Count how many numbers between 1 and 100 are divisible by 7
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) {
count++; // Increment count when the condition is true
}
}
System.out.println("Frequency of numbers divisible by 7: " + count);
}
}
2. Digit Processing
To process individual digits of an integer, we use % 10 to peel off the last digit and / 10 to remove it. This typically requires a while loop because we continue until the number becomes 0.
Task: Extracting and processing individual digits of a number.
public class DigitProcessor {
public static void main(String[] args) {
int number = 12345;
while (number > 0) {
int digit = number % 10; // 1. Isolate the rightmost digit
System.out.println("Processing digit: " + digit);
number /= 10; // 2. Remove the processed digit from the number
}
}
}
3. Finding a Minimum or Maximum
To find the largest or smallest value in a sequence, initialize a variable to a starting value (like the first input or Integer.MIN_VALUE/MAX_VALUE) and update it whenever a "better" value is found.
Task: Finding the maximum value in a sequence of user inputs.
import java.util.Scanner;
public class MaxFinder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Initialize max to the smallest possible integer
int max = Integer.MIN_VALUE;
for (int i = 0; i < 5; i++) {
System.out.print("Enter a number: ");
int num = input.nextInt();
if (num > max) {
max = num; // Found a new maximum! Update the variable.
}
}
System.out.println("The largest number entered was: " + max);
}
}
4. Sum and Average
This algorithm uses an accumulator variable to keep a running total. To find the average, divide the total sum by the number of items, ensuring you use type casting to avoid integer division.
Task: Calculating the sum and average of multiple values.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
int count = 5;
for (int i = 0; i < count; i++) {
System.out.print("Enter a value: ");
sum += input.nextInt(); // Accumulate the sum
}
// Use (double) to ensure decimal precision in the result
double average = (double) sum / count;
System.out.println("Total Sum: " + sum);
System.out.println("Calculated Average: " + average);
}
}