468,457 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Triangle Validity

Puhleeeease check this .......
Isnt giving the correct output ...
Expand|Select|Wrap|Line Numbers
  1. int a,b,c;
  2.  
  3.     printf("Enter the sides...");
  4.      scanf("%d %d %d",&a,&b,&c);
  5.  
  6.     if (a<(b+c))
  7.                 printf("The triangle is valid.");
  8.     else if (b<(c+a))
  9.                     printf("The triangle is valid.");
  10.     else if (c<(a+b))
  11.        printf("The triangle is valid.");
  12.     else
  13.        printf("The triangle is invalid.");
  14.  
  15.  
Apr 13 '08 #1
14 15868
weaknessforcats
9,207 Expert Mod 8TB
Nested if statements are an OR condition.

If your triangle is a= 10, b= 4 and c = 5:

then a < (b+c) is false. This is an invalid triangle. However the else goes onto to check b < (c+a) which is true. And so your program displays that the triangle is valid.

Once you have detected an invalid triangle, your program should stop and not continue on.

There are various ways to code this and I leave that up to you.
Apr 13 '08 #2
oler1s
671 Expert 512MB
Puhleeeease check this
For what? Don't give us a code dump and set us off with "find the errors". We can do bug hunting on our own code, thanks. Ask a real question.

Isnt giving the correct output ...
So maybe you should program it to give the correct output. If you don't give us anymore details than "it is not correct", than we won't give you a response any better than "make it correct".

Details please. What do you observe? What did you try? Where did you narrow the problem down to? Don't dump work on us with little information.

EDIT: As a hint, try to use braces more explicitly. It makes it clear what the blocks of code are. This is especially true in if/else one liners, which when nested without the braces can become confusing to read. You don't know what else matches to what if.
Apr 13 '08 #3
Banfa
9,051 Expert Mod 8TB
You may also wish to tell us, in English/Maths, the algorithm you think you are solving. I can pretty much guarantee that all the experts/mods that have posted so far know the algorithm for detecting a valid triangle but from what you have posted it is not clear if you
  1. Know the algorithm to detect a valid triangle but have implemented it incorrectly
  2. Do not know the algorithm to detect a valid triangle but have implemented the algorithm you think it should be correctly
  3. Do not know the algorithm to detect a valid triangle but have implemented the algorithm you think it should be but implemented that incorrectly

And before you laugh I have seen c happen in commercial projects all be it on a slightly more complex algorithm.
Apr 13 '08 #4
Sorry.

I have to input three sides of a triangle and check them to decide if the triangle is valid or not.
The result has to be displayed .

Another version I had tried is below, but the control never enters the "invalid" part, i always get the "Triangle is valid" message.
I think the problem is with the hierarchy structure for OR ...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6.  
  7. main()
  8.  
  9. {    int a,b,c;
  10.  
  11.     printf("Enter the sides...");
  12.      scanf("%d %d %d",&a,&b,&c);
  13.  
  14.     if ((a<(b+c))||(b<(c+a))||(c<(a+b)))
  15.                 printf("The triangle is valid.");
  16.     else
  17.        printf("The triangle is invalid.");
  18.  
  19.       getch();
  20.       return 0;
  21. }
Any further suggetions???
Apr 14 '08 #5
Banfa
9,051 Expert Mod 8TB
I have to input three sides of a triangle and check them to decide if the triangle is valid or not.
As I already tried to say (but was may be unclear) what algorithm are you using to determine if the 3 sides do or don't form a valid triangle.
Apr 14 '08 #6
JosAH
11,448 Expert 8TB
Bookmark the Wolfram Mathworld site; it's valuable.

kind regards,

Jos
Apr 14 '08 #7
Banfa
9,051 Expert Mod 8TB
Bookmark the Wolfram Mathworld site; it's valuable.
Thank you I have, unfortunately it now appears that I have to spend about 6 months browsing this site so you all on your own for a while :D
Apr 14 '08 #8
The condition is to check
((a<(b+c))||(b<(c+a))||(c<(a+b)))
that is....
sum of any 2 sides is greater than the third side

Will i have to check for the smallest side first and then proceed ?
Apr 14 '08 #9
Banfa
9,051 Expert Mod 8TB
This
((a<(b+c))||(b<(c+a))||(c<(a+b)))
and this
sum of any 2 sides is greater than the third side
are not the same thing, the second one is correct, the first one is not a correct implementation of it.
Apr 14 '08 #10
Ganon11
3,652 Expert 2GB
In order for it to be a triangle, the sum of any two sides must be greater than the third side. In other words, side1 + side2 > side3 for ALL side1, side2, or side3. Does that sound like a condition requiring AND, or a condition requiring OR? You've programmed it with OR (||), and you are getting wrong results - what does that tell you?
Apr 14 '08 #11
Thanks a million,don't know how I never missed it !
Maybe coz i got the algorithm corrected from my prof, but we both didn't notice the error....

Thanks ever so much!
Apr 14 '08 #12
JosAH
11,448 Expert 8TB
Thank you I have, unfortunately it now appears that I have to spend about 6 months browsing this site so you all on your own for a while :D
It's an extremely nice site with quite a history: first Eric Weisstein (sp?) built
that side by himself, he agreed to have a book published (a snapshot of the site)
but later the book publisher forbid him to work on his own site; it was his 'grand
opus' and was stuck because of lawsuits etc. Then Wolfram came in and 'bought'
the entire site including all book rights etc. and now everything is up and running
again.

kind regards,

Jos
Apr 15 '08 #13
please use this condion for if " if (((a+b)<c)||((b+c)<a)||((a+c)<b))"
Sep 9 '12 #14
all the inequalities need to be true so && operator should be used instead of ||
since a+b>c &&
a+c>b &&
b+c>a &&
should be put in if as a condition
if it is true
den valid triangle
else
invalid triangle
Oct 14 '13 #15

Post your reply

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

Similar topics

9 posts views Thread by coinjo | last post: by
16 posts views Thread by VISHNU VARDHAN REDDY UNDYALA | last post: by
9 posts views Thread by Andy Dingley | last post: by
geo039
reply views Thread by geo039 | last post: by
19 posts views Thread by lost1 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.