473,749 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble making formula for pythagorean theorem

8 New Member
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 7424
Laharl
849 Recognized Expert Contributor

// compute hypotenuse;
side1=((m*m)-(n*n));
side2=(2*m*n);
hypotenuse = sqrt((side1*sid e1)+(side2*side 2));

}
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
abkierstein
8 New Member
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
abkierstein
8 New Member
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 Recognized Expert Specialist
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
abkierstein
8 New Member
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
abkierstein
8 New Member
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 Recognized Expert Moderator Specialist
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
abkierstein
8 New Member
The error is that the hypotenuse isn'tt calculated correctly.
Jan 16 '08 #9
sicarie
4,677 Recognized Expert Moderator Specialist
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

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

Similar topics

37
3403
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 to extract pythagorean triples from an input stream. The input is formatted so that only the first two components <a,b> of a pythagorean triple are specified. The function signature will be: std::istream &operator>>(std::istream &is,...
12
1910
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 error messages mean & what I should do? thanks.
6
2035
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 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...
8
2092
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 monthly payment, the annual interest rate, and the total months required to pay off the loan. I am to use the formula: Balance(pn) = payment * (1 - (1 + mir) ^ (pn - tp)) / mir payment = monthly payment mir = monthly interest rate
5
2449
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 my implementation... hope you can help out :) #include <stdlib.h> #include <stdio.h>
5
4716
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 the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2, and the hypotenuse that fall within a user-specified range. Limit the upper-bound to 500. Use a...
11
15056
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 set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the following relationship: (side1)^2 + (side2)^2 = (hypotenuse)^2 Output all Pythagorean...
4
2853
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 corners. So I also want to implent a zone plate, but I can't get it to work. Already found following formula: phi = (k*phi) + (kx*x) + (ky*y) + (kt*t), the last section with kt*t I will not use that. So I started really simple and just implemted the...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.