Topic: Calculating the Average
In this lesson, we will cover the average calculation problem, which is frequently presented in coding tests. We will explain the definition of the problem and the solution step-by-step, and ultimately implement it in C++ code.
Problem Definition
Write a program to calculate the average of all elements in a given integer array. The length of the array is N, and all elements in the array are integers. The average value should be rounded to two decimal places, with the integer and decimal parts separated by a ‘.’.
Input Format:
- The first line contains the length of the array N (1 ≤ N ≤ 1000).
- The second line contains N integers in the array (each integer is between -10,000 and 10,000), separated by spaces.
Output Format:
Output the average value rounded to two decimal places.
Example
Input:
5
10 20 30 40 50
Output:
30.00
Problem Solving Method
The procedure to solve this problem is as follows:
- Read N from the input to determine the length of the array.
- When reading the integer array, sum each element.
- Divide the sum by N to find the average.
- Format the output to two decimal places.
C++ Code Implementation
Now, let’s write C++ code that follows the above procedure:
#include
#include // For std::setprecision
#include
using namespace std;
int main() {
int N;
cout << "Enter the length of the array: ";
cin >> N; // Input the length of the array
vector arr(N); // Declare a vector of size N
int sum = 0;
cout << "Enter the elements of the array separated by spaces: ";
for (int i = 0; i < N; ++i) {
cin >> arr[i]; // Input array elements
sum += arr[i]; // Sum the elements
}
double average = static_cast(sum) / N; // Calculate the average
cout << "Average: " << fixed << setprecision(2) << average << endl; // Format to two decimal places
return 0;
}
Code Explanation
- #include <iostream>: This is a library for input and output in C++.
- #include <iomanip>: This is a header file necessary for specifying output formats. It is used here to set the number of decimal places.
- vector
arr(N): This declares an integer array of size N using C++’s dynamic array, vector. - sum: This variable stores the sum of the inputted array elements.
- cin >> arr[i]: This assigns the user-inputted value to each element of the array.
- average: This calculates the average by dividing the total sum by N. It also casts this sum to double to maintain decimal places.
- std::fixed & std::setprecision(2): This sets the formatting for outputting up to two decimal places.
Complexity Analysis
The time complexity of this problem is O(N). The computation time increases in proportion to the input size, as each element is visited only once to calculate the sum. The space complexity is also O(N), as it uses N spaces to store the input data.
Conclusion
In this lesson, we learned to calculate the average value and practiced basic input/output and vector usage in C++. It is important to systematically approach a problem by breaking it down into steps, and to anticipate and handle potential errors at each step, which is a good habit in coding tests. Continue to practice various problems to improve your algorithm skills!