Write a C++ program to analyze a variety of triangles. The program should determ
ID: 3713071 • Letter: W
Question
Write a C++ program to analyze a variety of triangles. The program should determine all angles and all sides for a triangle for three different options (give the user a menu of choices):
1) Given two sides and the angle between
2) Given two angles and one side
3 ) Given three sides
Option 1: Given two sides and the angle between
First check to be sure that the three values entered are valid. In particular, the following conditions must be met:
Both sides > 0
If angle is in degrees, then 0 < angle < 180 (if angle is in radians, convert it to degrees first and then check)
Option 2: Given two angles and one side
First check to be sure that the three values entered are valid. In particular, the following conditions must be met:
Side > 0
For each angle: 0 < angle < 180 (if angle is in radians, convert it to degrees first and then check)
For the sum of the two angles entered: 0 < Sum < 180 (if angles are in radians, convert them to degrees first and then check)
Option 3: Given three sides
First check to be sure that the three sides entered form a valid triangle. For the triangle to be valid, 6 conditions must be met:
a > 0, b > 0, c > 0
a < b+c, b
Other program requirements:
The user should be able to indicate whether and angle is being entered is in degrees or in radians by entering either d or D for degrees or entering r or R for radians. Examples:
Please enter the angle: 30 d
Please enter the angle: 30 D
Please enter the angle: 1.5 r
Please enter the angle: 1.5 R
If any bad inputs are entered, print out an appropriate error message and terminate the program using the command: return 1;
Bad inputs include invalid triangles, invalid menu choices, and invalid units for angles.
Example:
if (Side < 0)
{ cout << “Error. Negative value for side not allowed. Program terminated.”;
Return 1;
}
After performing the calculations for any of the three menu choices, display the values for all three sides and all three angles (in degrees).
Use 2 digits after the decimal point for all angles.
Use 3 digits after the decimal point for all sides.
Include the unit degrees after all angles.
No units are required for the sides.
Note that for option 2, the calculations are different based on which side is entered. Ask the user to enter angles A and B, and then ask the user which side that they would like to enter (a, b, or c). Perform the appropriate calculations in each case.
Extra Credit Suggestions:
You can earn up to 10 additional points on this program’s grade. The number of points awarded depends on the complexity or creativity of the feature. Here are a few ideas:
1. If an incorrect entry is made by the user, display an error message and request that the user re-enter the information. This can be done fairly easily using a do while loop (covered in Ch. 6).
Display a diagram of the triangle with A, B, C, a, b, and c labeled so that it is clearer to the user what should be entered.
Include units with the sides of the triangle. Allow the user to enter m, ft, or in after the values for sides. Display the same units with the results.
Explanation / Answer
#include <iostream>
#include<iomanip>
#define PI 3.14159
#define RADTODEG(R)((180.0 * R) / PI)//Converts Radians to Degrees
using namespace std;
int main() {
// your code goes here
float a,b,c,A,B,C,angle,angle1,sum,side;
int ch,loop;
char dr,dr1;
do
{
cout<<"1.Two side and one angle are given "<<" ";
cout<<"2.Two angles and one side are given "<<" ";
cout<<"3.Thre sides are given "<<" ";
cout<<"Enter your choice :";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter the two sides: ";
cin>>a>>b;
cout<<"Please enter the angle: ";
cin>>A>>dr;
angle=A;
if(dr=='r'||dr=='R')
{
angle=RADTODEG(A);
}
if(a>0&&b>0)
{
if(angle>0&&angle<180)
{
cout <<fixed <<setprecision(3) <<a;
cout <<fixed <<setprecision(3) <<b;
cout <<fixed <<setprecision(2) <<angle<<"degree";
}
}
else
{
if (a< 0||b<0||angle<0||angle>180)
{
cout<<"Error Program Terminated.";
return 1;
}
}
}
break;
case 2:
{
cout<<"Enter the two angles: ";
cin>>A>>dr>>B>>dr1;
cout<<"Which side you want a/b/c: ";
cin>>side;
angle=A;
angle1=B;
if(dr=='r'||dr=='R')
{
angle=RADTODEG(A);
}
if(dr1=='r'||dr1=='R')
{
angle1=RADTODEG(B);
}
sum=angle+angle1;
if(side>0)
{
if(sum>0&&sum<180)
{
cout <<fixed <<setprecision(2) <<angle<<"degree";
cout <<fixed <<setprecision(2) <<angle1<<"degree";
cout <<fixed <<setprecision(3) <<side;
}
}
else
{
if (side< 0||sum>180)
{
cout<<"Error Program Terminated.";
return 1;
}
}
}
break;
case 3:
{
cout<<"Enter three sides of the triangle";
cin>>a>>b>>c;
if(a>0&&b>0&&c>0)
{
if(a<b+c&&b<a+c&&c<a+b)
{
cout <<fixed <<setprecision(3) <<a;
cout <<fixed <<setprecision(3) <<b;
cout <<fixed <<setprecision(3) <<c;
}
}
else
{
if(a<0||b<0||c<0)
{
cout<<"Error Negative Sides Not Allowed. Program terminated.";
return 1;
}
}
}
break;
default:
cout<<"Error. Invalid choice";
}
cout<<"Do you want to continue Press 1/0:";
cin>>loop;
}while(loop!=0);
}
// If you have any query regarding the solution do comment and if you like my solution then please give a thumbs up!
Thanks :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.