Introduction to C++ Programming: Fun Explanation of Problem 1059 – Calculate Average Age
Understanding the Problem
This problem requires calculating the average age of students in a class, rounded to two decimal places.
Key Points:
· Input: The first line contains the number of students n, followed by n lines with each student’s age. · Output: The average age, rounded to two decimal places. · Range: 1 <= n <= 100, ages between 15-25 years.
Problem-Solving Approach
Metaphorical Explanation
Imagine you need to calculate the average height of a class:
1. First, know how many people are in the class. 2. Add up everyone’s heights. 3. Divide the total height by the number of people. 4. Round the result to centimeters (just like rounding to two decimal places).
C++ Code Implementation
#include <iostream>#include <iomanip> // For controlling output formatusing namespace std;
int main() { int n; // Number of students cin >> n; // Read the number of students int sum = 0; // Total age int age; // Each student’s age // Loop to read each student’s age and accumulate for (int i = 0; i < n; i++) { cin >> age; sum += age; // Accumulate age } // Calculate average age and output, rounded to two decimal places double average = (double)sum / n; cout << fixed << setprecision(2) << average << endl; return 0;}
Line-by-Line Detailed Explanation
Lines 1-2: Prepare Tools
#include <iostream>#include <iomanip>using namespace std;
Explanation:
· #include <iomanip>: Includes the header file for controlling output format. · This allows us to use fixed and setprecision to control the number of decimal places.
Lines 3-5: Read Basic Information
int main() { int n; // Number of students cin >> n; // Read the number of students
Metaphor: Just like a teacher counting how many students are in the class.
Lines 6-7: Prepare Accumulator
int sum = 0; // Total ageint age; // Each student's age
Metaphor:
· sum = 0: Prepare an empty “age collection box”, starting at 0. · age: Prepare a “temporary age record card”.
Lines 9-12: Collect All Ages
for (int i = 0; i < n; i++) { cin >> age; sum += age; // Accumulate age}
Detailed Process:
· for (int i = 0; i < n; i++): Loop n times, with i ranging from 0 to n-1. · cin >> age: Read a student’s age. · sum += age: Equivalent to sum = sum + age, adding the current age to the total.
Metaphor: Just like a teacher asking each student their age and recording it in the total ledger.
Lines 15-16: Calculate and Output Result
double average = (double)sum / n;cout << fixed << setprecision(2) << average << endl;
Key Point Explanation:
· (double)sum: Converts the integer sum to a floating-point number, so the division can yield a decimal. · fixed << setprecision(2): Fixes the decimal format, rounding to 2 decimal places. · Without this, the output may not show two decimal places.
Example Explanation
Input:
3181920
Calculation Process:
1. n = 3. Loop 3 times: · First: age=18, sum=0+18=18 · Second: age=19, sum=18+19=37 · Third: age=20, sum=37+20=57. Average age = 57 ÷ 3 = 19.00
Output:
19.00
Important Detail Explanation
1. Why use (double)sum?
If written as sum / n, the result of dividing two integers is still an integer:
· 57 ÷ 3 = 19 (integer) · But we need 19.00 (floating-point).
(double)sum / n converts sum to a floating-point number, resulting in a floating-point output.
2. Output Format Control
· fixed: Fixed decimal format. · setprecision(2): Sets precision to 2 decimal places. · This way, 19 will display as 19.00 instead of 19.
3. Two Ways to Write the Loop
// Method 1: From 0 to n-1for (int i = 0; i < n; i++)
// Method 2: From 1 to n for (int i = 1; i <= n; i++)
Both methods loop n times, just with different starting values for the counter.
Complete Test Cases
Test 1:
Input: 2 18 17Output: 17.50
Test 2:
Input: 1 20Output: 20.00
Test 3:
Input: 4 15 16 17 18Output: 16.50
Conclusion
This problem tests:
1. Basic input and output. 2. Use of for loops. 3. Accumulation and summation. 4. Data type conversion. 5. Output format control.
Mastering these basics will allow you to easily solve problems involving average calculations!