Thread: right angle triangle help

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    right angle triangle help

    I need to make a program which calculates whether or not a triangle is a right angle triangle based on the input of the 3 sides. heres my code wondering if anyone can point out the errors?
    Code:
    /* This program will calculate wether or not the values entered by a user consitute a right angle triangle*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float a, b, c;
    		int sum;
    
    	printf("please enter your values of length for the sides of your triangle.\n a and b will be the shorter sides while the value you enter for \nc must be the greatest length\n");
    		printf(" enter a:\n");
    			scanf("%f", &a);
    
    		printf(" enter b:\n");
    			scanf("%f", &b);
    
    		printf(" enter c:\n");
    			scanf("%d", &c);
    
    sum = sqrt(a*a+b*b);
    
    	if (sum == c)
    	{
    		printf("your values indicate the triangle is a right angle triangle\n");
    	}
    
    	else
    	{
    		printf("The values entered do not indicate a right angle triangle\n");
    	}
    
    	system ("pause");
    
    	return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    sum is an integer, you are comparing it to a float, unless sqrt(a*a + b*b) is a whole number, it won't be able to store the proper result
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jackalope View Post
    I need to make a program which calculates whether or not a triangle is a right angle triangle based on the input of the 3 sides. heres my code wondering if anyone can point out the errors?
    If by that you mean, can we do a code review:
    • You've spelt "whether" incorrectly.
    • The grammar of your first printf statement is bad.
    • The indentation is bad.
    • system("pause") is not the recommended approach.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code tags: √
    good brace style: √
    verbose comments: √
    accuracy: +-

    I'd like to see it done very "cleanly", using the classic equation: A²+B² = C².
    And leave out the sqrt() function, entirely.

    Maybe:
    if((C*C) == ((A*A)+(B*B)))

    and leave out the sum variable. All variables can be unsigned long if you need that high a range. Because inches to feet is base 12, instead of 10, (not to mention problems with yards, etc.), I'd be very temped to FIRST change every measurement to it's *metric* equivalent distance. Now all your base 10 arithmetic works fine.

    If a measurement is say, 10 ft. 4 inches. I'd first change that into all inches, and then use 2.54 cm to the inch, to convert it to metric. Then convert all the other measurements to cm's, (centimeters), also.

    Now there's no problem comparing floats or doubles (which is a bit of a headache).

    In your comments - good essay style, but it's a program note, not an essay. Keep them short, and to the point, and don't mention the obvious. It's the
    "unusual" code that needs the comments. (Your teacher may differ on this idea, however).
    Last edited by Adak; 10-08-2010 at 12:30 AM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    thanks everyone comments were very helpful especially you adak. I made the necessary changes but im still left with the problem that it always returns that the triangles is not a right angle triangle. I am guessing its because the program wont round anything. Is there a way to make it accept only 2 significant figures? o and about the verbose description it's what was in the examples i was given by the prof so i figured I'd play it safe

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Print out the difference between sum and c, and then give yourself a fudge factor and check for the difference being in the range of your fudge factor.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    I changed it to this. May not be exact but its accurate to within 0.1, so not too shabby.

    Code:
    /* This program will calculate whether or not the values entered by a user consitute a right angle triangle*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float a, b, c;
    		float sum, difference;
    
    	printf("please enter your values of length for the sides of your triangle.\n a and b will be the shorter sides while the value you enter for \nc must be the greatest length\n");
    		printf(" enter a:\n");
    			scanf("%f", &a);
    
    		printf(" enter b:\n");
    			scanf("%f", &b);
    
    		printf(" enter c:\n");
    			scanf("%f", &c);
    
    	sum = sqrt((b*b)+(a*a));
    	difference = c - sum;
    
    	if (difference <= 0.1 )
    	{
    		printf("your values indicate the triangle is a right angle triangle\n");
    	}
    
    	else
    	{
    		printf("The values entered do not indicate a right angle triangle\n");
    	}
    
    	system ("pause");
    
    	return 0;
    }

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just FYI, this fudge factor's proper name is Epsilon. Machine epsilon - Wikipedia, the free encyclopedia
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help making Triangle
    By jensklemp in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2010, 03:59 PM
  2. Clipping a Triangle of Digits
    By towely in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2009, 07:00 AM
  3. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  4. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  5. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM