triangles & pythagorean

I really need help with my c++
I need to write 4 functions:
1. findRightTriangles
2. area
3. congruent
4. similar

I think I might have done the findRightTriangle, not sure if its correct though. I am clueless how to do this, any insights or help is appreciated (like how to start on any of these functions; how to go about them).

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include "triangles.h" #include using std::cout; #include using std::vector; #include using std::sort; unsigned long triangle::perimeter() { return s1+s2+s3; } unsigned long triangle::area() { unsigned long sides[3] = {s1, s2, s3}; // TODO: write this function. // Note: why is it okay to return an integer here? Recall that // all of our triangles have integer sides, and are right triangles... return 0; } void triangle::print() { cout << "[" << s1 << "," << s2 << "," << s3 << "]"; } bool congruent(triangle t1, triangle t2) { // TODO: write this function. return false; } bool similar(triangle t1, triangle t2) { // TODO: write this function. return false; } vector findRightTriangles(unsigned long l, unsigned long h) { // TODO: find all the right triangles with integer sides, // subject to the perimeter bigger than l and less than h vector retval; // storage for return value. triangle t1; t1.s1=l; t1.s3=h; for(unsigned long a=0; a< t1.s3; a++) { t1.s2=a; if(a >= t1.s1 && a <= t1.s3 && (((t1.s1*t1.s1)+(a*a)) == (t1.s3*t1.s3))) { retval.push_back(t1); break; } } return retval; } ``````

triangles.h:

 ``123456789101112131415161718192021222324`` ``````#pragma once #include using std::vector; class triangle { public: // member functions: // constructor: triangle(unsigned long a=3, unsigned long b=4, unsigned long c=5): s1(a),s2(b),s3(c) {} unsigned long perimeter(); unsigned long area(); void print(); // prints to standard output // member variables: // integers for the 3 sides: unsigned long s1; unsigned long s2; unsigned long s3; }; vector findRightTriangles(unsigned long l, unsigned long h); bool congruent(triangle t1, triangle t2); bool similar(triangle t1, triangle t2);``````
1) First of all, I need to check with you, 'findRightTriangles' meaning checking if the triangle is a right-angle triangle? If is to check, you can use this formula to check: a^2+b^2 = c^2, where c is the longest side of the triangle.

2) For getting the area of the triangle, you can use Heron's formula. (http://en.wikipedia.org/wiki/Heron%27s_formula)

3) For checking of congruent, just check if the sides of the triangles are equal. This can be done by arranging the length of the triangle by increasing/decreasing order. After arranging, you can compare the lengths of the 2 triangles respectively.

4) For checking of similarity, you can use 'SSS' (side, side, side) test. This can be done, again, by first arranging the length of the triangle by increasing/decreasing order. After arranging, check the ratio respectively.

Hope these help :)
findRightTriangles searches for right triangles satisfying certain conditions:
1. All the side lengths must be integers
2. The triangles must be right triangles
3. The perimeter must be bounded above and below any input paraameters that is, l <= perimeter <= h

I somewhat have the idea in my head of how to do each one, its just.. I don't know how to write a code for each of them

If it's possible, can someone show me an example of how to start making a code for one of the functions
Last edited on
Hi aquaturtle, I think understand what you want. Below is an example, hope it helps.

I have stored the sides of the triangle into a vector. So `vector<int>trianglesides` contains the 3 sides of the triangle. Not too sure if you are familiar with vector.

In addition, to use vector, you will need to include `#include <vector> `.

 ``12345678910111213141516171819`` ``````bool ifRightAngle(vector trianglesides) { sort(trianglesides.begin(), trianglesides.end()); //have to include "#include " if ((pow(double(trianglesides.at(0)),2)+pow(double(trianglesides.at(1)),2)) == pow(double(trianglesides.at(2)),2)) return true; //Is a right angle triangle else return false; //Is not a right angle triangle } bool perimeterCheck(vector trianglesides) { int nSum = trianglesides.at(0)+trianglesides.at(1)+trianglesides.at(2); int nSum_lower = 10; int nSum_upper = 100; if (nSum_lower < nSum && nSum < nSum_upper) return true; //Meet the requirement else return false;//Did not meet the requirement }``````
Topic archived. No new replies allowed.