473,378 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.     double s1,s2,s3;    //input variables (side lengths)
  7.     double longest,short1,short2;    //sides arranged by their length
  8.     double angle1,angle2,angle3;
  9.     cin >> s1;
  10.     cin >> s2;
  11.     cin >> s3;
  12.     longest=s1;
  13.     short1=s2;
  14.     short2=s3;
  15.     if (longest<s2)        //determining the longest and shorter sides
  16.     {
  17.         longest=s2;
  18.         short1=s1;
  19.         short2=s3;
  20.     }
  21.     if (longest<s3)
  22.     {
  23.         short1=s1;
  24.         short2=s2;
  25.         longest=s3;
  26.     }
  27.     angle1 = acos(((short1*short1)+(short2*short2)-(longest*longest))/(2*short1*short2))*(180/3.14159);
  28.     angle2 = acos(((longest*longest)+(short2*short2)-(short1*short1))/(2*longest*short2))*(180/3.14159);
  29.     angle3 = acos(((longest*longest)+(short1*short1)-(short2*short2))/(2*longest*short1))*(180/3.14159);
  30.  
  31.     if ((angle3+angle2+angle1) != 180)
  32.     {
  33.         cout << "Not a triangle" << endl;
  34.     }
  35.     else
  36.     {
  37.         if (s1 == s2 && s2 == s3)    //two of the sides are equal
  38.         {
  39.             cout << "Equilateral and Acute" << endl;
  40.         }
  41.  
  42.         else if ((short1*short1 + short2*short2) == (longest*longest))    //pythagoras theorem
  43.         {
  44.             cout << "Right Angle" << endl;
  45.         }
  46.  
  47.         else if (s1 == s2 || s2 == s3 || s1 == s3)    //all sides are equal
  48.         {
  49.             if ((short1*short1 + short2*short2) < (longest*longest))    //an angle is just less than 90 degrees
  50.             {
  51.                 cout << "Isosceles and obtuse" << endl;
  52.             }
  53.             else
  54.             {
  55.                 cout << "Isosceles and acute" << endl;
  56.             }
  57.         }
  58.  
  59.         else 
  60.         {
  61.             if ((short1*short1 + short2*short2) < (longest*longest))
  62.             {
  63.                 cout << "Scalene and obtuse" << endl;
  64.             }
  65.             else
  66.             {
  67.                 cout << "Scalene and acute" << endl;
  68.             }    
  69.         }    
  70.     }
  71.     return 0;
  72. }
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?
Mar 7 '08 #1
4 1684
Banfa
9,065 Expert Mod 8TB
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.
Mar 7 '08 #2
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:
http://www.mathopenref.com/scalene.html

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
Mar 7 '08 #3
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. angle1 = acos(((short1*short1)+(short2*short2)-(longest*longest))/(2*short1*short2))*(180/3.14159);
  2.     angle2 = acos(((longest*longest)+(short2*short2)-(short1*short1))/(2*longest*short2))*(180/3.14159);
  3.     angle3 = acos(((longest*longest)+(short1*short1)-(short2*short2))/(2*longest*short1))*(180/3.14159);
  4.  
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.
Mar 7 '08 #4
MACKTEK
40
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+Angle3...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.
Mar 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: lawrence | last post by:
I'm not sure how this is normally done, on a large site, perhaps one running Phorum. Occassionally a thread will have hundreds of entries, perhaps a meg or two worth of data. You won't necessarily...
1
by: Hung Jung Lu | last post by:
Hi, I have been looking into AOP (Aspect-Oriented Programming) for sometime, now. I frankly don't like the syntax of any of the approaches I have seen so far. I am kind playing around with some...
0
by: phil hunt | last post by:
I've just released version 0.4.2 of my Easibox program, which is used to automate the creation of archive files (tar, tar.gz, tgz, tar.bz2 and zip formats), typically for open source projects. ...
2
by: Joachim Bauer | last post by:
I'm using the code below to display a menu that opens when the mouse goes over the main menu item (try it in your browser to understand the behaviour). It uses "position:absolute" and a switch...
24
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
29
by: tele-commuter | last post by:
Hi folks, I want to understand how exactly is an image(compiled c code and loaded into memory) stored in memory. What exactly is a linker script? I work with a lot of c code on a daily...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
5
by: cdietschrun | last post by:
I have a program that I needed to create for a homework that is an Operating System Simulator. It's not as cool as it sounds. When I run it, long now ; srand( system( "time(&now)" ) );...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.