Assignment purpose: simple i/o in C++, simple math in C++, simple loop, data types string, double, and int, formatting output in C++, predefined functions, programmer defined functions, call by reference and call by value.

Write a program that will calculate the area and perimeter of a triangle and the area and perimeter of a rectangle

  • The user should be able to continue to the calculate either the area and perimeter of a triangle OR the area and perimeter of a rectangle until they enter a Q or q to QUIT
  • You must use the main function provided
  • YOU MAY NOT MAKE ANY MODIFICATIONS TO THE MAIN FUNCTION
  • You will implement the following void functions:
    • MenuChoices
    • ProcessChoice
  • You may add other programmer defined functions as needed
  • You must use function prototypes (above the main function) and function definitions (below the main function)
  • BE SURE TO ADD COMMENTS TO THE FUNCTION PROTOTYPES
    • pre and post conditions and a brief description
  • BE SURE TO ADD COMMENTS TO THE FUNCTION DEFINITIONS
    • brief description

Here is the main function:

int main()
{
double area = 0.0;
double perimeter;
string quit;
string name;
cout << "Welcome to the area and perimeter calculator, what is your first name? ";
cin >> name;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3); //display the area to 3 decimal places
MenuChoices(name, quit);
ProcessChoice(quit, area, perimeter);
while (quit != "Q" && quit != "q")
{
cout << "\nThe area (displayed in the main function) is " << area << endl;
cout << "\nThe perimeter (displayed in the main function) is " << perimeter << endl;
MenuChoices(name, quit);
ProcessChoice(quit, area, perimeter);
}
cout << "\nGoodbye " << name << ", Have a great day! ";
return 0;
}

Additional instructions:

Write the algorithm before implementing the program
Be sure to test throughout the code development

Follow all style rules and include all necessary documentation

  • consistent indentation
  • proper, meaningful function and variable names
  • display the sides to one decimal place and the area and perimeter to 3 decimal places, see the sample output
  • pre/post conditions and brief description on the function prototypes
  • brief description comments on the function definitions, see the program 3 sample
  • See the example provided for program 3 for proper commenting and style.
  • Be sure to eliminate all warnings

The area of an arbitrary triangle can be computed using the following formula:

area =sqrt(s(s - a)(s - b)(s - c))

Where a, b, and c are the lengths of the sides and s is the semiperimeter.

s = (a + b + c) / 2

Note that not all combinations of a, b, and c produce a triangle.

  • The sum of any two sides of a triangle must be greater than the third side.
  • You must make sure that the lengths of the sides of the triangle entered by the user are valid
    • if not valid lengths, let the user know by printing a message stating that the lengths are invalid
    • produce correct results for legal data and reasonable results for illegal combinations.

NOTE: The perimeter is NOT the same as the semiperimeter

  • Use a descriptive name for any additional functions and identifiers
  • Get the users first name and interact with the user by displaying their name (see the sample output)
  • Print the results (area and perimeter) in the main function
  • The user should be able to continue to enter triangle sides until they enter a Q or q to QUIT or Y or y to continue
  • There is a sqrt function in #include < cmath> (square root)

Here is sample output

Welcome to the area and perimeter calculator, what is your first name? Tami
----------------------------------------------
Tami, please enter a T, R, or Q.
To calculate:
The area and perimeter of a triangle, enter T
The area and perimeter of a rectangle, enter R
----------------------------------------------
Enter Q to quit: w
That input is not recognized
The area (displayed in the main function) is 0.000
The perimeter (displayed in the main function) is 0.000
----------------------------------------------
Tami, please enter a T, R, or Q.
To calculate:
The area and perimeter of a triangle, enter T
The area and perimeter of a rectangle, enter R
----------------------------------------------
Enter Q to quit: t
*********************************************
Enter the length of side one: 9.21
Enter the length of side two: 3.3
Enter the length of side three: 2.76
Not a valid triangle
The area (displayed in the main function) is 0.000
The perimeter (displayed in the main function) is 0.000
----------------------------------------------
Tami, please enter a T, R, or Q.
To calculate:
The area and perimeter of a triangle, enter T
The area and perimeter of a rectangle, enter R
----------------------------------------------
Enter Q to quit: t
*********************************************
Enter the length of side one: 3.78
Enter the length of side two: 2.43
Enter the length of side three: 4.12
------------------------------------
Side A Side B Side C
3.8 2.4 4.1
The area (displayed in the main function) is 4.522
The perimeter (displayed in the main function) is 10.330
----------------------------------------------
Tami, please enter a T, R, or Q.
To calculate:
The area and perimeter of a triangle, enter T
The area and perimeter of a rectangle, enter R
----------------------------------------------
Enter Q to quit: r
Enter the length of side one: 1.345
Enter the length of side two: 82.911
------------------------------------
Side X Side Y
1.3 82.9
The area (displayed in the main function) is 111.515
The perimeter (displayed in the main function) is 168.512
----------------------------------------------
Tami, please enter a T, R, or Q.
To calculate:
The area and perimeter of a triangle, enter T
The area and perimeter of a rectangle, enter R
----------------------------------------------
Enter Q to quit: q
Goodbye Tami, Have a great day!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.