Question

In: Computer Science

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

Program

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

/* Define the structure Triplet to store the values of the
three sides in the vector */
struct Triplet
{
int side1, side2, side3;
};

/* Definition of the class Triangle to generate and print
all possible pythogorean triples with sides <= 50 */
class Triangle
{
public:

// constructor to assign user-defined values to the
// private member variables
Triangle(int a, int b, int c)
{
x = a;
y = b;
z = c;
}

  
/* isPythagorean() method is used to check if three integer
values satisfy the rules of a pythogorean triple as
defined in the question */
  
bool isPythagorean() const
{
if(z*z == x*x + y*y)
return true;
// the given values form a pythogorean triple
else
return false;
// the given values donot form pythogorean triple
}

// Private member variables for three sides of a triangle.
private:
int x,y,z;
};


int main()
{

// range specifies the upper limit of any value in a pythogorean triple
int range=50;
// pythogorean_triples is a vector of type Triplet
vector<Triplet> pythogorean_triples;
  
/* The below given nested for loop is to get all possible combinations
of x, y, z with values 1 to 50 and check for possible combinations
that result in pythogorean triple */

// start the outer loop with value 1 and upper limit as range
for(int side1=1; side1<=range; side1++){
// the range of side2 starts from side1 and ends with range
for(int side2=side1; side2<=range; side2++){
// the range of side3 starts from side2 and ends with range
for(int side3=side2; side3<=range; side3++){
  

// Create an object of Triangle with the indices of the
// the three for loops getting passed as sides to the constructor
Triangle t(side1, side2, side3);
// isPythagorean() with check if those three values satify the
// condition and return true or false
if(t.isPythagorean())
// push_back method appends the pythogorean triplet
// to the end of the vector.
pythogorean_triples.push_back({side1, side2, side3});
}
}
}
  
// size() returns the number of elements in the vector
int size = pythogorean_triples.size();
// repeat the printing process for each triplet in the vector
for (int i=0; i<size; i++)
{
cout << "( " << pythogorean_triples[i].side1 << ", " << pythogorean_triples[i].side2
<< ", " << pythogorean_triples[i].side3 << " )" << endl;
}
}

Output:


Related Solutions

/* 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...
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)
a) Use the law of sines to solve a triangle with sides a = 9, b...
a) Use the law of sines to solve a triangle with sides a = 9, b = 16, and angle C = 80◦ . b) . Use the law of cosines to solve a triangle with sides a = 8, c = 17, and angle B = 35◦ .
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...
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