Copy the following code and run it. You should break it into the following 3 functions
getValidInput - which asks the user to enter the radius and then make sure that it is valid before returning it
circleCalculations - which uses the raius passed in to calculate both the area and the circumference. The area is returned
printResults - sets the fixed and precision and prints out the output

#include
#include
using namespace std;

const double PI = 3.14159;
int main()
{
int radius;
double area;
double circ;

cout << "Type a -1 for radius to exit" << endl;
cout << "Enter the radius: ";
cin >> radius;
while (radius >= 0)
{
circ = 2 * PI * radius;
area = PI * pow(radius, 2);

cout.setf(ios::fixed);
cout.precision(1);
cout << "A circle with radius " << radius << " has a circumference of "
<< circ << " and an area of " << area << endl << endl;

cout << "Enter the radius: ";
cin >> radius;
}
}
Sample Output

Type a -1 for radius to exit
Enter the radius: 5
A circle with radius 5 has a circumference of 31.4 and an area of 78.5

Enter the radius: -4
Radius cannot be negative - try again!!!
Enter the radius: -7
Radius cannot be negative - try again!!!
Enter the radius: 12
A circle with radius 12 has a circumference of 75.4 and an area of 452.4

Enter the radius: 7
A circle with radius 7 has a circumference of 44.0 and an area of 153.9

Enter the radius: -1

Respuesta :

Answer:

The solution code is as follows:

  1. #include <iostream>
  2. using namespace() std;
  3. const double PI = 3.14159;
  4. int getValidInput() {
  5.    int radius;
  6.    cout << "Type a -1 for radius to exit" << endl;
  7.    cout << "Enter the radius: ";
  8.    cin >> radius;
  9.    
  10.    while(radius < 0){
  11.        cout << "Type a -1 for radius to exit" << endl;
  12.        cout << "Enter the radius: ";
  13.        cin >> radius;
  14.    }
  15.    return radius;
  16. }
  17. double circleCalculations(int r){
  18.    
  19.        double circ = 2 * PI * r;
  20.        double area = PI * pow(r, 2);
  21.        return area;
  22. }
  23. void printResults(int r, double a){
  24.    
  25.    cout.setf(ios::fixed);
  26.    cout.precision(1);
  27.    cout << "A circle with radius " << r << " has a circumference of "
  28.    << circ << " and an area of " << a << endl << endl;  
  29. }
  30. int main()
  31. {
  32.    int radius;
  33.    double area;
  34.    double circ;
  35.    radius = getValidInput();
  36.    area = circleCalculations(radius);
  37.    printResults(radius, area);
  38. }

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).