Question

In: Computer Science

C++ exercise: The statements in the file main.cpp are in incorrect order. using namespace std; #include...

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:

  • The shape type (rectangle, circle, or cylinder)
  • The appropriate dimension of the shape.

Note: For grading purposes place the cylinder height statement before the radius statement.

The program then outputs the following information about the shape:

  • For a rectangle, it outputs the area and perimeter
  • For a circle, it outputs the area and circumference
  • For a cylinder, it outputs the volume and surface area.

Use 3.1416 as the constant value for any calculations that may need \piπ.

After rearranging the statements, your program should be properly indented.

Solutions

Expert Solution

#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;
}

Related Solutions

Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT