C++20 sections · 1024 units
Open in Course

Problem: Calculate Average

Multiple parameters

Write double average(int a, int b, int c) that returns the average of three integers. Add them together, then divide by 3.0. Notice 3.0, not 3. Integer division loses decimals. Test with 1, 2, 3.

The average is 2.0, not 2. If you get 2, you used integer division. Fix by dividing by 3.0 to force floating-point math. Return types matter. If you returned int, the decimal gets truncated and you lose precision.

Think about what answer you need before choosing your return type.