Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
C++ provides many functions that have already been written and tested for common jobs. std::pow raises one value to a power, and std::sqrt finds a square root. They are ordinary function calls with arguments and return values, not special operators that the language understands in every file. For the point (-3, 4), you can square both coordinates, add the results, and find the square root of that sum.
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
int x = -3;
int y = 4;
double xSquared = std::pow(x, 2);
double ySquared = std::pow(y, 2);
double distance = std::sqrt(xSquared + ySquared);
std::cout << std::fixed << std::setprecision(1) << distance << '\n';
return 0;
}The two std::pow calls return 9.0 and 16.0. Their sum is 25.0, and std::sqrt returns 5.0, which is the distance from the point to the origin. The names of the functions, their arguments, the intermediate values, and the printed result are different parts of the program.
Which parts of this program are the prewritten standard library function calls used to calculate the distance?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The line #include <cmath> gives the compiler the declarations for mathematical functions such as std::pow and std::sqrt. The prefix std:: says that these names belong to the standard namespace. Both parts matter: the header makes the declarations available, and the qualified name tells C++ exactly which names you want to call.
#include <cmath>
int x = -3;
int y = 4;
double xSquared = std::pow(x, 2);
double distance = std::sqrt(xSquared + std::pow(y, 2));If you omit <cmath>, the compiler may say that pow or sqrt was not declared. If you write a name that is not available as a member of std, it may report that the function is not a member of std. A bare call such as sqrt(25.0) is not automatically safe just because C++ has a square root function somewhere in its library.
A standard library reference describes a function's contract. It tells you which header to include, the qualified name to call, the arguments it accepts, and the return type it produces. You do not need to guess these details from the function's spelling.
std::pow(-3, 2) -> 9.0
std::pow(4, 2) -> 16.0
9.0 + 16.0 -> 25.0
std::sqrt(25.0) -> 5.0For this point, std::pow(-3, 2) produces 9.0 because a negative number becomes positive when squared. std::pow(4, 2) produces 16.0. Adding those returned values gives 25.0, which becomes the argument to std::sqrt. The square root call then returns 5.0.
Repair this distance calculation by adding the needed header and qualifying both mathematical function names.
int x = -3;
int y = 4;
double distance = sqrt(pow(x, 2) + pow(y, 2));Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The coordinates x and y are int because they are whole numbers, but xSquared, ySquared, and distance are double because the mathematical functions return floating-point values. Using double keeps the returned values suitable for calculations that may contain a fractional part.
int x = -3;
int y = 4;
double xSquared = std::pow(x, 2);
double ySquared = std::pow(y, 2);
double distance = std::sqrt(xSquared + ySquared);For the current point, storing 5.0 in an int does not visibly change the number because it has no fractional part. A different square root call could return a value such as 2.5. If that successful return value is stored in an int, the fractional part is discarded, leaving 2 instead of 2.5. The function worked, but the receiving variable stored the answer in a form that could not preserve it.