// Written by: Michael Mozer // Date: Oct 13 2003 // // PROGRAM DESCRIPTION // // This program lets a user type in number as a radius. // It then checks if the number is a valid radius. // If it is, the program calculates // 1. the circumference of the circle // 2. the area of the circle // 3. the surface area of the sphere // 4. the volume of the sphere // for that radius and then prints the result to the screen. // If it is not, the program prints an error message and quits. #include int calculate(double radius, double& circumference, double& area, double& surf_area, double& volume); void main() { double radius, circumference, area, surface_area, volume; int result; cout << "Given a radius, this program will compute" << endl; cout << " 1. The cicumference of a circle," << endl; cout << " 2. The area of a circle," << endl; cout << " 3. The surface area of a sphere, and" << endl; cout << " 4. The volume of a sphere." << endl; cout << endl; cout << "Please enter a non-negative number as the radius: "; cin >> radius; result = calculate(radius, circumference, area, surface_area, volume); if (result) { cout << "Given the radius of " << radius << endl; cout << "The circumference of the circle is " << circumference << endl; cout << "The area of the circle is " << area << endl; cout << "The surface area of the sphere is " << surface_area << endl; cout << "The volume of the sphere is " << volume << endl; cout << "Good bye." << endl; } else { cout << "The number you gave is not a valid radius." << endl; cout << "A valid radius must be non-negative." << endl; cout << "Please try running this program again. Bye." << endl; } } // return 1 if the calculation is successful, return 0 otherwise int calculate(double radius, double& circumference, double& area, double& surface_area, double& volume) { const double PI = 3.14159; if (radius < 0.0) return 0; circumference = 2.0 * PI * radius; area = PI * radius * radius; surface_area = 4.0 * PI * radius * radius; volume = (4.0 / 3.0) * PI * radius * radius * radius; return 1; }