Right Triangle Program

This is a discussion on Right Triangle Program within the C Programming forums, part of the General Programming Boards category; Hello again! I am having some trouble on this right triangle program I have to write for class. I have ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Right Triangle Program

    Hello again!

    I am having some trouble on this right triangle program I have to write for class. I have it written out, and it compiled/runs just fine. The problem is that it says everything is a right triangle! lol. Not completely sure why, but I know not everything I type in there could possibly be a right triangle. I was wondering if somebody might take a look at what I've got for me to try and figure out what's wrong?

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main()
    {
            double a;
            double b;
            double c;
            double hypotenuse;
    
            /*get lengths from user*/ 
            printf("Insert the length of the hypotenuse, followed by the two\n");
            printf("other sides of the triangle.\n");
            scanf("%lf%lf%lf", &c, &a, &b);
    
            c = sqrt((a * a)+(b * b));
    
    
    if ((c = sqrt((a * a)+(b * b))))
       {
            printf("This is a right triangle");
       }
    
    else
       {
            printf("This is not a right triangle");
       }
    
        return 0;
    
    }
    Sorry in advance if I did something stupid. I'm still really new to this! Thank you for any help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are asking your user to input the legth of the hypotenuse in c...
    Then on the very next line you assign c from a math result.
    Right after that, you recalculate it *again* in your if statement.
    Also the hypoteneuse variable is never used.

    I'm thinking you need to rethink this just a tad...
    Last edited by CommonTater; 10-12-2011 at 07:32 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Oops! Well, if it means anything I meant to remove the first stated Pythagorean theorem after I put it in the if statement. I just forgot to remove it. But yeah, I did define it twice differently with the scanf and if statement. Ok, let me go back into it and see what the heck I did. I'm too tired for this, lol.

    Thank you! I'll post again once I work with it a little more.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    I got it! It's all coming out right, but hopefully the code makes sense.

    Code:
    # include <stdio.h>
    # include <math.h>
    
    int main()
    {
            double a;
            double b;
            double c;
            double sum;
    
            printf("Insert the length of the hypotenuse, followed by the two\n");
            printf("other sides of the triangle.\n");
            scanf("%lf%lf%lf", &c, &a, &b);
    
            sum = sqrt((a * a) + (b * b));
    if (c == sum)
       {
            printf("This is a right triangle");
       }
    
    else
       {
            printf("This is not a right triangle");
       }
    
    return 0;
    
    }

  5. #5
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    You really shouldn't check for exact equality when working with floating point numbers. Read this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Gardhr is right about equality checks with floating point numbers... they are at best an approximation. There's this little troublemaker called "last bit ambiguity" where not all numbers can be accurately represented in floating point formats.

    Try this...
    Code:
    if ( abs(c - sum) < .005)  // adjust the .005 for your allowable margin of error
      Printf(...
    Last edited by CommonTater; 10-12-2011 at 09:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pascal's Triangle Program Only Accurate to 12 Rows
    By quintaessentia in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 04:12 PM
  2. triangle help??
    By unix7777 in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2008, 02:46 PM
  3. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  4. Triangle
    By BB18 in forum C Programming
    Replies: 14
    Last Post: 10-16-2004, 10:59 PM
  5. triangle
    By volk in forum C Programming
    Replies: 2
    Last Post: 01-06-2003, 04:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21