473,503 Members | 13,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Right triangles...

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;

}
Sep 10 '05 #1
12 8003
GB
deanfamily11 wrote:
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
Sep 10 '05 #2
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.
"deanfamily11" <de**********@verizon.net> wrote in message
news:QKrUe.165$R9.0@trnddc02...
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;

}

Sep 10 '05 #3
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.
On Fri, 9 Sep 2005 21:39:01 -0700, "Dave Townsend"
<da********@comcast.net> wrote:
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.
"deanfamily11" <de**********@verizon.net> wrote in message
news:QKrUe.165$R9.0@trnddc02...
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;

}


Sep 10 '05 #4
I inserted corrections below:

"deanfamily11" <de**********@verizon.net> schrieb im Newsbeitrag
news:QKrUe.165$R9.0@trnddc02...
#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 )
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}

Sep 10 '05 #5
"Oliver (Nospam)" <ok*****@arcor.de> wrote in message
news:43**********************@newsread2.arcor-online.net
I inserted corrections below:

"deanfamily11" <de**********@verizon.net> schrieb im Newsbeitrag
news:QKrUe.165$R9.0@trnddc02...
#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 )
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}


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)
//

--
John Carson

Sep 10 '05 #6
Yeap, you are wright, but look at the original source code: "double sum;"

// oliver
Sep 10 '05 #7
deanfamily11 wrote:
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
Sep 10 '05 #8
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.
On Sat, 10 Sep 2005 23:18:48 +1000, "John Carson"
<jc****************@netspace.net.au> wrote:
"Oliver (Nospam)" <ok*****@arcor.de> wrote in message
news:43**********************@newsread2.arcor-online.net
I inserted corrections below:

"deanfamily11" <de**********@verizon.net> schrieb im Newsbeitrag
news:QKrUe.165$R9.0@trnddc02...
#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 )
cout << "This is a right triangle.";
else
cout << "This is not a right triangle.";

return 0;

}


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)
//


Sep 10 '05 #9
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.
Sep 10 '05 #10
M
On Sat, 10 Sep 2005 21:51:18 +0200, "Oliver \(Nospam\)"
<ok*****@arcor.de> wrote:
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.

Sep 10 '05 #11
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
Sep 10 '05 #12
"Oliver (Nospam)" <ok*****@arcor.de> wrote in message
news:43**********************@newsread2.arcor-online.net
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.

--
John Carson

Sep 11 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
2926
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
5
4794
by: George | last post by:
I have a problem creating triangles with this program it creates rectangles and squares but not triangles. For example I would like to create a triangle with the vertices (1,1), (31,1), (31,31) and...
4
2762
by: asif929 | last post by:
I have another program to write, i will appreciate if somebody can help......prompts the user to enter positive integer, and then prints out four triangles For Example: If we enter 4 it should...
0
1378
by: simpleeshelbee | last post by:
When I went back and looked at what came out to see for my message, I saw that it came out a little different than I need it. I need it like this.... * = asterisks 1st row = 1 *, 9 spaces, 10...
2
2128
by: js06am | last post by:
I can't for the life of me work out just how to draw a triangle in java. Using the g.fillRect etc has been easy for rectangles but I have absolutely no idea where to start with triangles. Any...
0
1335
by: Grant Edwards | last post by:
I posted this question to the vtk mailing list last week: I've been Googling and wandering through the class references most of the afternoon, but I can't figure out how to get the triangles out...
3
3498
by: aj2317 | last post by:
how do i do shapes like diamond,right angled triangle etc using just *(stars) in nested for loop???? like * ...
5
3352
by: luxor1275bc | last post by:
I am trying to google for how to create click-down triangles with either CSS or some other method but not having much luck. It's probably because my choice of words to describe this is incorrect....
1
3568
Markus
by: Markus | last post by:
Recently got PS CS4 (looking good). However, when I create a new image, be it white BG, transparent, etc. I'm greeted with a few triangles of what appears to be transparency. I'm unable to draw into...
0
7212
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
7296
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
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
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...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.