0

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.

2
  • what did the debugger say? Commented Oct 21, 2013 at 23:54
  • 1
    Please note = is for variable declaration and == for comparison/conditions Commented Oct 22, 2013 at 0:01

2 Answers 2

1

Perhaps the four statements starting with a[0]==a[j+4] should be using = instead of ==.

Sign up to request clarification or add additional context in comments.

2 Comments

But then why does it give me (99,99,99)
Where it prints out the three values corresponding to the thinnest triangle, please also print out the value of i. Maybe it's 0.
0

switch your assignments to single equal signs = instead of double ones ==.

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]; // <- here,
            a[1] = a[j+5]; // <- here,
            a[2] = a[j+6]; // <- here,
            a[3] = a[j+7]; // <- and here

        }
    //printf("%d\n",a[0]);
    }
    printf("The thinnest triangle is formed by (%d , %d , %d)", a[1], a[2], a[3]);

}

2 Comments

I did, but then if I enter 10 for example, the output is (99,99,99)
You are assigning a[i+1]=99.99; etc. to an array of type int. The array will parse the floats into type int aka 99. I think a safer value for triangles could be 181 or something, since no 2D triangle has more than 180 degrees. If you print out your angles you will see this. I entered 10 into your code and received (99, 99, 99) with angle 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.