20.5k views
4 votes
You are to write a program which will have the following Functions:

getAreaRectangle
getArea Triangle
getAreaCircle
getPerimeterRectangle
getPerimeter Triangle
getPerimeter Circle

You will first need to ask the user if they want to find the Area or the Perimeter.
Then you will ask of which shape (Rectangle, Triangle, Circle).

Finally, based on the input of the user, you will call the Function, passing all necessary variables to the function. The function will
return the answer and you will then display the correct answer on the screen.

This is going to be a long programit it will mainly test your ability to use Condition statements and Functions. You can use a switch
for the menu if you want.

User Phil Poore
by
9.0k points

1 Answer

3 votes

Sample code (in c++):

#include <iostream>

#include <cmath>

using namespace std;

const double PI = 3.14159265358979323846;

double getAreaRectangle(double length, double width)

{

return length * width;

}

double getAreaTriangle(double base, double height)

{

return 0.5 * base * height;

}

double getAreaCircle(double radius)

{

return PI * pow(radius, 2);

}

double getPerimeterRectangle(double length, double width)

{

return 2 * (length + width);

}

double getPerimeterTriangle(double side1, double side2, double side3)

{

return side1 + side2 + side3;

}

double getPerimeterCircle(double radius)

{

return 2 * PI * radius;

}

int main()

{

int choice;

string shape;

cout << "Choose what to find: \\";

cout << "1 - Area\\";

cout << "2 - Perimeter\\";

cout << "Enter choice: ";

cin >> choice;

cout << "Choose a shape: \\";

cout << "1 - Rectangle\\";

cout << "2 - Triangle\\";

cout << "3 - Circle\\";

cout << "Enter choice: ";

cin >> shape;

switch (shape)

{

case "Rectangle":

{

double length, width;

cout << "Enter length: ";

cin >> length;

cout << "Enter width: ";

cin >> width;

if (choice == 1)

{

double area = getAreaRectangle(length, width);

cout << "The area of the rectangle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterRectangle(length, width);

cout << "The perimeter of the rectangle is " << perimeter << endl;

}

break;

}

case "Triangle":

{

double base, height, side1, side2, side3;

cout << "Enter base: ";

cin >> base;

cout << "Enter height: ";

cin >> height;

cout << "Enter side 1: ";

cin >> side1;

cout << "Enter side 2: ";

cin >> side2;

cout << "Enter side 3: ";

cin >> side3;

if (choice == 1)

{

double area = getAreaTriangle(base, height);

cout << "The area of the triangle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterTriangle(side1, side2, side3);

cout << "The perimeter of the triangle is " << perimeter << endl;

}

break;

}

case "Circle":

{

double radius;

cout << "Enter radius: ";

cin >> radius;

if (choice == 1)

{

double area = getAreaCircle(radius);

cout << "The area of the circle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterCircle(radius);

cout << "The perimeter of the circle is " << perimeter << endl;

}

break;

}

default:

cout << "Invalid shape choice" << endl;

break;

}

return 0;

}Sample code:

#include <iostream>

#include <cmath>

using namespace std;

const double PI = 3.14159265358979323846;

double getAreaRectangle(double length, double width)

{

return length * width;

}

double getAreaTriangle(double base, double height)

{

return 0.5 * base * height;

}

double getAreaCircle(double radius)

{

return PI * pow(radius, 2);

}

double getPerimeterRectangle(double length, double width)

{

return 2 * (length + width);

}

double getPerimeterTriangle(double side1, double side2, double side3)

{

return side1 + side2 + side3;

}

double getPerimeterCircle(double radius)

{

return 2 * PI * radius;

}

int main()

{

int choice;

string shape;

cout << "Choose what to find: \\";

cout << "1 - Area\\";

cout << "2 - Perimeter\\";

cout << "Enter choice: ";

cin >> choice;

cout << "Choose a shape: \\";

cout << "1 - Rectangle\\";

cout << "2 - Triangle\\";

cout << "3 - Circle\\";

cout << "Enter choice: ";

cin >> shape;

switch (shape)

{

case "Rectangle":

{

double length, width;

cout << "Enter length: ";

cin >> length;

cout << "Enter width: ";

cin >> width;

if (choice == 1)

{

double area = getAreaRectangle(length, width);

cout << "The area of the rectangle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterRectangle(length, width);

cout << "The perimeter of the rectangle is " << perimeter << endl;

}

break;

}

case "Triangle":

{

double base, height, side1, side2, side3;

cout << "Enter base: ";

cin >> base;

cout << "Enter height: ";

cin >> height;

cout << "Enter side 1: ";

cin >> side1;

cout << "Enter side 2: ";

cin >> side2;

cout << "Enter side 3: ";

cin >> side3;

if (choice == 1)

{

double area = getAreaTriangle(base, height);

cout << "The area of the triangle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterTriangle(side1, side2, side3);

cout << "The perimeter of the triangle is " << perimeter << endl;

}

break;

}

case "Circle":

{

double radius;

cout << "Enter radius: ";

cin >> radius;

if (choice == 1)

{

double area = getAreaCircle(radius);

cout << "The area of the circle is " << area << endl;

}

else if (choice == 2)

{

double perimeter = getPerimeterCircle(radius);

cout << "The perimeter of the circle is " << perimeter << endl;

}

break;

}

default:

cout << "Invalid shape choice" << endl;

break;

}

return 0;

}

User Travis Well
by
8.8k points