Question

In: Computer Science

/* A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is...

/*
A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50.
For this you should first implement the following Triangle class and its methods. Then use it in main() to find all pythagorean triples and put then in a std::vector. Finally print them out as triples, like
(3, 4, 5)
.....
Make sure your output contains a triple only once, and not any reordered ones, this means (3, 4, 5) is the same as (4, 3, 5) and so only the increasing order triple should be printed out. This means only (3, 4, 5) shoud be printed.
You program should print out all possible pythogorean triples with sides <= 50.
*/
class Triangle
{
   public:
   Triangle(int a, int b, int c) // constructor. Implement this
   {}
  
   bool isPythagorean() const // implement this properly
   {
       return false;
   }
   private: // what do you put in private section?
}

int main()
{
}

Solutions

Expert Solution

#include<iostream>
#include<vector>
#include<tuple>
using namespace std;

class Triangle
{
        /* private section of class has three sides of triangle*/
        int side1;
        int side2;
        int side3;
public:
        /* parametrized constructor to initialize sides of triangle*/
        Triangle(int a, int b, int c)
        {
                side1 = a;
                side2 = b;
                side3 = c;
        }
        /* function to check if three sides are forming pythagorean triplet or not*/
        bool isPythagorean() const
        {
                if ((side3 * side3) == ((side2 * side2) + (side1 * side1))) /* if side3^2 = side1^2 + side2^2  then it is a pythagorean triplet */
                        return true;

                return false;
        }
};

int main()
{
        vector<tuple<int, int, int> > v; //declare vector v of tuples
        for (int i = 1; i <= 50; i++)   /* three loop to generate all the possible pairs of sides */
        {
                for (int j = i + 1; j <= 50; j++)
                {
                        for (int k = j + 1; k <= 50; k++)
                        {
                                Triangle obj(i, j, k);  /* create object of triangle class with three sides i,j,k*/
                                if (obj.isPythagorean())  /* check if three sides form right triangle, if yes add tuple to vector*/
                                {
                                        v.push_back(make_tuple(i, j, k));
                                }
                        }
                }
        }
        
        cout << "The Pythagorean triplets are :: " << endl;
        for (int i = 0; i < v.size(); i++)  /* print all the tuples on console */
        {
                cout << "(" << get<0>(v[i]) << "," << get<1>(v[i]) << "," << get<2>(v[i]) << ")"<<endl;
        }

        return 0;
}


Related Solutions

use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5,...
use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50. For this you should first implement the following Triangle class and its methods. Then...
a) The triangle △ ABC is right-angled with right angle at corner C and angle α...
a) The triangle △ ABC is right-angled with right angle at corner C and angle α at corner A. Calculate a = | BC |, given that c = | AB | = 8, and that tan α = 12. Calculate a= ? b) In the triangle △ ABC before the designations a = | BC |, b = | CA |, c = | AB |, and ∠A = α, ∠B = β and ∠C = γ. Find c,...
Suppose that A is a triangle with integer sides and integer area. Prove that the semiperimeter...
Suppose that A is a triangle with integer sides and integer area. Prove that the semiperimeter of A cannot be a prime number. (Hint: Suppose that a natrual number x is a perfect square, and suppose that p is a prime number that divides x. Explain why it must be the case that p divides x an even number of times)
Find a b c of a right triangle. with a perimeter of 18.
Find a b c of a right triangle. with a perimeter of 18.
Three charges are arranged so that each is at the vertex of a 3-4-5 right triangle....
Three charges are arranged so that each is at the vertex of a 3-4-5 right triangle. the charge opposite the short side is Q1 = -11 nC. The charge opposite the long side is Q2 = 5 nC. The charge opposite the hypotenuse is Q3 = 16 nC. The hypotenuse is H = 0.45. 1) What is the magnitude of the electric force on Q3 due to Q1? Q3 due to Q2? Q3 due to Q1 and Q2? 2) What...
triangle ABC is a right-angled triangle with the size of angle ACB equal to 74 degrees.
 triangle ABC is a right-angled triangle with the size of angle ACB equal to 74 degrees. The lengths of the sides AM, MQ and QP are all equal. Find the measure of angle QPB.
3 charges, 8 µC each, are located on three vertices A, B, C of an equilateral triangle with sides 1 cm each.
  9). 3 charges, 8 µC each, are located on three vertices A, B, C of an equilateral triangle with sides 1 cm each. Another charge q is located at the mid point D of the side BC. Calculate q in micro Coulomb so that net force on the charge at A due to the charges at B, C and D is zero. 10). In a right angle triangle ABC, angle ABC is 90 Degree, AB = 2 m, and...
5.11 LAB: Drawing a right triangle c++ This program will output a right triangle based on...
5.11 LAB: Drawing a right triangle c++ This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as...
ABC is an isoceles right-angled triangle in which ∠A = 90°. Find ∠B and ∠C.
ABC is an isoceles right-angled triangle in which ∠A = 90°. Find ∠B and ∠C.
Three point charges are placed at the corners of a right-angle triangle, as shown in the...
Three point charges are placed at the corners of a right-angle triangle, as shown in the figure. The masses, charges and coordinates of the three objects are given as follows: Mass (g): Charge (μC): Coordinate (mm): ?1 = 2.30 ?1 = −1.25 ?1 = (0; 6.00) ?2 = 0.15 ?2 = +0.55 ?2 = (0; 0) ?3 = 1.50 ?3 = −2.05 ?3 = (4.00; 0) (a) Determine the coordinate of the centre of mass of the system. (b) Calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT