CSCI-14 Lab 11 -- direct file output. Due 5/10/16

advertisement
CSCI-14 Lab 11 -- direct file output. Due 5/10/16
Work in groups of two, with a new partner. Do not work alone.
A Pythagorean triple is an ordered triple (a, b, c) that obeys the Pythagorean formula a2 +
b2 = c2. For example, (3, 4, 5) is a Pythagorean triple because 9 + 16 = 25. Write a
function long Pythag( long a, long b ) that takes a pair of integers (a, b)
and looks to see they are part of a Pythagorean triple.
To do this, you need to see if c, the square root of (a2 + b2), is an integer. Calculate c2
then take its square root c. If c is in fact an integer (has no fractional part), then (a, b, c)
is a Pythagorean triple, and your function should return c to indicate this. If c has a
fractional part, return -1 to indicate that (a, b) is not part of a Pythagorean triple.
Write a program to try all pairs of positive numbers (a, b) where a > 1, a < b and b <=
500, e.g., you will test (2, 3), but NOT test (3, 2). This can be done by choosing your
loop conditions carefully: you DO NOT have to test a < b inside the loops. Print out the
triples three triples to a line, in the format ( a, b, c ). That is, parentheses around
the triples, spaces around the numbers (and the comma that goes immediately after the
first two numbers in the triple), and tabs between the triples.
This program's output will be much too long to fit on a screen, so use direct file output
for this.
Download