So, I'm trying to write a program that goes through all possible combination of side lengths for three sides of a triangle and prints those that follow the Pythagorean theorem (i.e. sideA (sqr) + sideB (sqr) = hypotenuse (sqr)). Those that match must print. However, mathematics does not do as it should. The problem, I think, is how I set up my loops.
Then my question is: how do I configure this? Since I got the impression that for loops it runs like this:
The outermost loop checks if the pythagorean condition is satisfied. If so, it again prints the result, increments, and tests. When the condition is not fulfilled, it proceeds to the second cycle, in which the same process is repeated, and at this moment the second cycle goes to the third, the most internal for the cycle. There it will increase to 500, test and print each applicable result, and as soon as this is done, it will go back to the middle cycle. The middle cycle increases, the tests are repeated again, and as soon as the condition is not checked, the code goes into the innermost cycle, and the process repeats, only this time the middle cycle variable changes (i.e., new sets of numbers are tested.) The whole process repeats until until the cycle of the 2nd cycle increases to 500, and at that moment it moves to the outer cycle,and the process will be repeated. The outermost loop increases until the condition is no longer satisfied, then it is transferred to the middle loop, and the two inner loops will go through their procedure again only this time with the changed outer loop variable (i.e., check the new features again).
Any help on this would be appreciated. I would like to move on, but I feel that this concept of a nested loop is very important for understanding, and I do not want to sell myself on this.
int main(void) {
int sideA = 1;
int sideB = 1;
int hypotenuse = 1;
for (hypotenuse = 1; hypotenuse <= 500; hypotenuse++) {
if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
}
else {
for (sideA = 1; sideA <= 500; hypotenuse++) {
if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
}
else {
for (sideB = 1; sideB <= 500; sideB++) {
if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
}
}
}
}
}
}
}