# c++ homework help

Discussion in 'OT Technology' started by Shrug, Feb 17, 2008.

1. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
I'm not really going to post code or ask for code but rather ask for anyones thought on how the pseudocode should go for this problem:

Pythagorean Triples: A right triangle can have sides that are all
integers. A set of three integer values for the sides of a right triangle
is called a Pythagorean triple. These three sides must satisfy the
relationship that the sum of the squares of two of the sides is equal to
the square of the hypotenuse. Find all _unique_ Pythagorean triples for side1, side2 and hypotenuse, all no longer than 500. How many are there?
Note that this problem specifically asks you to exclude the duplicates. For example, if you were to check all unique Pythagorean Triples less than 5 (rather than 500) your output
must be
3 4 5
That is, it must NOT output both (3,4,5) and (4,3,5).

my thought: im pretty sure there needs to be multiple for (or while, whatever) loops for this but im not sure how to go about this problem. i figure the outter loop would have to incriment from 1 to 500 and incorporate the Pythagorean theorem.

can anyone give me some insight, again, not asking for code, jsut ideas.

2. ### TheDarkHorizon\xC0\xFF\xEE

Joined:
Sep 26, 2002
Messages:
2,396
0
Location:
San Francisco, CA
Just skimming this, the most obvious way to me (i.e. not optimized) would be to use three nested for loops.

3. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
i suck at c++ and optimization isnt an issue for me right now

4. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
ok i wrote it up real quick and it looks pretty good except i keep getting a
"expected primary-expression before ')' token " which occurs during the if statement, can anyone help me out?

5. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
#include <iostream>
#include <math.h>
using namespace std;

int main() {
int side1, side2, hypotenuse, counterloops, countertrip;

for ( side1=0; side1 < 500; side1++) {
for ( side2=0; side2 < 500; side2++) {
for ( hypotenuse=0; hypotenuse < 500; hypotenuse++) {
counterloops++;

if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse))) {
cout << side1 << "\t" << side2 << "\t" << hypotenuse"\n";
countertrip++; }
}
}

cout << " The innermost for loops is entered " << counterloops << " times";
cout << " There are " << countertrip << " Pythagorean triples";
return 0;
}
}

6. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
sorry the indentation looks like ass on the forum

7. ### ge0New Member

Joined:
Oct 31, 2005
Messages:
8,398
0
Location:
JERSEY
what line is that error on?

8. ### ge0New Member

Joined:
Oct 31, 2005
Messages:
8,398
0
Location:
JERSEY
This :
if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse))) {

should be this:
if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse)) {

9. ### ShrugDie in a Kimbo fight

Joined:
Oct 18, 2003
Messages:
15,979
0
Location:
Michigan
thx 1 min ill test

Joined:
Oct 31, 2005
Messages:
8,398
0
Location:
JERSEY
ok

11. ### deusexaetheraOT Supporter

Joined:
Jan 27, 2005
Messages:
19,712
0
Use two nested integer loops: one for Side 1 and one for Side 2. Increment each by 1, to a max of 500.
Inside both loops, perform the Pythagorean formula to calculate the hypotenuse. If the hypotenuse is an integer, print out the lengths of all three sides.

12. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
due to floating point rounding error, definitely not the way to approach the problem.

13. ### deusexaetheraOT Supporter

Joined:
Jan 27, 2005
Messages:
19,712
0
That's why you static cast the hypotenuse length, instead of rounding it. ( if (hyp == static_cast<int>(hyp)) {/*do something*/} ... ) You're going to have to truncate it eventually anyway, whether to check if it's an integer or to check if it's equal to another variable that happens to be an integer.

If you use three loops, the program will take 500x longer to run, and the prof will probably penalize him for being so inefficient.

14. ### rparkNew Member

Joined:
Apr 9, 2005
Messages:
1,082
0
Location:
Isla Vista, California

Joined:
Jan 6, 2001
Messages:
2,337