Respuesta :
Answer:
The solution code is as follows:
- #include <iostream>
- using namespace() std;
- const double PI = 3.14159;
- int getValidInput() {
- int radius;
- cout << "Type a -1 for radius to exit" << endl;
- cout << "Enter the radius: ";
- cin >> radius;
- while(radius < 0){
- cout << "Type a -1 for radius to exit" << endl;
- cout << "Enter the radius: ";
- cin >> radius;
- }
- return radius;
- }
- double circleCalculations(int r){
- double circ = 2 * PI * r;
- double area = PI * pow(r, 2);
- return area;
- }
- void printResults(int r, double a){
- cout.setf(ios::fixed);
- cout.precision(1);
- cout << "A circle with radius " << r << " has a circumference of "
- << circ << " and an area of " << a << endl << endl;
- }
- int main()
- {
- int radius;
- double area;
- double circ;
- radius = getValidInput();
- area = circleCalculations(radius);
- printResults(radius, area);
- }
Explanation:
In this question, we can identify three main functional blocks from the original codes. We cut the block of codes that ask for user input radius and paste it into the new function getValidInput() (Line 6 - 9). However, we need to add the code in Line 11-15 to ensure only the radius with valid value will be returned.
Next, we can proceed to cut another block of code that calculate the circumference and area and paste it into function circleCalculations (Line 19 -24).
Next, we cut the code that display the radius and area and paste it to function printResults(Line 26-32).
At last, we call all the three functions in the main program (Line 39-41).