Read the question, write C++ on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
The first number is n, then n integers. Print their average, rounded to exactly two decimals.
Watch the integer-division trap: divide by n as a double, or the fraction is lost.
Input. n, then n integers (whitespace-separated).
Output. The average with two decimals, e.g. 5.00.
Constraints
- 1 ≤ n ≤ 1000
Examples
Input
4 2 4 6 8
Output
5.00
Input
3 1 2 2
Output
1.67
Hint
- Sum into a long long, then cast: (double)sum / n.
- #include <iomanip>, then std::fixed << std::setprecision(2).
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <iostream>
#include <iomanip>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector<int> v(n);
long long sum = 0;
for (int i = 0; i < n; i++) { std::cin >> v[i]; sum += v[i]; }
std::cout << std::fixed << std::setprecision(2) << ((double)sum / n) << "\n";
return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.