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

Hero's Formula

2
Freinds ,
I have given a project to find out area of a triangle with
the help of Hero's formula.In school we are using turbo c++3.0.
What I did is:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. void main()
  5. {
  6. clrscr();
  7. float a,b,c,area,S;
  8. cout<<"Enter a";
  9. cin>>a;
  10. cout<<"Enter b";
  11. cin>>b;
  12. cout<<"enter c";
  13. cin>>c;   
  14. S=((a+b+c)/2);
  15. if(S<a || S<b||S<c)
  16.   {
  17.    cout<<"Invalid triangle!!!";
  18.   }
  19. else
  20.   {
  21.     area=sqrt(S*(S-a)*(S-b)*(S-c));
  22.    cout<<"The area of triangle is "<<area;
  23.   }
  24. getch(); 
  25. }
  26.  
  27.  
Just wondering If there is any other way also bcoz I doubt what I have used
in declaring a triangle as INVALID is correct or not.
Dec 3 '06 #1
4 5231
Banfa
9,065 Expert Mod 8TB
No it looks right to me.

For a valid triangle with sides a, b and c then

a + b > c
a + c > b
b + c > a

are all true i.e the sum of any 2 sides is greater than the 3rd side.

Put another way 3 lines a, b and can not make a valid triangle if any of

a + b <= c
a + c <= b
b + c <= a

are false

time those by 1/2

1/2(a + b) <= 1/2.c
1/2(a + c) <= 1/2.b
1/2(b + c) <= 1/2.a

and it still holds true, add 1/2.c to the first equation, 1/2 b to the second and 1/2 a to the third

1/2(a + b) + 1/2.c <= 1/2.c + 1/2.c
1/2(a + c) + 1/2.b <= 1/2.b + 1/2.b
1/2(b + c) + 1/2.a <= 1/2.a + 1/2.a

simplify

1/2(a + b + c) <= c
1/2(a + b + c) <= b
1/2(a + b + c) <= a

subsitute 1/2(a + b + c) = S gives and invalid triangle if

S <= c
S <= b
S <= a

which is close to what you have coded but not quite. You code passes as a valid triangle any three lines where a + b = c (or the other 2 combinations), this is not a triangle as it has no height, the 2 shorter lines would lay exactly on top of the longer line. Hero's forumla will calculate the area as 0.

As an example of this try lines 10, 5 and 5.

You can correct your code easily report this invalid case by changing < to <=
Dec 4 '06 #2
sdrsa
2
Thumbs up!!!
Great mind.SO fine.Thnks Mate.
Dec 4 '06 #3
i don't get it so kindly put the exact code pls cuz i really need this...
plsss thnx
Oct 12 '08 #4
JosAH
11,448 Expert 8TB
Google is your friend; it found this page for you.

kind regards,

Jos
Oct 12 '08 #5

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

Similar topics

0
by: RJN | last post by:
Hi I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the formula in the main...
0
by: RJN | last post by:
Hi Sorry for posting this message again. I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be...
0
by: rjn | last post by:
Hi I have a main report in which I have inserted a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the...
3
by: Mike | last post by:
Hi, I have three tables in the following structure (simplified): Table 1: Containing the customers ------------------------------------------------- create table Customers ( int...
11
by: Brian VanPelt | last post by:
I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: barnzee | last post by:
Hi all, newbie here, but having a go I am trying to build a stock watchlist in excel 2007 with a dynamic link to a DDE server (paid for from a broker).There is no add-in or plug-in, I just CTL ALT...
30
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the...
18
by: Chinde | last post by:
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.