In: Computer Science
C++ exercise:
The statements in the file main.cpp are in incorrect order.
using namespace std;
#include <iostream>
int main()
{
string shape;
double height;
#include <string>
cout << "Enter the shape type: (rectangle, circle, cylinder)
";
cin >> shape;
cout << endl;
if (shape == "rectangle")
{
cout << "Area of the circle = "
<< PI * pow(radius, 2.0) << endl;
cout << "Circumference of the circle: "
<< 2 * PI * radius << endl;
cout << "Enter the height of the cylinder: ";
cin >> height;
cout << endl;
cout << "Enter the width of the rectangle: ";
cin >> width;
cout << endl;
cout << "Perimeter of the rectangle = "
<< 2 * (length + width) << endl;
double width;
}
cout << "Surface area of the cylinder: "
<< 2 * PI * radius * height + 2 * PI * pow(radius, 2.0)
<< endl;
}
else if (shape == "circle")
{
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << endl;
cout << "Volume of the cylinder = "
<< PI * pow(radius, 2.0)* height << endl;
double length;
}
return 0;
else if (shape == "cylinder")
{
double radius;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << endl;
#include <iomanip>
cout << "Enter the radius of the base of the cylinder:
";
cin >> radius;
cout << endl;
const double PI = 3.1416;
cout << "Area of the rectangle = "
<< length * width << endl;
else
cout << "The program does not handle " << shape
<< endl;
cout << fixed << showpoint <<
setprecision(2);
#include <cmath>
}
Rearrange the statements so that they prompt the user to input:
Note: For grading purposes place the cylinder height statement before the radius statement.
The program then outputs the following information about the shape:
Use 3.1416 as the constant value for any calculations that may need \piπ.
After rearranging the statements, your program should be properly indented.
#include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; int main() { string shape; double height; double width; double radius; double length; const double PI = 3.1416; cout << "Enter the shape type: (rectangle, circle, cylinder) "; cin >> shape; cout << endl; cout << fixed << showpoint << setprecision(2); if (shape == "rectangle") { cout << "Enter the length of the rectangle: "; cin >> length; cout << endl; cout << "Enter the width of the rectangle: "; cin >> width; cout << endl; cout << "Perimeter of the rectangle = " << 2 * (length + width) << endl; cout << "Area of the rectangle = " << length * width << endl; } else if (shape == "circle") { cout << "Enter the radius of the circle: "; cin >> radius; cout << endl; cout << "Area of the circle = " << PI * pow(radius, 2.0) << endl; cout << "Circumference of the circle: " << 2 * PI * radius << endl; } else if (shape == "cylinder") { cout << "Enter the height of the cylinder: "; cin >> height; cout << endl; cout << "Enter the radius of the base of the cylinder: "; cin >> radius; cout << endl; cout << "Surface area of the cylinder: " << 2 * PI * radius * height + 2 * PI * pow(radius, 2.0) << endl; cout << "Volume of the cylinder = " << PI * pow(radius, 2.0) * height << endl; } else cout << "The program does not handle " << shape << endl; return 0; }