There are three types of triangles: equilateral, isosceles, and scalene. Equilat
ID: 3877693 • Letter: T
Question
There are three types of triangles: equilateral, isosceles, and scalene. Equilateral triangle has 3 sides of equal length: Isosceles triangle has 2 sides of equal length and one side not equal to the others: Scalene triangle has 3 sides of unequal length. A special case is the right triangle: When a computer program is used to construct or validate a triangle, the programmer can develop some rules from the relationships between the sides. Every triangle has exactly three sides, and the lengths of the sides must be nonzero and positive. Not every combination of lengths is valid for a triangle, however. For an equilateral triangle, any positive length is valid provided all three sides have the same length. For any non-equilateral triangle, the third side has a length greater than zero and less than the sum of the other two sides. An isosceles triangle can be modeled as two mirror image right triangles: hence an unknown side can be determined from the Pythagorean theorem. The scalene triangle is the most difficult to validate. It can be modeled as two right triangles with the same height, but the two right triangles are not mirror images. Develop an algorithm to calculate the length of the third side of a triangle, given lengths of two sides and the angle between them. Study the examples illustrated above and figure out tests that the algorithm will have to perform to determine the third side. Is the calculation different for different angles? Are there special cases that should be tested? The algorithm should be thorough and take these considerations into account. Submit your algorithm on September 9. In the next class meeting (September 16), you will write a C++ program from the algorithm. Remember this when you are designing the algorithm. Don't forget to identify inputs and outputs! List or map the simple steps that must be taken with the user's input to produce the program's output. Your algorithm should have all the information and instructions the program needs except the code itself!Explanation / Answer
Hi, we can use law of cosines to solve this, according to the law the third side is given by,
c^2= a^2+b^2- 2abcos(C) where a,b,c are sides and C is included angle.here is the complete runnable code in c++ as requested along with comments.
#include <iostream>
#include <math.h>
using namespace std;
#define PI 3.14159265
int main() {
int a,b,c;
a=3;
b=4;
double angle=90.0; // defining angle
double temp= a*a+b*b-2*a*b*cos(angle*PI/180.0); // law of cosines
if(temp<0) // if less than 0 invalid input
cout<<"such triangle doesnt exist";
else
cout<<sqrt(temp);
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.