![]() |
|
#1
|
|||
|
|||
Arithmetic concepthello
I'm soluted alot of exercises easily , but sometimes facing problems in mathematic concepts. look this example : Write a program that reads three nonzero double values and determines and prints whether they could represent the sides of a triangle. I searched about this formula during it decide to the three integers represent the sides of triangle, but didn't find. my school information : area = 1/2 base * height famous rule a^2 = b^2 + c^2 ; who can provide. |
|
#2
|
|||
|
|||
Re: arithmetic conceptHi:
Any three (different) points will form a triangle, unless they happen to all lie on the same line. I wasn't sure from your post if you have just three numbers for the lengths of the sides or if you have actual (x,y) coords of the points, either way: 1.) For three side lengths: If the you have three sides of length a,b and c then to make a triangle each one must be shorter than the other two combined, that is you must have a<b+c, and b<a+c, and c<a+b all at the same time (such as 3, 4, 5). 2.) If you have three points (x1,y1), (x2,y2) and (x3,y3) then you could find the distance a b and c between each pair and apply the test in part 1. The distance between two points is square_root( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ). Also you may recall the formula for the slope of a straight line slope=(y2-y1)/(x2-x1). For a triangle the slopes of the three possible line segments would all be different. If the slopes for the line through any pair of the points is the same, then they all lie on the same line and don't make a triangle. The problem with finding slope is you have to handle the (x2-x1)=0 hence slope=infinity case separately. Hope this helps. Brian Kaney |
|
#3
|
|||
|
|||
Re: arithmetic conceptQuestion ask me about length of three sides of triangle.
so, from ur post I understood to the code will be like next : CPP / C++ / C Code:
but when ask like that : Write a program that reads three nonzero integers and determines and prints whether they could be the sides of a right triangle. is right to apply this formula a^2 = b^2 + c^2 as the previos concept and style like this : CPP / C++ / C Code:
? |
|
#4
|
||||
|
||||
Re: Arithmetic conceptYou need to calculate the length of each side of the triangle and see if they fit the right triangle formula
__________________
Age is unimportant -- except in cheese |
|
#5
|
|||
|
|||
Re: Arithmetic conceptIn order for the three numbers to be sides of a triangle (any triangle, not just a right triangle) then all three of the conditions must be true at the same time. So I would 'and' them together with a single if statement like this:
CPP / C++ / C Code:
What you had would not always give the right answer. For instance if a=2, b=3 and c=20, then your first test a<b+c would be true and your code would output 'Is Triangle'. But since the test c<a+b fails the points cannot form a triangle. [If you were to 'lay' the c=20 side on the ground, there would be no way to attach a 2 side and a 3 side to the two ends and have them meet because they are too short]. Actually if you put the three numbers in ascending order you could get away with only two of the tests, but it's not going to save any time. Now for a right triangle, you are right about the a^2 = b^2 + c^2 formula. What you have will work, although again you could use a single 'if' CPP / C++ / C Code:
Again, if you have a,b,c in ascending or descending order, you don't have to have all three tests bkaney |
|
#6
|
|||
|
|||
Re: Arithmetic conceptI know the right triangle example may be just a learning exercise, but in practice there would be some problems. Being a physics teacher and a programmer I can't resist.
The formula a^2 = b^2 + c^2 is called the Pythagorean Theorm so I'll call it PT for short. The PT has a handful of solutions where all three are intergers, such 3,4,5 or 5,12,13 but these are very 'rare'. For most any right triangle at least one of the three values will be irrational. For instance a=1, b=2, c=square root(5). A computer double can not perfectly represent an irrational number. It can approximate it to a lot of decimal places, but that's not the same as being equals. So if you had a=1, b=2, c= 2.23607 (that square root of 5 to 5 decimal places) it would fail the PT test and not qualify as right triangle. There is the law of cosines that is an extension of PT, it is: a^2 = b^2 + c^2 - 2*b*c*cos(theta) where theta is the angle between side b and c. Now given an a,b,c you can use this to find the three angles that the triangle would have. For a=1, b=2 and c=2.23607 the three angles are 63.43497, 26.56490 and 90.00013. Now that is not exactly a right triangle but it's pretty close. In practice, in programming you might need to decide how closely you want an equation like PT to be satisified and then code it with inequalities such as: side 1 equation <= side 2 equation + 0.000001 and side 1 equation >= side 2 equation - 0.000001 If you just had a program that could generate three random doubles a,b,c the odds that they would satisfy PT is basically zero. Formally in math it is exactly zero. [The set of all a,b,c real number tripletts is a 3-D volume, whereas the set of all solutions to PT is a 2-D ellipsiodal surface (with no thickness) so the 'percentage' of all double tripletts a,b,c that form right triangles as determined by the ratio of the 'volumes' of the two infinite point sets is 0%.] bkaney |
|
#7
|
|||
|
|||
Re: Arithmetic conceptQuote:
Note that this should be: CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: Arithmetic conceptQuote:
Yes, thanks I missed that. The hazards of cutting and pasting. The '=' should have been '=='. And then I notice that I'm also short a closing paranthesis too. How about this: CPP / C++ / C Code:
|
Recent GIDBlog
More photos on Flickr by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| strlen concept question? | Red83 | C Programming Language | 6 | 13-Feb-2006 07:25 |
| Check This New Searchengine Concept Out ! | onauc | Search Engine Optimization Forum | 5 | 10-Feb-2006 10:23 |
| multiple-precision arithmetic package | TimHDG | C Programming Language | 1 | 29-Nov-2004 15:14 |
| Pointer arithmetic | sanman10535 | C Programming Language | 6 | 22-Mar-2004 11:08 |
| modulus arithmetic | ewallo | C++ Forum | 3 | 22-Feb-2004 23:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The