473,505 Members | 15,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Triangle Validity

6 New Member
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 16247
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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,065 Recognized Expert Moderator Expert
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
ginevralupin
6 New Member
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,065 Recognized Expert Moderator Expert
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 Recognized Expert MVP
Bookmark the Wolfram Mathworld site; it's valuable.

kind regards,

Jos
Apr 14 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
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
ginevralupin
6 New Member
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,065 Recognized Expert Moderator Expert
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 Recognized Expert Specialist
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
ginevralupin
6 New Member
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 Recognized Expert MVP
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
pavanhasabnis
1 New Member
please use this condion for if " if (((a+b)<c)||((b+c)<a)||((a+c)<b))"
Sep 9 '12 #14
nikitha vijay
1 New Member
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

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

Similar topics

9
3910
by: coinjo | last post by:
I need to write a program, which takes two inputs: •a value n that represents the number of elementsin the longest row of a triangle. •a character c to be printed in place of each...
16
50398
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hi, Could anyone over here, write a program in C using only for loop to print the following output * *** ***** ******* ********* ***********
2
1927
by: javadkhan | last post by:
Hi All, I am trying to supress the small black triangle that shows up in the menus meaning the menuitem is parent. The reason for that is I drew my own 3D looking triangle in DrawItem using...
1
3320
by: Leo | last post by:
Can someone tell me how to make the sorting triangle on datagrid column header always show up? And which property can change the color of the triangle ? The default color is white. Thanks
5
6591
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
9
1638
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
0
1829
geo039
by: geo039 | last post by:
I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 constructors...
19
8243
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
6
13337
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.