There is only one easy, practical test case: r = 1, which yields a = 3.141592 (we can also use r = 0, which produces 0, but that provides little assurance that the program is operating correctly). There is no shortcut here: we must choose a set of test inputs arbitrarily and use a calculator to calculate the results manually.
r | a | |
---|---|---|
Case 1 | 2 | 12.5664 | Case 2 | 2.75 | 23.7583 |
Although I try to restrict the programming examples to generic, ANSI-compliant features, it's not always possible to complete a program without them. Microsoft Visual Studio requires the unusual and non-standard #define _USE_MATH_DEFINES directive to use the symbolic constants (just the constants, not the functions) contained in the math library, and the directive must appear before any of the #include statements. Other ANSI compilers do not require the directive, but it doesn't cause problems if we include it in a program compiled with non-Microsoft compilers.
#define _USE_MATH_DEFINES // Microsoft only #include <iostream> #include <cmath> // for M_PI using namespace std; int main() { double r; cout << "Radius: "; cin >> r; double a = M_PI * r * r; cout << "Area = " << a << endl; return 0; } |
#define _USE_MATH_DEFINES // Microsoft only #include <iostream> #include <cmath> // for pow and M_PI using namespace std; int main() { double r; cout << "Radius: "; cin >> r; double a = M_PI * pow(r, 2); cout << "Area = " << a << endl; return 0; } |