77.6k views
0 votes
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

1 Answer

1 vote

Final answer:

The area and circumference of a circle should be presented with the same number of significant figures as the least precise measurement used in the calculations.

Step-by-step explanation:

When calculating the area and circumference of a circle, it's important to consider the number of significant figures from the given inputs. If the radius has only two significant figures, like 1.2 m, then the resulting area calculated should also be represented with two significant figures, such as A = 4.5 m², for consistency and accuracy in scientific and mathematical calculations.

The circle calculations using the formula for area (πr²) and circumference (2πr) directly depend on the precision of the given radius. To ensure the result meets the correct number of significant figures, programming statements in C++ should include methods such as cout.precision() to control the output format.

User Angelito
by
8.4k points