Write a C++ program to analyze a variety of triangles. The program should determ
ID: 3670089 • 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 More details for each option is provided belowOption 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) A 180 - B- C 180 - 48.12 - 35 96.88 Find angle A : B 48.12 2 (20) (11.55) 20 11.55 15 B cos 15 20 11.55 2 (20) (11.55) cos(B) b a c 2 a c cos(B) Find angle B using the law of cosines again : c 11.55 c 20 15 2 (20) (15) cos(35 ) c a b 2 (a) (b) cos(C) Find side c using the law of cosines : If a 20, b 15, C 35 , then Example 1
Explanation / Answer
Solution: See the code below
---------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <cmath> //for math functions
#define PI 3.1418 //constant PI
using namespace std;
//main function
int main() {
cout << "Program to analyze variety of triangles" << endl; // introduction statement
//while loop for processing choices
while(1)
{
int choice;
cout<<"Use a choice below to enter your options."<<endl;
cout<<"1. Given two sides and the angle between:"<<endl;
cout<<"2. Given two angles and one side:"<<endl;
cout<<"3. Given three sides:"<<endl;
cout<<"0. To Exit."<<endl;
cout<<"Choice:";
cin>>choice;
double sideA=0.0,sideB=0.0,sideC=0.0; //sides of the triangle
double angleA=0.0, angleB=0.0, angleC=0.0; //angles of the triangle
bool degrees;
switch(choice)
{
case 1: //choice 1
cout<<"Enter side a (in front of angle A):";
cin>>sideA;
cout<<"Enter side b (in front of angle B):";
cin>>sideB;
cout<<"Enter angle between sides a and b (angle C):";
cin>>angleC;
cout<<"Angle in degress or radians(1/0)?";
cin>>degrees;
cout<<"Checking validity of values..."<<endl;
if(sideA < 0 || sideB < 0)
{
cout<<"Invalid sides! Aborting..."<<endl;
break;
}
if(degrees)
{
if(angleC < 0 || angleC > 180)
{
cout<<"Invalid angle! Aborting..."<<endl;
break;
}
else
angleC=angleC*(PI/180);
}
else
{
if(angleC < 0 || angleC > PI)
{
cout<<"Invalid angle! Aborting..."<<endl;
break;
}
}
cout<<"Values are valid. Now calculating rest of sides and angles..."<<endl;
//for side C
sideC = sqrt(pow(sideA,2)+pow(sideB,2)-2*sideA*sideB*cos(angleC));
//for angle A
angleA = acos((pow(sideB,2)+pow(sideC,2)-pow(sideA,2))/(2*sideB*sideC));
//for angle B
angleB = acos((pow(sideC,2)+pow(sideA,2)-pow(sideB,2))/(2*sideC*sideA));
cout<<"Rest of the sides and angles calculated!"<<endl;
cout<<"Triangle is (all angles in radians):"<<endl;
cout<<"Side a:"<<sideA<<" Side b:"<<sideB<<" Side c:"<<sideC<<endl;
cout<<"Angle A:"<<angleA<<" Angle B:"<<angleB<<" Angle C:"<<angleC<<endl;
break;
case 2: //choice 2
cout<<"Enter angle A:";
cin>>angleA;
cout<<"Enter angle B:";
cin>>angleB;
cout<<"Enter side between angles A and B (side C):";
cin>>sideC;
cout<<"Angle in degrees or radians(1/0)?";
cin>>degrees;
cout<<"Checking validity of values..."<<endl;
if(sideC < 0)
{
cout<<"Invalid side! Aborting..."<<endl;
break;
}
if(degrees)
{
if((angleA < 0 || angleA > 180) || (angleB < 0 || angleB > 180))
{
cout<<"Invalid angles! Aborting..."<<endl;
break;
}
else
{
angleA=angleA*(PI/180);
angleB=angleB*(PI/180);
}
}
else
{
if((angleA < 0 || angleA > PI) || (angleB < 0 || angleB > PI))
{
cout<<"Invalid angles! Aborting..."<<endl;
break;
}
}
cout<<"Values are valid. Now calculating rest of sides and angles..."<<endl;
//for angle C
angleC=PI-(angleA+angleB);
//for side A
sideA = sideC*(cos((PI/2+angleA))/cos((PI/2+angleC)));
//for side B
sideB = sideC*(cos((PI/2+angleB))/cos((PI/2+angleC)));
cout<<"Rest of the sides and angles calculated!"<<endl;
cout<<"Triangle is (all angles in radians):"<<endl;
cout<<"Side a:"<<sideA<<" Side b:"<<sideB<<" Side c:"<<sideC<<endl;
cout<<"Angle A:"<<angleA<<" Angle B:"<<angleB<<" Angle C:"<<angleC<<endl;
break;
case 3: //choice 3
cout<<"Enter side a:";
cin>>sideA;
cout<<"Enter side b:";
cin>>sideB;
cout<<"Enter side c:";
cin>>sideC;
cout<<"Checking validity of values..."<<endl;
if(sideA < 0 || sideB < 0 || sideC < 0)
{
cout<<"Invalid sides! Aborting..."<<endl;
break;
}
cout<<"Values are valid. Now calculating rest of sides and angles..."<<endl;
//for angle A
angleA = acos((pow(sideB,2)+pow(sideC,2)-pow(sideA,2))/(2*sideB*sideC));
//for angle B
angleB = acos((pow(sideC,2)+pow(sideA,2)-pow(sideB,2))/(2*sideC*sideA));
//for angle C
angleC = acos((pow(sideA,2)+pow(sideB,2)-pow(sideC,2))/(2*sideA*sideB));
cout<<"Rest of the sides and angles calculated!"<<endl;
cout<<"Triangle is (all angles in radians):"<<endl;
cout<<"Side a:"<<sideA<<" Side b:"<<sideB<<" Side c:"<<sideC<<endl;
cout<<"Angle A:"<<angleA<<" Angle B:"<<angleB<<" Angle C:"<<angleC<<endl;
break;
case 0: //choice 0
cout<<"Exiting..."<<endl;
exit(1);
default: //wrong choice
cout<<"Wrong choice, Re-enter."<<endl;
break;
}
}
return 0;
}
----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.