Having some trouble with a Pythagorean Program.

3

3than7

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
 
P

psp

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;
 
M

Mike Wahler

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
 
3

3than7

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
 
3

3than7

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
 
3

3than7

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
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top