468,504 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Trouble making formula for pythagorean theorem

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?

Expand|Select|Wrap|Line Numbers
  1. // Program: Pythagorean Theorem
  2. // Written by: Hellbreaker
  3. //
  4. //
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. void main ()
  11. {
  12.     float m, n;
  13.     float side1;
  14.     float side2;
  15.     float hypotenuse;
  16.     cout << "****************************************\n";
  17.     cout << "Hypotenuse Finder: Pythagorean Theorem\n";
  18.     // Input side lenghts
  19.     cout << "Enter Length of side M\n";
  20.     cout << "M =";
  21.     cin >>  m;
  22.     cout << "Enter Length of Side N\n";
  23.     cout << "N =";
  24.     cin >>  n;
  25.     if (m > n && n >= 0)
  26.     {
  27.     // compute hypotenuse;
  28.         side1=((m*m)-(n*n));
  29.         side2=(2*m*n);
  30.         hypotenuse = sqrt((side1*side1)+(side2*side2));
  31.     }
  32.         cout << "The hypotenuse is......\n";
  33.         cout << hypotenuse;
  34.     if (n>m)
  35.     {
  36.         cout <<"M must be greater than N.\n" ;
  37.     }
  38.     if (0>n)
  39.     {
  40.         cout <<"Variable N must be greater than 0.\n";
  41.     }
  42. }
Jan 15 '08 #1
12 7041
Laharl
849 Expert 512MB

// 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:
Expand|Select|Wrap|Line Numbers
  1. m| \ hypo
  2.    -
  3.    n
  4.  
Jan 15 '08 #2
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)
Expand|Select|Wrap|Line Numbers
  1. // Program: Pythagorean Theorem
  2. // 
  3. //
  4. //
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. void main ()
  11. {
  12.     float m, n;
  13.     float side1;
  14.     float side2;
  15.     float hypo;
  16.     float hypotenuse;
  17.     cout << "****************************************\n";
  18.     cout << "Hypotenuse Finder: Pythagorean Theorem\n";
  19.     // Input side lenghts
  20.     cout << "Enter Length of side M\n";
  21.     cout << "M =";
  22.     cin >>  m;
  23.     cout << "Enter Length of Side N\n";
  24.     cout << "N =";
  25.     cin >>  n;
  26.     if (m > n && n >= 0)
  27.     {
  28.     // compute hypotenuse;
  29.         side1=m;
  30.         side2=n;
  31.         hypo =((pow(m,2)) + (pow(n,2)));
  32.         hypotenuse= sqrt(hypo)
  33.     }
  34.         cout << "\nThe hypotenuse is......\n" << hypotenuse;
  35.  
  36.     if (n>m)
  37.     {
  38.         cout <<"M must be greater than N.\n" ;
  39.     }
  40.     if (0>n)
  41.     {
  42.         cout <<"Variable N must be greater than 0.\n";
  43.     }
  44. }
Jan 15 '08 #3
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.

Expand|Select|Wrap|Line Numbers
  1. // Program: Pythagorean Theorem
  2. // Written by: 
  3. //
  4. //
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. void main ()
  11. {
  12.     float m, n;
  13.     float side1;
  14.     float side2;
  15.     float hypotenuse;
  16.     cout << "****************************************\n";
  17.     cout << "Hypotenuse Finder: Pythagorean Theorem\n";
  18.     // Input side lenghts
  19.     cout << "Enter Length of side M\n";
  20.     cout << "M =";
  21.     cin >>  m;
  22.     cout << "Enter Length of Side N\n";
  23.     cout << "N =";
  24.     cin >>  n;
  25.     while (m > n && n >= 0)
  26.     {
  27.     // compute hypotenuse;
  28.         side1=m*m;
  29.         side2=n*n;
  30.         hypotenuse= sqrt(side1+side2);
  31.         cout << "The hypotenuse is......"; hypotenuse;
  32.     }
  33.     if (n>m)
  34.     {
  35.         cout <<"M must be greater than N.\n" ;
  36.     }
  37.     if (0>n)
  38.     {
  39.         cout <<"Variable N must be greater than 0.\n";
  40.     }
  41.     if (0>m)
  42.     {
  43.         cout <<"Variable M must be greater than 0.\n";
  44.     if (m=n)
  45.     {
  46.         cout <<"Vatiable M cannot be the same as variable N.\n";
  47.     }
  48. }
Jan 15 '08 #4
Ganon11
3,652 Expert 2GB
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.
Jan 16 '08 #5
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!
Jan 16 '08 #6
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.
Expand|Select|Wrap|Line Numbers
  1. if (m > n && n >= 0)
  2.     {
  3.     // Compute hypotenuse
  4.         side1=pow(m,2);
  5.         cout << "side one" <<side1;
  6.         side2=pow(n,2);
  7.         cout << side2;
  8.         side3 = sqrt((side1+side2));
  9.         cout << "The hypotenuse is\n" <<side3;
  10.         cout << "\n\n";
even this doesn't work right. I'm about to give up :(
Jan 16 '08 #7
sicarie
4,677 Expert Mod 4TB
Well, what doesn't work? Does it print anything out? The wrong number? Alien symbols? Does the compiler error out? What's the error?
Jan 16 '08 #8
The error is that the hypotenuse isn'tt calculated correctly.
Jan 16 '08 #9
sicarie
4,677 Expert Mod 4TB
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.
Jan 16 '08 #10
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
Expand|Select|Wrap|Line Numbers
  1. // Program: Pythagorean Theorem
  2. // Written by:
  3. //
  4. //
  5.  
  6. #include <iostream>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. void main ()
  12. {
  13.     float m, n;
  14.     float side1;
  15.     float side2;
  16.     float side3;
  17.     // Program Starts
  18.     cout << "****************************************\n";
  19.     cout << "Hypotenuse Finder: Pythagorean Theorem\n";
  20.     // Explanation of program's mathmatics
  21.     cout << "\nA^2+B^2=C^2 (Principle of the Pythagorean Theorem)\n";
  22.     // Input side lenghts
  23.     cout << "\nEnter Length of Side M\n\n";
  24.     cout << "M=";
  25.     cin >>  m;
  26.     cout << "\nEnter Length of Side N\n\n";
  27.     cout << "N=";
  28.     cin >>  n;
  29.     if (m > n && n >= 0)
  30.     {
  31.     // Compute hypotenuse
  32.         side1=pow(m,2);
  33.         side2=pow(n,2);
  34.         side3 = sqrt((side1+side2));
  35.         cout << "\nDrumroll please....................\n\n\n";
  36.         cout << "\nThe Hypotenuse is  " <<side3;
  37.         cout << "\n\nSide 1 is  " <<side1;
  38.         cout << "\nSide 2 is  " <<side2;
  39.         cout << "\n\n";
  40.         cout << "************************************\n";
  41.     }
  42.  
  43.     // Clean up user input
  44.     else if (n > m)
  45.     {
  46.         cout <<"\nM must be greater than N.\n\n" ;
  47.     }
  48.     else if (0 > n)
  49.     {
  50.         cout <<"\nVariable N must be greater than 0.\n\n";
  51.     }
  52.     else if (0 > m)
  53.     {
  54.         cout <<"\nVariable M must be greater than 0.\n\n";
  55.     }
  56.     else if (m == n)
  57.     {
  58.         cout <<"\nVatiable M cannot be the same as variable N.\n\n";
  59.     }
  60. }
  61.  
it is my 1st.
Jan 16 '08 #11
sicarie
4,677 Expert Mod 4TB
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!
Jan 16 '08 #12
Right on, thanks man. I appreciate it
Jan 18 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

37 posts views Thread by Jason Heyes | last post: by
12 posts views Thread by Santosh Krisnan | last post: by
5 posts views Thread by Carramba | last post: by
4 posts views Thread by Pieter | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.