The program does the opposite of what it's supposed to do...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manticure
    New Member
    • Mar 2007
    • 10

    #1

    The program does the opposite of what it's supposed to do...

    This program, basically asks user for the lengths of 3 sides of a triangle, checks if it is actually a triangle, and if so it tells you what type of triangle it is:
    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main()
    {
    	double s1,s2,s3;	//input variables (side lengths)
    	double longest,short1,short2;	//sides arranged by their length
    	double angle1,angle2,angle3;
    	cin >> s1;
    	cin >> s2;
    	cin >> s3;
    	longest=s1;
    	short1=s2;
    	short2=s3;
    	if (longest<s2)		//determining the longest and shorter sides
    	{
    		longest=s2;
    		short1=s1;
    		short2=s3;
    	}
    	if (longest<s3)
    	{
    		short1=s1;
    		short2=s2;
    		longest=s3;
    	}
    	angle1 = acos(((short1*short1)+(short2*short2)-(longest*longest))/(2*short1*short2))*(180/3.14159);
    	angle2 = acos(((longest*longest)+(short2*short2)-(short1*short1))/(2*longest*short2))*(180/3.14159);
    	angle3 = acos(((longest*longest)+(short1*short1)-(short2*short2))/(2*longest*short1))*(180/3.14159);
    
    	if ((angle3+angle2+angle1) != 180)
    	{
    		cout << "Not a triangle" << endl;
    	}
    	else
    	{
    		if (s1 == s2 && s2 == s3)	//two of the sides are equal
    		{
    			cout << "Equilateral and Acute" << endl;
    		}
    
    		else if ((short1*short1 + short2*short2) == (longest*longest))	//pythagoras theorem
    		{
    			cout << "Right Angle" << endl;
    		}
    
    		else if (s1 == s2 || s2 == s3 || s1 == s3)	//all sides are equal
    		{
    			if ((short1*short1 + short2*short2) < (longest*longest))	//an angle is just less than 90 degrees
    			{
    				cout << "Isosceles and obtuse" << endl;
    			}
    			else
    			{
    				cout << "Isosceles and acute" << endl;
    			}
    		}
    
    		else 
    		{
    			if ((short1*short1 + short2*short2) < (longest*longest))
    			{
    				cout << "Scalene and obtuse" << endl;
    			}
    			else
    			{
    				cout << "Scalene and acute" << endl;
    			}	
    		}	
    	}
    	return 0;
    }
    Looking at line 31. Basically what it's supposed to do is - if the sum of the three angles is not 180, then it should say it's not a triangle. But it doesn't. However if I change != to == (which doesn't make sense to me) the program works perfectly. Do I misunderstand something?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Manticure
    Looking at line 31. Basically what it's supposed to do is - if the sum of the three angles is not 180, then it should say it's not a triangle. But it doesn't. However if I change != to == (which doesn't make sense to me) the program works perfectly. Do I misunderstand something?
    I doubt this statement is true, while I imagine that it is true that with the != in place it doesn't recognise a correct triangle and always prints out "Not a triangle" and when you change to == it always treats it as a triangle even when it can't be.

    The problem is this angle1, angle2 and angle3 are floating point numbers, this means they hold an approximation of the 3 angles and when you add them together for an actual triangle you will get an approximation to 180 but it wont actually be 180 because of round errors in your calculations.

    You should absolutely never use == or != to test the value of a floating point number and you should be very circumspect about using <, >, <= and >=.

    The thing to do in cases like this is to add printf statements to the program so you can see the actual numbers your program is producing and testing. Then you will have to decided how you are going code the logic of the 180 degree angle test for the triangle given that the sum of those 3 variables is very unlikely to actually be 180.

    Comment

    • Manticure
      New Member
      • Mar 2007
      • 10

      #3
      Originally posted by Banfa
      I doubt this statement is true, while I imagine that it is true that with the != in place it doesn't recognise a correct triangle and always prints out "Not a triangle" and when you change to == it always treats it as a triangle even when it can't be.

      The problem is this angle1, angle2 and angle3 are floating point numbers, this means they hold an approximation of the 3 angles and when you add them together for an actual triangle you will get an approximation to 180 but it wont actually be 180 because of round errors in your calculations.

      You should absolutely never use == or != to test the value of a floating point number and you should be very circumspect about using <, >, <= and >=.

      The thing to do in cases like this is to add printf statements to the program so you can see the actual numbers your program is producing and testing. Then you will have to decided how you are going code the logic of the 180 degree angle test for the triangle given that the sum of those 3 variables is very unlikely to actually be 180.
      When I put == in it the program works PERFECTLY. I tryed many variations of sides and here is some output samples of the program when I change it to ==:
      3
      4
      5
      Right Angle
      Press any key to continue

      1
      1
      80
      Not a triangle
      Press any key to continue

      3
      8
      17
      Not a triangle
      Press any key to continue

      8
      6
      12
      Scalene and obtuse
      Press any key to continue

      15
      13
      25
      Scalene and obtuse
      Press any key to continue

      I entered the values taken from here to check:


      And here is the output samples with the same input and using != sign in the code:
      3
      4
      5
      Not a triangle
      Press any key to continue

      1
      1
      80
      Isosceles and obtuse
      Press any key to continue

      3
      8
      17
      Scalene and obtuse
      Press any key to continue

      8
      6
      12
      Not a triangle
      Press any key to continue

      15
      13
      25
      Not a triangle
      Press any key to continue

      Which totally confuses me

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Interesting but that doesn't invalidate my reply please feel free to act on what I have already written.

        Additionally if you are trying values like 1,1,80 the this section of you calculations which are done before any checks

        [code=c]
        angle1 = acos(((short1*s hort1)+(short2* short2)-(longest*longes t))/(2*short1*short 2))*(180/3.14159);
        angle2 = acos(((longest* longest)+(short 2*short2)-(short1*short1) )/(2*longest*shor t2))*(180/3.14159);
        angle3 = acos(((longest* longest)+(short 1*short1)-(short2*short2) )/(2*longest*shor t1))*(180/3.14159);
        [/code]cause a problem because none of the operands to acos are in the valid range -1 - 1.

        Rather than checking the sum of the angles, which is problematic for the reasons a specify in my first post you should check the length od sides rule for triangles, namely that for a triangle with sides of length s1, s2 and s3 then

        s1+s2 > s3
        s1+s3 > s2
        s2+s3 > s1

        are all true.

        Comment

        • MACKTEK
          New Member
          • Mar 2008
          • 40

          #5
          Banfa is correct... on several levels. Once you start doing very complex math you will almost NEVER get a perfect Integer of 180 degrees by calaculating from Trig... using acomputer or even a calculator.
          Think about it... Pi has no end to its length after the decimal, so how could ANY equation based off Pi really be a true integer unless it canceled out?

          OK, so, lets move on to the next problem:

          Your IF statement logic is saying:
          IF NOT EQUAL to 180 (then print NOT a triangle)
          as we discussed this will almost NEVER be true.
          The best way to prove this is simply print Angle1+Angle2+A ngle3...to the screen.
          You will see, that for a triangle, you will get a number close to 180 (unless your trig formula for the angles is wrong).

          Now, moving on...

          Lets say you change it to IF Sum of angles == 180 then
          What happens here is THAT is never true because its a fractional result, so it only gets CLOSE to 180... therefore, the program moves on the next ELSE statement
          At least one of the else statements COULD CARE LESS if the lines make a triangle.
          All it does is look at lengths... and ASSUME certain triangles are created.

          For example:
          You could type in 5,5,0
          and it would probably tell you its a triangle!

          My advice is take Banfa's advice.

          Comment

          Working...