Question

Write a C++ program to analyze a variety of triangles. The program should determine all angles...

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 below. 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<a+c,   c< a+b

Please following what the requirements are. Also do not use float for an identifier please. These are the other requirements.

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:

o  Please enter the angle: 30 d

o  Please enter the angle: 30 D

o  Please enter the angle: 1.5 r

o  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;

·   After performing the calculations for any of the three menu choices, display the values for all three sides and all three angles (in degrees).

o  Use 2 digits after the decimal point for all angles.

o  Use 3 digits after the decimal point for all sides.

o  Include the unit degrees after all angles.

o  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.

Any help would be greatly appreciated, thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable C++ Code -:

#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define pi 3.14


double degre(double rdn)
{
   return (rdn*180)/pi;
}

double rdn(double degre)
{
   return (degre*pi)/180;
}

int main()
{
   int choice;
   cout << "1)Given two sides and the angle between\n 2) Given two angles and one side\n 3) Given three sides\n";
   cin >> choice;
  
   double a,b,c,A,B,C;
  
   switch(choice)
   {
       case 1:
           {
               char type1;
               cout << "enter two sides \n";  
               cin >> b >> c;
               cout << "a angle with type\n";
               cin >> A >> type1;
              
               if(type1 == 'r' || type1 == 'R')
               {
                   if(degre(A)<0 || degre(A)>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
               }
               else if(type1 == 'd' || type1 == 'D')
               {
                   if(A<0 || A>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
                   A=rdn(A);
               }
               else
               {
                   cout << "bad inputs\nprogram terminated\n";
                   exit(0);
               }
              
               if(b<0 || c<0)
               {
                   cout << "bad inputs\nprogram terminated\n";
                   exit(0);
               }
               a=sqrt(b*b+c*c-2*b*c*cos(A));              
               B=acos((a*a+c*c-b*b)/(2*c*a));
               C=acos((a*a+b*b-c*c)/(2*b*a));
              
               break;
           }
       case 2:
           {
               char typeA,typeB;
               cout << "enter angle A\n";
               cin >> A >> typeA;
              
               cout << "enter angle B\n";
               cin >> B >> typeB;
              
               if(typeA == 'r' || typeA == 'R')
               {
                   if(degre(A)<0 || degre(A)>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
               }
              
               if(typeB == 'r' || typeB == 'R')
               {
                   if(degre(B)<0 || degre(B)>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
               }
               if(typeA == 'd' || typeA == 'D')
               {
                   if(A<=0 || A>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
                   A=rdn(A);
               }
               if(typeB == 'd' || typeB == 'D')
               {
                   if(B<=0 || B>=180)
                   {  
                       cout << "bad inputs\nprogram terminated\n";
                       exit(0);  
                   }
                   B=rdn(B);
               }
              
              
               cout << "which side you would like to enter (a, b, or c)\n";
               char side;
               cin >> side;
               if(side=='a')
               {
                   cin >> a;
                   C=pi-A-B;
                   c=sin(C)*a/sin(A);
                   b=sin(B)*a/sin(A);
                  
               }
               else if(side=='b')
               {
                   cin >> b;
                   C=pi-A-B;
                   a=sin(A)*b/sin(B);
                   c=sin(C)*b/sin(B);
               }
               else if(side=='c')
               {
                   cin >> c;
                   C=pi-A-B;
                   a=sin(A)*c/sin(C);
                   b=sin(B)*c/sin(C);
               }
               else
               {
                   cout << "bad inputs\nprogram terminated\n";
                   exit(0);
               }
               break;
           }
       case 3:
           {
              
               cout << "enter three sides \n";  
               cin >> a >>b >> c;
              
               if(a <= 0 || b <= 0 || c <= 0 || a >= b+c || b>=a+c || c>= a+b)
               {
                   cout << "bad inputs\nprogram terminated\n";
                   exit(0);
               }
               A=acos((b*b+c*c-a*a)/(2*c*b));
               B=acos((a*a+c*c-b*b)/(2*c*a));
               C=acos((a*a+b*b-c*c)/(2*b*a));
              
               break;
           }
   }
   std::cout << std::fixed;
std::cout << std::setprecision(3);
   cout << "side a=" <<a << endl;
   cout << "side b=" <<b << endl;
   cout << "side c=" <<c << endl;
   std::cout << std::fixed;
std::cout << std::setprecision(2);
   cout << "angle A=" <<degre(A )<< " D"<< endl;
   cout << "angle B=" <<degre(B )<< " D" << endl;
   cout << "angle C=" <<degre(C )<< " D" << endl;
  
}

Sample Outputs -:

For Case -1

for case 2 -

For Case 3:

Formula Used -:

1 - a2 = b2 + c2 − 2bc cosA

2 - sin B / b = sin A / a = sin C/c

3 - A+B+C=3.14 (in radian)

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to analyze a variety of triangles. The program should determine all angles...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to analyze a variety of triangles. The program should determine all angles...

    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): 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...

  • Write a program to compute the area of a triangle using side-angle-side method and reports the...

    Write a program to compute the area of a triangle using side-angle-side method and reports the area of that triangle (rounded to 2 decimal places). Side-angle-side formula: ???? = 1/ 2 ?? sin(?), where a and b are two sides of the triangle, and C is the included angle. Your program must meet the following criteria to receive full marks: • Randomly generate two values between 5 and 10 (inclusive) for two sides a and b of the triangle, respectively....

  • 2. Write a program that prompts a user to enter the lengths of sides and angles...

    2. Write a program that prompts a user to enter the lengths of sides and angles for the two triangles described below. The program should calculate the length of the side of the triangle indicated below. Print the two answers to 3 decimal places onto the console screen Triangle 1 Read in the value of the angle, alpha, and the length, a, of the side indicated in the picture below. Calculate the length of the hypotenuse, c, of this right...

  • c++ 17 please help!! A triangle can be of three types-one in which all the sides...

    c++ 17 please help!! A triangle can be of three types-one in which all the sides are equal, or one in which only two sides are equal, or one in which all the three sides are unequal. You are assigned to write a program that allows the user to enter the three sides of a triangle. The program should use three double variables to store the three sides of the triangle. The program should also be able to print the...

  • Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths...

    Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths of its sides using Heron's Formula. Here is an outline: Get the three side lengths from the user (which might have decimal values): a, b, c. Check to ensure that the sides are valid for a triangle. Importantly, the sum of the lengths of any two sides must be larger than the length of the third side (you must check all three sides this...

  • Your math professor asked you to determine whether triangles are equilateral, isosceles, or scalene triangles. You...

    Your math professor asked you to determine whether triangles are equilateral, isosceles, or scalene triangles. You need to write a MATLAB program that accepts the side lengths for sides A, B, and C of a triangle and compares the side lengths in order to determine if the triangle is equilateral, isosceles, or scalene. Each triangle will only be classified as a single type based on the definitions below: 1. An equilateral triangle is a triangle in which all three sides...

  • help im alittle lost my teacher posted all this its suppose to be in C++ language....

    help im alittle lost my teacher posted all this its suppose to be in C++ language. can yoh leave comments ans type the code thank you Write a C++ program that accepts the lengths of three sides (a,b,c) of a triangle as input from the user Validate the user input, so that sides a, b, c can only be POSITIVE. The program output should indicate whether or not the triangle is an Equilateral Triangle, a Right Triangle, Isosceles which is...

  • The program should be in C programming The law of sines is an equation relating the...

    The program should be in C programming The law of sines is an equation relating the lengths of the sides of an arbitrary triangle to the sines of its angles. Such that if we have the following triangle: sin a sin ß sin y A B C B Write a program that takes the values of angle a, angle B and length of A from a user. These input values are passed to a function find which calculates the angle...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • C code. Write a program to find all the triangles with integer side lengths and a...

    C code. Write a program to find all the triangles with integer side lengths and a user specified perimeter. Perimeter is the sum of all the sides of the triangle. Integers a, b and c form a triangle if the sum of the lengths of any two sides is greater than the third side. Your program should find all the triples a, b and c where a + b + c is user specified perimeter value. Print each triple only...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT