Trouble making formula for pythagorean theorem | Newbie | | Join Date: Jan 2008
Posts: 8
| |
This is my 1st program and I need some help. I've almost got this one finished but I don't know where to go from here. There is something wrong with the sides I've assigned. Any tips? - // Program: Pythagorean Theorem
-
// Written by: Hellbreaker
-
//
-
//
-
#include <iostream>
-
#include <cmath>
-
-
using namespace std;
-
-
void main ()
-
{
-
float m, n;
-
float side1;
-
float side2;
-
float hypotenuse;
-
cout << "****************************************\n";
-
cout << "Hypotenuse Finder: Pythagorean Theorem\n";
-
// Input side lenghts
-
cout << "Enter Length of side M\n";
-
cout << "M =";
-
cin >> m;
-
cout << "Enter Length of Side N\n";
-
cout << "N =";
-
cin >> n;
-
if (m > n && n >= 0)
-
{
-
// compute hypotenuse;
-
side1=((m*m)-(n*n));
-
side2=(2*m*n);
-
hypotenuse = sqrt((side1*side1)+(side2*side2));
-
}
-
cout << "The hypotenuse is......\n";
-
cout << hypotenuse;
-
if (n>m)
-
{
-
cout <<"M must be greater than N.\n" ;
-
}
-
if (0>n)
-
{
-
cout <<"Variable N must be greater than 0.\n";
-
}
-
}
| | Expert | | Join Date: Sep 2007
Posts: 856
| | | re: Trouble making formula for pythagorean theorem Quote:
Originally Posted by abkierstein
// compute hypotenuse;
side1=((m*m)-(n*n));
side2=(2*m*n);
hypotenuse = sqrt((side1*side1)+(side2*side2));
} This looks sort of like you've crossed the Law of Cosines with the Pythagorean Theorem. m and n are the values you want to square and add, then square root.
This assumes your triangle is like this: | | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
so i keep getting the hypo to equal 40????? I can't figure it out. here's my code now. (just trying this one out) - // Program: Pythagorean Theorem
-
//
-
//
-
//
-
#include <iostream>
-
#include <cmath>
-
-
using namespace std;
-
-
void main ()
-
{
-
float m, n;
-
float side1;
-
float side2;
-
float hypo;
-
float hypotenuse;
-
cout << "****************************************\n";
-
cout << "Hypotenuse Finder: Pythagorean Theorem\n";
-
// Input side lenghts
-
cout << "Enter Length of side M\n";
-
cout << "M =";
-
cin >> m;
-
cout << "Enter Length of Side N\n";
-
cout << "N =";
-
cin >> n;
-
if (m > n && n >= 0)
-
{
-
// compute hypotenuse;
-
side1=m;
-
side2=n;
-
hypo =((pow(m,2)) + (pow(n,2)));
-
hypotenuse= sqrt(hypo)
-
}
-
cout << "\nThe hypotenuse is......\n" << hypotenuse;
-
-
if (n>m)
-
{
-
cout <<"M must be greater than N.\n" ;
-
}
-
if (0>n)
-
{
-
cout <<"Variable N must be greater than 0.\n";
-
}
-
}
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
Sorry I've gone through it a lot but I'm about to give up. Please someone give me some hope, c++ has drained all my mana. - // Program: Pythagorean Theorem
-
// Written by:
-
//
-
//
-
#include <iostream>
-
#include <cmath>
-
-
using namespace std;
-
-
void main ()
-
{
-
float m, n;
-
float side1;
-
float side2;
-
float hypotenuse;
-
cout << "****************************************\n";
-
cout << "Hypotenuse Finder: Pythagorean Theorem\n";
-
// Input side lenghts
-
cout << "Enter Length of side M\n";
-
cout << "M =";
-
cin >> m;
-
cout << "Enter Length of Side N\n";
-
cout << "N =";
-
cin >> n;
-
while (m > n && n >= 0)
-
{
-
// compute hypotenuse;
-
side1=m*m;
-
side2=n*n;
-
hypotenuse= sqrt(side1+side2);
-
cout << "The hypotenuse is......"; hypotenuse;
-
}
-
if (n>m)
-
{
-
cout <<"M must be greater than N.\n" ;
-
}
-
if (0>n)
-
{
-
cout <<"Variable N must be greater than 0.\n";
-
}
-
if (0>m)
-
{
-
cout <<"Variable M must be greater than 0.\n";
-
if (m=n)
-
{
-
cout <<"Vatiable M cannot be the same as variable N.\n";
-
}
-
}
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: Trouble making formula for pythagorean theorem
There are a few things wrong:
a) Your while... loop never terminates. m and n are never changed, and so the initial conditions of the loop never change. It is either never executed, or will continue to execute forever and ever and ever and...
b) You put a semicolon after your cout statement in the loop, so the hypotenuse is never being displayed.
c) The purpose of all your if statements is unclear. They will indeed inform the user of the situation, but since they occur after the loop, they can't actually fix anything.
d) In the last if statement, you use m=n, which is assignment. You should use m==n, which is comparison, to determine equality.
The math side of this looks correct, you just need to put a little more thought into the programming aspect of it. Think out your program's flow before you sit down at the computer and try this (i.e. in what order to things have to happen to make sense?).
In addition, there was no need to create a second thread on the same topic. You can keep us updated in this thread - any more than that is unnecessary and wastes space.
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
Okay so I'm kind of unclear on what I've missed. I feel really overwhelmed with this, does it get easy to write programs once you get the hang of it because I feel like I'm riding the short bus.
Here's what I've changed.
while (m > n && n >= 0)
{
// compute hypotenuse;
side1=pow(m,2);
side2=pow(n,2);
side3 =(side1+side2)/(side1+side2);
cout << "The hypotenuse is......";
cout << "\n\n";
cout << side3;
THANKS FOR ALL THE HELP, YOU GUYS ARE SAVING MY SANITY!
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
So i don't think I asked for any help right there but I really could use some now. I just want to use the Pythagorean theorem. -
if (m > n && n >= 0)
-
{
-
// Compute hypotenuse
-
side1=pow(m,2);
-
cout << "side one" <<side1;
-
side2=pow(n,2);
-
cout << side2;
-
side3 = sqrt((side1+side2));
-
cout << "The hypotenuse is\n" <<side3;
-
cout << "\n\n";
even this doesn't work right. I'm about to give up :(
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Trouble making formula for pythagorean theorem
Well, what doesn't work? Does it print anything out? The wrong number? Alien symbols? Does the compiler error out? What's the error?
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
The error is that the hypotenuse isn'tt calculated correctly.
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Trouble making formula for pythagorean theorem Quote:
Originally Posted by abkierstein The error is that the hypotenuse isn'tt calculated correctly. What an astoundingly descriptive reply. So, as you haven't really given me much to go on, I'm going to go copy your code, put it into my compiler, guess as to what you have and haven't put in, re-write some of it, and get it to work, then try to figure out if everything I guessed at is actually what you have put.
My initial guess is that the && operators are messing with the precedence in the if statement (ie make it if ( (m > 0) && (n > 0) ) so that there are definitely not an 'order of operations' errors). That's just as guess as you're not saving that anywhere, so it shouldn't affect it (except for the true/false part of the condition - maybe the loop is never even running, but I can't even tell if everything is getting initialized properly as you are not saying what number you're putting in, what you are getting out, etc... because you didn't post the new program you created.)
What I'd recommend (as I will probably fall asleep before I get your code to work), is to put print statements after each line and print out all the variables (even if you're not using them on that line) so that you can see 1) what is being executed and 2) what the values are before/after they are executed.
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
lol it's cool now, i'm just crazy cause I had to figure it out. THANKS GUYS, now I'm crazy. it finally works -
// Program: Pythagorean Theorem
-
// Written by:
-
//
-
//
-
-
#include <iostream>
-
#include <cmath>
-
-
using namespace std;
-
-
void main ()
-
{
-
float m, n;
-
float side1;
-
float side2;
-
float side3;
-
// Program Starts
-
cout << "****************************************\n";
-
cout << "Hypotenuse Finder: Pythagorean Theorem\n";
-
// Explanation of program's mathmatics
-
cout << "\nA^2+B^2=C^2 (Principle of the Pythagorean Theorem)\n";
-
// Input side lenghts
-
cout << "\nEnter Length of Side M\n\n";
-
cout << "M=";
-
cin >> m;
-
cout << "\nEnter Length of Side N\n\n";
-
cout << "N=";
-
cin >> n;
-
if (m > n && n >= 0)
-
{
-
// Compute hypotenuse
-
side1=pow(m,2);
-
side2=pow(n,2);
-
side3 = sqrt((side1+side2));
-
cout << "\nDrumroll please....................\n\n\n";
-
cout << "\nThe Hypotenuse is " <<side3;
-
cout << "\n\nSide 1 is " <<side1;
-
cout << "\nSide 2 is " <<side2;
-
cout << "\n\n";
-
cout << "************************************\n";
-
}
-
-
// Clean up user input
-
else if (n > m)
-
{
-
cout <<"\nM must be greater than N.\n\n" ;
-
}
-
else if (0 > n)
-
{
-
cout <<"\nVariable N must be greater than 0.\n\n";
-
}
-
else if (0 > m)
-
{
-
cout <<"\nVariable M must be greater than 0.\n\n";
-
}
-
else if (m == n)
-
{
-
cout <<"\nVatiable M cannot be the same as variable N.\n\n";
-
}
-
}
-
it is my 1st.
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Trouble making formula for pythagorean theorem Quote:
Originally Posted by abkierstein lol it's cool now, i'm just crazy cause I had to figure it out. THANKS GUYS, now I'm crazy. it finally works
it is my 1st. Congrats, I'm glad it finally works. Sorry for going off on you last night - it was getting late and I was losing Desktop Tower Defense pretty badly.
Okay, not really (I rock at that game), but I was tired and couldn't get to sleep, but I'm still sorry and glad you got it to work. Please feel free to post again if you get stuck on your next project!
| | Newbie | | Join Date: Jan 2008
Posts: 8
| | | re: Trouble making formula for pythagorean theorem
Right on, thanks man. I appreciate it
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|