Generate right triangles c++ -
i trying make program generate 3 sides given following input: longest allowed hypotenuse , number of triangles required. sides can integers. program have written hangs on me , not return output. if downvote me explain why.
#include <iostream> #include <cmath> #include <cstdlib> int generator(int number, int hypoth){ int a,b,c; while (number>0){ c=rand()%(hypoth-1)+1; (a=1;a<hypoth-2;a++){ (b=1;pow(a,2)+pow(b,2)<=pow(c,2); b++){ if (pow(a,2)+pow(b,2)==pow(c,2)){ std::cout<<"sides: "<<a<<" "<<b<<" "<<c<<std::endl; number--; } } } } return 0; } int main(){ int triangle_number, hypothenuse; std::cout << "how many triangles generate? "; std::cin >> triangle_number; std::cout << "how long max hypothenuse?"; std::cin >> hypothenuse; generator(triangle_number, hypothenuse); return 0; }
if think should improve algorithm please hint me in right direction. thank time.
the code provided works fine on machine: inputting 1 , 6 gives output sides: 3 4 5
.
however, problem arises line: pow(a,2)+pow(b,2)==pow(c,2)
. pow
returns double
. comparing floating-point numbers equality slippery, , practically never idea, since it's off tiny amount, , false.
replace a*a + b*b == c*c
(and condition within loop above a*a + b*b <= c*c
).