Hello! In this tutorial, we will address an algorithm problem to calculate the average using Java. Calculating an average is one of the basic operations regardless of the programming language and is often a topic in coding tests. The complexity of the average calculation problem can vary depending on how input values are processed. Therefore, we will start learning step by step from the basics.
Problem: Calculate Average
Write a program to calculate the average of a given integer array. The length of the array must be between 1 and 100, and all elements of the array must be integers. Additionally, the average should be rounded to two decimal places when printed.
Input:
- Integer N (1 ≤ N ≤ 100): Length of the array
- Integer array A[0..N-1] (each element -1000 ≤ A[i] ≤ 1000): Each element of the array
Output:
- Print the average rounded to two decimal places.
Problem Solving Process
To solve the problem, we will proceed with the following steps:
- Receive the input and create the array.
- Add all the elements of the array.
- Calculate the average by dividing the total by the number of elements in the array.
- Print the average rounded to two decimal places.
Step 1: Receiving Input
The first step to solving the problem is to receive input from the user. In Java, we can use the Scanner
class to receive input. We need to read the length of the array and its elements in order.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int N = scanner.nextInt();
int[] A = new int[N];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
}
// Proceed to the next step.
}
}
Step 2: Add All Elements of the Array
In the second step, we sum all the elements of the array. To do this, declare a variable to store the total sum and initialize it to 0, then add each element one by one using a loop.
int sum = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
}
// Proceed to the next step.
Step 3: Calculate Average
Now that we have the total sum, it's time to calculate the average. The average can be calculated by dividing the total by the length of the array. Please declare a variable to store the average value.
double average = (double) sum / N; // Cast is needed due to integer division
Step 4: Print Average Rounded
In the final step, we need to print the average rounded to two decimal places. In Java, we can use the Math.round
method for rounding. After rounding, we can print it in a suitable format.
average = Math.round(average * 100.0) / 100.0; // Round to two decimal places
System.out.printf("Average: %.2f\n", average); // Print to two decimal places
}
}
Complete Code
When we combine all the steps above, the final program looks like this:
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Receive Input
System.out.print("Enter the length of the array: ");
int N = scanner.nextInt();
int[] A = new int[N];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
}
// Step 2: Add All Elements of the Array
int sum = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
}
// Step 3: Calculate Average
double average = (double) sum / N; // Cast is needed due to integer division
// Step 4: Print Average Rounded
average = Math.round(average * 100.0) / 100.0; // Round to two decimal places
System.out.printf("Average: %.2f\n", average); // Print to two decimal places
}
}
Conclusion
In this tutorial, we solved a simple algorithm problem to calculate the average using Java. Although calculating an average is a basic concept, it requires a deeper understanding through various variations of the problem. There are often cases where the range of input data or exception handling must be considered. In the future, we will also cover these additional elements.
To strengthen your basics for coding tests, I recommend practicing by solving various problems through repetition. Thank you!