First off thank you for quick help! I can't believe I didn't find this forum sooner, as it appears to be a great resource for me!
I think I kind of get it, in the sense that since its a^2 + b^2 that a+/- some value will equal b and that will equal c^2.
However, when the loop runs, I got a constant stream of changing numbers for a, and a consistent 1 & 2 for b & c.
Questions I have: why am I multiplying pow(a, 2) by 2 in the first loop? that would give me 2a^2 wouldn't it?
What the first loop is basically saying is:
a = 1, when 2*a^2 is less than or equal to limit^2; increase a
where the second loop reads as:
x = 0, when a^2 +(a+x)^2 are less than or equal to limit^2, increase x.
So this will increment the values at different times in the loop.
So then why is it that I get this stream of: |random number||1||2|
for my cout values? Also how would I make it stop, do I need a break statement or should it stop automatically once it tries to surpass the limit set in the loop?
I guess I'm less adept than I thought :/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int x = 0;
int sides = 0;
int a = 1;
int b = 1;
int c = 2;
int limit = 0;
cout << "Enter the upper limit for the sides of a right triangle: ";
cin >> limit;
cout << "Side 1 Side 2 Side 3" << endl;
do {
for (int a = 1; 2 * pow(a, 2) <= pow(limit, 2); a++){
for (int x = 0; pow(a, 2) + pow(a + x, 2) <= pow(limit, 2); x++){
cout << a << " " << b << " " << c << endl;
}
}
} while (c <= limit);
return 0;
}
|