473,385 Members | 1,570 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,385 software developers and data experts.

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 16240
weaknessforcats
9,208 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,065 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,065 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,065 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,065 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

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

Similar topics

9
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
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
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
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
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
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
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
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.