So I was making a program that computes Pythagorean triples, as long as c is lower than the number entered by the user. So I used 3 while loops to accomplish this. What I also want to accomplish is print to the screen, the set of triples that has the thinnest interior angle, has to be a right-angled triangle. Basically, I calculated using the sine law the smallest angle for each of the triples, and then stored that smallest in an array, and right after it the three indexes of the array represent its corresponding triples. Then I made a method to compare each angle from the triples and if one was greater to store it in the initial four spots of the array. I am currently not worrying about the array size and have declared it as 9999. So the problem is that when I compare more than 1 set of triples, the program does not make the 1st set of indexes in the array equal to the triple with the smallest angle. I agree that the procedure that I have used is very inefficient and time consuming, but if you could give me some sort of solution or even guide me in the right direction I'd appreciate it. Thanks. Oh and here is my code,
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
static int a[9999];
int main(void)
{
int side1, side2, hyp, num;
int i = 0;
int j;
side1 = 1;
hyp = 0;
printf("Please enter a number\n");
scanf("%d", &num);
while (side1 < num) {
side2 = 1;
while (side2 < num) {
hyp = 1;
while (hyp < num) {
if (side1 * side1 + side2 * side2 == hyp * hyp && side1 < side2) {
printf("The side lengths are %d,%d,%d\n", side1, side2, hyp);
float angle1 = (asin((float) side1 / hyp) * (180 / PI));
float angle2 = (asin((float) side2 / hyp) * (180 / PI));
if (angle1 > angle2) {
a[i] = (int)angle2;
a[i + 1] = side1;
a[i + 2] = side2;
a[i + 3] = hyp;
} else if (angle2 > angle1) {
a[i] = (int)angle1;
a[i + 1] = side1;
a[i + 2] = side2;
a[i + 3] = hyp;
} else {
a[i] = (int)angle1;
a[i + 1] = side1;
a[i + 2] = side2;
a[i + 3] = hyp;
}
i=i+4;
}
hyp++;
}
side2++;
}
side1++;
}
a[i+1]=99.99;
a[i+2]=99.99;
a[i+3]=99.99;
a[i+4]=99.99;
compare(i);
return (0);
}
void compare(int i)
{
int j;
for(j=0;j<i;j=j+4)
{
if (a[0]>a[j+4])
{
a[0]=a[j+4];
a[1]=a[j+5];
a[2]=a[j+6];
a[3]=a[j+7];
}
//printf("%d\n",a[0]);
}
printf("The thinnest triangle is formed by (%d , %d , %d)", a[1], a[2], a[3]);
}
Oh one more thing, the reason I have made some indexes 99.9 is so that when the loop is checking and it is not the last triple, I do not want an error,since the loop will have nothing further to compare the previous triples to. Ok I changed it to one equal sign, but now the output is always 99,99,99.
=is for variable declaration and==for comparison/conditions