By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,441 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,441 IT Pros & Developers. It's quick & easy.

Having some trouble with a Pythagorean Program.

P: n/a
I am writing an application to solve Pythagorean Theorum Problems. This
is on my own time, i am using a book to learn c++, and after doing a
fahrenheit to celsuis program from that book, i wanted to try to make
something all be meself.

I have it working great to find the hypotenuse, but am having some
dufficulty making it produce a missing leg. As you know, a^2 + b^2 =
c^2
I have a variable that does the input for the one of the legs subtraced
from the hypotenuse, but i dont know where to go from there. PLEASE
help me, thanks~~3than7

TTHIS IS THE C++ I WROTE
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main( int nNumberofArgs, char* pszArgs[])
{
int wwi;
cout << "Press 1 to find the hypotenuse, press 2 to find a missing
leg: ";
cin >wwi;
if (wwi == 1 ) {
int A;
int B;
//A^2 + B^2 = C^2 or the pythagorean Theorum
cout <<" Please enter A, do not square it!: ";
cin >A;
cout <<" Please enter B, do not square it!: ";
cin >B;

int C;
C = (A*A) + (B*B);

cout<< " C = ";
cout << C << " ";

cout << "";
cout << "";
}
if (wwi == 2) {
int Hypc;
cout << "Enter (C) the Hypotenuse: ";
cin >Hypc;
int Leg;
cout << "Enter one of the legs: ";
cin >Leg;

int F;
F = ( Hypc * Hypc ) - ( Leg * Leg ) ;


cout << "Missing leg is: ";
cout << << " " ;

}
system("PAUSE");
return 0;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>.
~thanks again, 3than7

Dec 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
psp

3t****@gmail.com wrote:
I am writing an application to solve Pythagorean Theorum Problems. This
is on my own time, i am using a book to learn c++, and after doing a
fahrenheit to celsuis program from that book, i wanted to try to make
something all be meself.

I have it working great to find the hypotenuse, but am having some
dufficulty making it produce a missing leg. As you know, a^2 + b^2 =
c^2
I have a variable that does the input for the one of the legs subtraced
from the hypotenuse, but i dont know where to go from there. PLEASE
help me, thanks~~3than7

TTHIS IS THE C++ I WROTE
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main( int nNumberofArgs, char* pszArgs[])
{
int wwi;
cout << "Press 1 to find the hypotenuse, press 2 to find a missing
leg: ";
cin >wwi;
if (wwi == 1 ) {
int A;
int B;
//A^2 + B^2 = C^2 or the pythagorean Theorum
cout <<" Please enter A, do not square it!: ";
cin >A;
cout <<" Please enter B, do not square it!: ";
cin >B;

int C;
C = (A*A) + (B*B);

cout<< " C = ";
cout << C << " ";

cout << "";
cout << "";
}
if (wwi == 2) {
int Hypc;
cout << "Enter (C) the Hypotenuse: ";
cin >Hypc;
int Leg;
cout << "Enter one of the legs: ";
cin >Leg;

int F;
F = ( Hypc * Hypc ) - ( Leg * Leg ) ;


cout << "Missing leg is: ";
cout << << " " ;

}
system("PAUSE");
return 0;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>.
~thanks again, 3than7
I see 2 bugs in this program:
1. You are printing the square of the leg, e.g. if hypotenuse is 10 and
one leg is 6 you are calculating only '64' not 8.
Same with your hypotenuse calculation you are calculating the square of
the hypotenuse.
Correction:
F = sqrt ( ( Hypc * Hypc ) - ( Leg * Leg ) ); // you need to link with
the math library on your OS

2. You are not printing the variable 'F':
cout << "Missing leg is: ";
cout << << " " ;
Correction: cout << "Missing leg is:" << F << endl;

Dec 28 '06 #2

P: n/a

<3t****@gmail.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...
>I am writing an application to solve Pythagorean Theorum Problems. This
is on my own time, i am using a book to learn c++, and after doing a
fahrenheit to celsuis program from that book, i wanted to try to make
something all be meself.

I have it working great to find the hypotenuse, but am having some
dufficulty making it produce a missing leg. As you know, a^2 + b^2 =
c^2
I have a variable that does the input for the one of the legs subtraced
from the hypotenuse, but i dont know where to go from there. PLEASE
help me, thanks~~3than7

TTHIS IS THE C++ I WROTE
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main( int nNumberofArgs, char* pszArgs[])
{
int wwi;
cout << "Press 1 to find the hypotenuse, press 2 to find a missing
leg: ";
cin >wwi;
if (wwi == 1 ) {
int A;
int B;
//A^2 + B^2 = C^2 or the pythagorean Theorum
cout <<" Please enter A, do not square it!: ";
cin >A;
cout <<" Please enter B, do not square it!: ";
cin >B;

int C;
C = (A*A) + (B*B);

cout<< " C = ";
cout << C << " ";

cout << "";
cout << "";
}
if (wwi == 2) {
int Hypc;
cout << "Enter (C) the Hypotenuse: ";
cin >Hypc;
int Leg;
cout << "Enter one of the legs: ";
cin >Leg;

int F;
F = ( Hypc * Hypc ) - ( Leg * Leg ) ;


cout << "Missing leg is: ";
cout << << " " ;

}
system("PAUSE");
return 0;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>.
~thanks again, 3than7
Do you remember how to do this with pen and paper?
You need to calculate square root(s).

Look up 'sqrt()' function.

-Mike
>

Dec 28 '06 #3

P: n/a

Yes i can do this on pen and paper, and one thing i forgot to ask is
how to find a square root in C++, but ill assume sqrt is what i use
for that

thanks to both of you , ill go try it right now

Dec 28 '06 #4

P: n/a

okay, when i use F = sqrt ( (Hypc*Hypc) - (Leg*Leg) );
i get this error

38 C:>>>>>`sqrt' undeclared (first use this function)
what needs to be done to fix this,
im a C++ Noob if you will

Dec 28 '06 #5

P: n/a

<3t****@gmail.comwrote in message
news:11*********************@n51g2000cwc.googlegro ups.com...
>
okay, when i use F = sqrt ( (Hypc*Hypc) - (Leg*Leg) );
i get this error

38 C:>>>>>`sqrt' undeclared (first use this function)
what needs to be done to fix this,
im a C++ Noob if you will
std::sqrt is probably in cmath.

http://dinkumware.com/manuals/?manua...math.html#sqrt
Dec 28 '06 #6

P: n/a
TY SO MUCH!
all of you who posted thanks a million

duane, that website will come in handy later too.

i added

#include <cmath>

and i changed the variable F from int to float

now it works perfectly, excpet with decimals
thaks a lot to all of you! ~3than7

Dec 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.