473,569 Members | 2,737 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 8010
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.
"deanfamily 11" <de**********@v erizon.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********@com cast.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.
"deanfamily1 1" <de**********@v erizon.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:

"deanfamily 11" <de**********@v erizon.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:

"deanfamily 11" <de**********@v erizon.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.n et.au> wrote:
"Oliver (Nospam)" <ok*****@arcor. de> wrote in message
news:43******* *************** @newsread2.arco r-online.net
I inserted corrections below:

"deanfamily 11" <de**********@v erizon.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

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

Similar topics

42
2931
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 interesting solutions at the end, so the win are the results :-) -- Frank Buß, fb@frank-buss.de
5
4797
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 have it be the color red. Here is the code your insights will be very helpful thanks a lot. #include <stdio.h> #include <stdlib.h> #include...
4
2766
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 shows * * * * * * * * * *
0
1384
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 *, 10 more * (with no space between the first set of 10 and the second), and then 9 spaces, 1 *, 2nd row = 2 *, 8 spaces, 9 *, 2 spaces, 9 *, 8...
2
2131
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 suggestions?
0
1338
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 of the vtkPolyData that vtkDelaunay2D produces? I can get the vertex corredinates like this: delny = vtk.vtkDelaunay2D()...
3
3501
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
3355
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. Anyway, what I'm looking for is similar to the click-down triangles in an OSX Finder window in list mode. Thanks for any help or URL to what I'm...
1
3576
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 these triangles, and that is getting on my nerves. I'm sure there's some professional reason for them, but I'm not a professional and I would like to...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.