473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 7384
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

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

Similar topics

37
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function...
12
by: Santosh Krisnan | last post by:
hello all, I fiddled with BASIC in the early 90s but left it at that. Now I am trying to learn C. I tried to solve an exercise in my book, but it failes to compile. Can anyone tell me what the...
6
by: 3than7 | last post by:
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...
8
by: teddarr | last post by:
I'm having trouble getting a mathmatical formula to work in my code. I am supposed to write a program in java that calculates the ending balance each month. The user is supposed to input the...
5
by: Carramba | last post by:
theorem states that: Integer n is prime if and only if (x +1)^n ≡ x^n +1 (mod n) in Z. so I testing it, but values doesn't match ... and I don't se why.. I guess :) it's some thing wrong in...
5
by: stephanieanne2 | last post by:
The Problem: A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy...
11
by: inferi9 | last post by:
hi everyone I am new here and I have this C++ program that I have to write but it keep given me nothing useful. here is the question: A right triangle can have sides that are all integers. A...
4
by: Pieter | last post by:
Hi, I'm currently making a basic pattern generator. When you open the program it draws the selected pattern in a box, you can make the box also bigger and smaller just by dragging at the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.