Question

(55 pts) Write a class Triangle that provides getSides, which returns the lengths of the three sides, and getAngles, which returns the degrees of the three internal angles. Then write a main function that randomly generates an array of triangles and finds the largest triangles first on the basis of area and then on the basis of perimeter

Program in C++ with sample shots.

Thank You.

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

#include<iostream>

#include<cmath>

#include<cstdlib>

using namespace std;

class Triangle{

   

    // store the length of sides

    double a;

    double b;

    double c;

   

    // store the angle opposite to corresponding sidex

    double A;

    double B;

    double C;

   

    public:

           

        Triangle()

        {

            this->a = 0.0;

            this->b = 0.0;

            this->c = 0.0;

           

            this->A = 0.0;

            this->B = 0.0;

            this->C = 0.0;

        }

       

        // constructor

        Triangle(double a, double b, double c)

        {

            this->a = a;

            this->b = b;

            this->c = c;

           

            // call function to calculate angles

            calculateAngles();

        }

       

        // function to calculate angles using cosine formula

        void calculateAngles()

        {

            //           ( b^2 + c^2 - a^2 )

            // cos(A) = ----------------------

            //                  2a

            this->A = acos( ( pow( this->b , 2 ) + pow( this->c , 2 ) - pow( this->a , 2 ) ) / ( 2.0 * this->a ) ) * 180.0 / 3.14;

            

            //           ( a^2 + c^2 - b^2 )

            // cos(B) = ----------------------

            //                  2b

            this->B = acos( ( pow( this->a , 2 ) + pow( this->c , 2 ) - pow( this->b , 2 ) ) / ( 2.0 * this->b ) ) * 180.0 / 3.14;

           

            //           ( a^2 + b^2 - c^2 )

            // cos(C) = ----------------------

            //                  2c

            this->C = acos( ( pow( this->a , 2 ) + pow( this->b , 2 ) - pow( this->c , 2 ) ) / ( 2.0 * this->c ) ) * 180.0 / 3.14;

        }

   

        // get the sides of triangle

        double* getSides()

        {

            // array to store the sides

            double *sides = new double[3];

           

            sides[0] = this->a;

            sides[1] = this->b;

            sides[2] = this->c;

           

            return sides;

        }

   

        // get the sides of triangle

        double* getAngles()

        {

            // array to store the angles

            double *angles = new double[3];

           

            angles[0] = this->A;

            angles[1] = this->B;

            angles[2] = this->C;

           

            return angles;

        }

       

        double getArea()

        {

            // calculate semi perimeter

            //       a + b + c

            // s = --------------

            //           2

            double s = ( this->a + this->b + this->c ) / 2.0;

           

            // calculate area using heron formula

            double area = sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) );

           

            return area;

        }      

       

        double getPerimeter()

        {

            // perimeter is sum of all sides

            return this->a + this->b + this->c;

        }

       

        void display()

        {

            cout<<"Sides : ("<<this->a<<" , "<<this->b<<" , "<<this->c<<" )\n";

            cout<<"Angles : ("<<this->A<<" , "<<this->B<<" , "<<this->C<<" )\n";

            cout<<"Area : "<<this->getArea()<<endl;

            cout<<"Perimeter : "<<this->getPerimeter()<<endl<<endl;

        }

};

int main()

{

    // genarate sides ranomly in range 1-20

    int a = rand() % 10 + 1;

    int b = rand() % 10 + 1;

    int c = rand() % 10 + 1;

   

    Triangle ob1(a , b , c);

   

    ob1.display();

   

    // genarate sides ranomly in range 1-20

    a = rand() % 10 + 1;

    b = rand() % 10 + 1;

    c = rand() % 10 + 1;

   

    Triangle ob2(a , b , c);

   

    ob2.display();

   

    if( ob1.getArea() > ob2.getArea() )

        cout<<"Area of ob1 is more.\n";

    else

        cout<<"Area of ob2 is more.\n";

       

    if( ob1.getPerimeter() > ob2.getPerimeter() )

        cout<<"Perimeter of ob1 is more.\n";

    else

        cout<<"Perimeter of ob2 is more.\n";

       

    return 0;

}

Sample output

Sides: (2, 8, 5) Angles : (nan , nan , nan) Area: nan Perimeter15 Sides (1, 10, 5) Angles (nan , nan, nan) Area: nan Perimeter: 16 rea of ob2 is more Perimeter of ob2 is more.

The nan is coming because triangle becomes invalid as the sides are generated randomly.

Add a comment
Know the answer?
Add Answer to:
Program in C++ with sample shots. Thank You. (55 pts) Write a class Triangle that provides...
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
  • C++ question-classes ? Implement a Triangle class in C++. The triangle is defined by its three...

    C++ question-classes ? Implement a Triangle class in C++. The triangle is defined by its three side lengths - a, b, and c. The class includes appropriate class constructors and public and private methods that perform the following operations: is_triangle - checks whether the given side lengths form a proper triangle; area - returns the area of the triangle; perimeter - returns the perimeter of the triangle; angle_a, angle_b, angle_c - return the vertex angles (in degrees) opposite to side...

  • JAVA question Interface and Abstract Class (20 pts): There are three types of triangles in terms...

    JAVA question Interface and Abstract Class (20 pts): There are three types of triangles in terms of how many sides are equal: • Equilateral Isosceles Scalene There are three types of triangles in terms of the degrees of interior angles: • Right · Acute . Obtuse Right Isosceles triangle is a right triangle, as well as an isosceles triangle. Triangle 590° Right triangle Isosceles triangle Right isosceles triangle Create a public interface Triangle, add the following method signatures: • double...

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

  • this is what i have so far but it does not work. please help thank you...

    this is what i have so far but it does not work. please help thank you Part 2: Perimeter of a Triangle Write a program named Triangle.java that asks for the lengths of the three sides of a triangle and computes the perimeter if the input is valid.The input is valid if the sum of every pair of two sides is greater than the remaining side. For example, the lengths 3, 4, and 5 define a valid triangle: 3 plus...

  • 1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the are...

    1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the area of a triangle and counts total number of areas program has computed Program should have two functions sfun(): sfun receives the values of a, b and c then computes and returns the value ofs as areafun 0: areafun receives the value of s and computes and return the area of a triangle to main program using below formulae...

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

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

  • Write a C++ program which has a main-driver and creat a polygon class Poly which has...

    Write a C++ program which has a main-driver and creat a polygon class Poly which has an array of n pairs of floats, x[i] and y[i], creat a derived class Triangle,   creat a derived class Quadrilateral class (which you may assume is convex and points given clockwise) You need to compute the area for the two derived classes but use inheritance to compute perimeter in all these classes. Constructors, accessors, mutators, and anything else needed should be written too. Make...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string...

    a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string parameter to initialize the title, a Rectangle object to pass to the Rectangle constructor when creating the canvas_frame object, and a Rectangle object to pass to the Rectangle constructor when creating the canvas object. b. Write a print method that prints the title and prints the perimeter of canvas_frame and the area of canvas - call the methods area and perimeter to get both...

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
ADVERTISEMENT