0% found this document useful (0 votes)
17 views1 page

Calculate Triangle Area and Sides

This C++ program uses trigonometric functions and formulas to calculate the missing side length and area of an oblique triangle given two sides and the angle between them. It prompts the user to input the known values, applies the law of cosines to find the missing side, and uses the area formula for oblique triangles to output the calculated area.

Uploaded by

Jahannie Sarip
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Calculate Triangle Area and Sides

This C++ program uses trigonometric functions and formulas to calculate the missing side length and area of an oblique triangle given two sides and the angle between them. It prompts the user to input the known values, applies the law of cosines to find the missing side, and uses the area formula for oblique triangles to output the calculated area.

Uploaded by

Jahannie Sarip
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

#include <cmath>

using namespace std;

int main()
{
float angle1, distance, side1, side2, angle, cosine, squared, area; //the variables and data type
const float Pl= 3.14159265358979323846; // the pi
cout <<"Find the missing value of one side and the area "<<endl;
cout <<"using the law of cosine and the formula for the area of oblique triangles."<< endl; //A
sentence

//output

cout<<"Enter the value of one side: ";//sentence


cin >> side1;//input number
cout <<"Enter the value of the angle: ";//sentence
cin >> angle;//input number
cout <<"Enter the value of other side: "; //sentence
cin>> side2;//input number

angle1 = (angle* Pl/180); //turning degree into radiant


distance = pow(side1,2) + pow(side2,2)-(2* side1* side2*cos(angle1));//formula of the law of
cosine
squared = sqrt(distance); // square rooted the number with the square root library function
area = (0.5* side1* side2* sin(angle1)); //formula of area for oblique triangles
cout <<"The distance of it is: "<<squared<<","<<endl;//output
cout <<"The area of the triangle is: "<<area<<",";//output

Common questions

Powered by AI

Using float data types implies that the program can handle fractional values and is suitable for calculations requiring precision up to about 7 decimal places. However, float may introduce rounding errors in very precise calculations or situations requiring greater precision, such as double. Therefore, choosing float balances efficiency and sufficient precision for typical trigonometric problems, though developers should be aware of its limitations in high-precision contexts .

The use of 'sin(angle1)' in the area calculation formula '(0.5 * side1 * side2 * sin(angle1))' indicates that the triangle is oblique, meaning it does not contain a right angle. The sine function relates the angle and area, reflecting how the area is directly proportional to the sine of the angle between the two sides. This helps in accurately determining the area when direct perpendicular dimensions are not available .

Potential errors include lack of input validation and absence of error handling, which could cause inaccurate calculations or crashes with invalid inputs. The design could be improved by adding checks to ensure inputs are positive numbers within realistic ranges and implementing exception handling to manage and report errors gracefully. Furthermore, enhancing precision with double data types for critical calculations could increase reliability .

The program demonstrates real-world applications of trigonometric principles by calculating triangle side lengths and area, tasks commonly found in fields like engineering and architecture. By utilizing the law of cosines and sine area formula, it shows how mathematical theory translates into algorithms solving practical geometrical challenges, essential in design and structural analysis .

The 'const' keyword is used to declare 'Pl' as a constant, which means its value cannot be altered during program execution. This is advantageous because it prevents accidental modification of pi's value, which could lead to incorrect calculations as pi is fundamental for trigonometric computations .

The program uses the law of cosines to calculate the length of the missing side of the triangle. The formula 'distance = pow(side1,2) + pow(side2,2) - (2* side1* side2*cos(angle1))' is applied, where 'side1' and 'side2' are the known sides and 'angle1' is the angle between them in radians. This formula calculates the square of the missing side, and the program then takes the square root to determine the actual length .

The program's output formatting, as indicated by the lines 'cout << "The distance of it is: " << squared' and 'cout << "The area of the triangle is: " << area', impacts usability by providing direct and clear communication of results. While straightforward, it doesn't offer additional context or formatting features that enhance readability, such as unit of measurement or error messages for invalid operations. Improving formatting could enhance user experience, making it more intuitive and informative for diverse users .

The 'cmath' library is crucial in this program as it provides mathematical functions such as 'pow' and 'sqrt', which are used to compute powers and square roots, respectively. These functions enhance the program's functionality by enabling complex calculations necessary for trigonometric and geometric problem-solving, such as finding the length of the triangle's side and calculating its area .

The program uses 'cin' to capture user inputs for the sides and angle of the triangle, with prompts to guide the user. It assumes that the inputs will be valid numerical values since it does not include input validation or error handling mechanisms. Therefore, the program expects the user to input appropriate float values for the calculations to avoid runtime errors or incorrect output .

The program converts an angle from degrees to radians using the formula 'angle1 = (angle * Pl / 180)', where 'Pl' is the mathematical constant pi. This conversion is necessary for trigonometric calculations because most trigonometric functions in programming languages like C++ expect angles to be in radians rather than degrees .

You might also like