Some calculations deserve a named library function rather than a handwritten approximation. <cmath> provides common mathematical operations. Use the std:: qualification so the source makes their origin clear.
| Function | Purpose |
|---|---|
std::sqrt(x) | Nonnegative square root, with nonnegative input here |
std::abs(x) | Magnitude without the sign |
std::round(x) | Nearest whole value, halfway cases away from zero |
#include <cmath>
#include <iostream>
int main() {
std::cout << std::sqrt(49.0) << '\n';
std::cout << std::abs(-2.5) << '\n';
std::cout << std::round(3.5) << '\n';
}
The .0 spelling makes the square-root input a double. These functions return values; they do not change the argument variable. std::round on a double returns a double representing a whole value, not an integer type. Converting that result to an integer would still require checking that it fits the destination range.
Stay within each function's intended domain. Here square-root inputs are nonnegative. The absolute-value examples use small doubles; taking the absolute value of the most negative signed integer cannot always be represented by the same integer type. Avoid that boundary rather than assuming a library name removes numeric limits.
In the editor, area is the area of a square, so its square root gives the side length. change is negative, but the report needs its magnitude. estimate needs a whole-value approximation. Replace the three zero initializers with the appropriate calls, using the supplied variables as arguments. The probes use values with uncomplicated expected display, so this lesson is about selecting operations rather than managing floating-point tolerances. In larger numerical work, decimal representation and accumulated rounding error need separate attention. Use rounding deliberately at a meaningful boundary, not after every intermediate calculation.
Edit main.cpp; the supplied Console probes already call your code.
side with the square root of area. Expect Side: 9.magnitude with the absolute value of change. Expect Magnitude: 4.5.rounded by rounding estimate. Expect Rounded: 7.Use Run to inspect the Console, then Submit to check your work.