GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Dec-2007, 06:27
urFriend urFriend is offline
New Member
 
Join Date: Nov 2007
Posts: 13
urFriend is on a distinguished road

Arithmetic concept


hello

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  
Old 08-Dec-2007, 07:06
BKaney BKaney is offline
New Member
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 5
BKaney is on a distinguished road

Re: arithmetic concept


Hi:

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  
Old 11-Dec-2007, 17:52
urFriend urFriend is offline
New Member
 
Join Date: Nov 2007
Posts: 13
urFriend is on a distinguished road

Re: arithmetic concept


Question 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:
int main () {

	int a, b, c;

	cin >> a;
	cin >> b;
	cin >> c;

	if ( a < b + c) {
		cout <<"Is Traingle " << endl; } 
	else if ( b < a + c ) {
		cout <<"Is Traingle " << endl;
	}
	else if ( c < a + b ) {
		cout <<"Is Traingle " << endl;
	}

	else 
			cout <<"isn't Traingle " << endl;


return 0;

}

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:
int main () {

	int a, b, c;

	cin >> a;
	cin >> b;
	cin >> c;

	if ( a*a = b*b + c*c) {
		cout <<"Is Right Traingle " << endl; } 
	else if ( b*b = a*a + c*c ) {
		cout <<"Is Traingle " << endl;
	}
	else if ( c*c = a*a + b*b ) {
		cout <<"Is Right Traingle " << endl;
	}

	else 
			cout <<"isn't Right Traingle " << endl;

return 0;

}


?
  #4  
Old 11-Dec-2007, 19:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Arithmetic concept


You 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  
Old 11-Dec-2007, 22:01
BKaney BKaney is offline
New Member
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 5
BKaney is on a distinguished road

Re: Arithmetic concept


In 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:

int main () {

	int a, b, c;

	cin >> a;
	cin >> b;
	cin >> c;

	if ( (a < b + c) && ( b < a + c ) && ( c < a + b ) )  {
                                      cout <<"Is Traingle " << endl;
	}

	else 
			cout <<"isn't Traingle " << endl;


return 0;

}

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:

int main () {

	int a, b, c;

	cin >> a;
	cin >> b;
	cin >> c;

	if ( (a*a = b*b + c*c) || (b*b = a*a + c*c ) || ( c*c = a*a + b*b ) {
		cout <<"Is Right Traingle " << endl;
	}

	else 
			cout <<"isn't Right Traingle " << endl;

return 0;

}


Again, if you have a,b,c in ascending or descending order, you don't have to have all three tests

bkaney
  #6  
Old 12-Dec-2007, 07:19
BKaney BKaney is offline
New Member
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 5
BKaney is on a distinguished road

Re: Arithmetic concept


I 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  
Old 12-Dec-2007, 07:37
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Arithmetic concept


Quote:
Originally Posted by BKaney
if ( (a*a = b*b + c*c) || (b*b = a*a + c*c ) || ( c*c = a*a + b*b ) {
cout <<"Is Right Traingle " << endl;
}

Note that this should be:
CPP / C++ / C Code:
	if ( (a*a == b*b + c*c) || (b*b == a*a + c*c ) || ( c*c == a*a + b*b ) {
		cout <<"Is Right Traingle " << endl;
	}
  #8  
Old 12-Dec-2007, 08:52
BKaney BKaney is offline
New Member
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 5
BKaney is on a distinguished road

Re: Arithmetic concept


Quote:
if ( (a*a == b*b + c*c) || (b*b == a*a + c*c ) || ( c*c == a*a + b*b ) {
cout <<"Is Right Traingle " << endl;
}

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:
if ( (a*a == b*b + c*c) || (b*b == a*a + c*c ) || ( c*c == a*a + b*b ) ) {
		cout <<"Is Right Triangle " << endl;

 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:59.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.