Right triangles...


D

deanfamily11

Now, I've made a program that when the user enters 3 numbers, it is supposed
to tell the user if it is a right triangle by using the quadratic equation
(a^2 + b^2 = c^2). Now, granted it can be difficult to come up with 3
numbers on the fly that when run through that equation come out true, but
even with using 0's, it should be correct. If there is something wrong with
the following code, I'd appreciate the help, otherwise, tell me some numbers
that will work.


#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int side1, side2, side3;
double sum;

//get sides from the user
cout << "Enter 3 integers representing sides of a triangle: ";
cin >> side1 >> side2 >> side3;

//determine if it is a right triangle
sum = (side1 ^ 2) + (side2 ^ 2);
side3 = (side3 ^ 2);

if (sum == side3)
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}
 
Ad

Advertisements

G

GB

deanfamily11 said:
the following code, I'd appreciate the help, otherwise, tell me some numbers
that will work.
side3 = (side3 ^ 2);

The "^" operator does not perform exponentiation. It performs bitwise
exclusive or.

Gregg
 
D

Dave Townsend

the numbers 3 4 5 and multiple of them will work.
You also need to test all the combinations of the three numbners,
that is, if I enter 5 3 4, you program won't detect a right angle triangle.
 
M

M

Yes, the code assumes that the third number entered is the hypotenuse
of the triangle. (If the sides need to be entered in any order, the
code could just use the largest of the three values as the
hypotenuse.)

But the bigger problem is that in C, the '^' character is not used for
exponentiation; it's the bitwise exclusive or operator.
 
G

Guest

I inserted corrections below:

deanfamily11 said:
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int side1, side2, side3;
double sum;

//get sides from the user
cout << "Enter 3 integers representing sides of a triangle: ";
cin >> side1 >> side2 >> side3;

//determine if it is a right triangle
sum = (side1 ^ 2) + (side2 ^ 2);

"^" is taken from basic. In C you call the function "pow":
sum = pow(side1, 2) + pow(side2, 2);
side3 = (side3 ^ 2);

side3 = pow(side3, 2);
if (sum == side3)

Because floating numbers are never as precise, that equality can always be
guaranteed, even in case were equality is given, you should write
if ( fabs(sum-side3) < 0.01 )
 
J

John Carson

Oliver (Nospam) said:
I inserted corrections below:



"^" is taken from basic. In C you call the function "pow":
sum = pow(side1, 2) + pow(side2, 2);


side3 = pow(side3, 2);


Because floating numbers are never as precise, that equality can
always be guaranteed, even in case were equality is given, you should
write
if ( fabs(sum-side3) < 0.01 )

Given that the user is entering integers, you can test exactly (provided the
numbers are not too big) with:

sum = side1*side1 + side2*side2;
side3 = side3*side3;

if(sum == side3)
//
 
Ad

Advertisements

K

Kai-Uwe Bux

deanfamily11 said:
Now, I've made a program that when the user enters 3 numbers, it is
supposed to tell the user if it is a right triangle by using the quadratic
equation
(a^2 + b^2 = c^2). Now, granted it can be difficult to come up with 3
numbers on the fly that when run through that equation come out true, but
even with using 0's, it should be correct. If there is something wrong
with the following code, I'd appreciate the help, otherwise, tell me some
numbers that will work.


#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int side1, side2, side3;
double sum;

//get sides from the user
cout << "Enter 3 integers representing sides of a triangle: ";
cin >> side1 >> side2 >> side3;

//determine if it is a right triangle
sum = (side1 ^ 2) + (side2 ^ 2);
side3 = (side3 ^ 2);

if (sum == side3)
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}

Since others have already pointed out the bit about ^, I will just remark
that fixing that won't do. Consider

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int side1, side2, side3;
double sum;

//get sides from the user
cout << "Enter 3 integers representing sides of a triangle: ";
cin >> side1 >> side2 >> side3;

//determine if it is a right triangle
sum = (side1 *side1) + (side2 * side2);
side3 = (side3 * side3);

if (sum == side3)
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}


Now, this works fine on ( 0, 0, 0 ) and ( 3, 4, 5 ). But, on my machine, it
fails on ( 0.3, 0.4, 0.5 ). The reason is that you use == for variables of
type double which is not really what you want in cases where the internal
precission kicks in and screws the least significant digits of your
numbers.


Best

Kai-Uwe Bux
 
M

M

Fine, but won't our friend here learn more if we guide him (her?)
toward the answers rather than giving them to him? It just seems to
me that he won't learn very much if we do his homework for him.
 
G

Guest

Clever guy, hmm? Watching all around and pouring out very amusing comments
of - I guess - an elder veteran, watching for beginners to satisfy his own
needs of understatement. Very fine, Mr M anonymous! Very fine! Thanks a lot
for your good advices all around here. We love you very much. Thanks again
for visiting us.
 
M

M

Clever guy, hmm? Watching all around and pouring out very amusing comments
of - I guess - an elder veteran, watching for beginners to satisfy his own
needs of understatement. Very fine, Mr M anonymous! Very fine! Thanks a lot
for your good advices all around here. We love you very much. Thanks again
for visiting us.

Hmm... I seem to have touched a nerve there. I apologize, for that
certainly wasn't my intention.

If ones looks at this and previous questions posted by 'deanfamily11',
it seems clear (to me at least) that he is just learning C++ and since
the posts just started in September, he is probably a student. That
being the case, I think it is more constructive and ultimately more
beneficial to him if he is encouraged to find answers (with guidance)
on his own, rather than having them handed to him. (Teach a man to
fish and so on.)

Perhaps I wasn't clear enough or perhaps you are overacting a bit, but
in no way did I intend to be critical of anyone. I'm sorry if you
misunderstood my intention.
 
Ad

Advertisements

G

Guest

I do understand, what you mean, of course.

But from the educational point of view - this is obviously the concerning
aspect of your speech - , you are wrong. You try to think for the trainee
and to "save" him. And that is no good training. Best training is to learn
from life. And already this thread is a good example.

// oliver
 
Ad

Advertisements

J

John Carson

Oliver (Nospam) said:
Yeap, you are wright, but look at the original source code: "double
sum;"

// oliver

doubles store integers exactly provided the integers are not so large that
they require more than the available number of significant digits in a
double (e.g., on Windows, a double can store an integer exactly provided the
integer has no more than 15 decimal digits). It is only fractions that
doubles (in many cases) store inexactly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top