Hypotenuse of a Right Triangle-NO MATH LIBRARY

Hey guys so I need to come up with an equation in my C++ program that calculates the hypotenuse of a right triangle but we cannot use the math library so no "cmath".

1
2
3
4
5
6
7
 double HypoTriangle(float sideA, float sideB)
{
 

   double sideC = sqrt(sideA*sideA + sideB*sideB);
   return sideC;
ode you need help with here.


That is what I had BEFORE knowledge of this came from my professor, any suggestions?
That's utter nonsense that you can't use cmath. But rather than flame your professor, I guess we'll just have to work with it.

Take a look at the Newton's approximation method to compute square roots. It shouldn't be horribly hard to implement.

-Albatross

That's utter nonsense

Non necessarily nonsense - much depends on what exactly branch of programming students are learning.


Take a look at the Newton's approximation method to compute square roots. It shouldn't be horribly hard to implement.

Here is the short explanation of simplification of this method for square roots:
http://codeabbey.com/index/wiki/square-root-approximation

However, you can use even binary search to calculate square root since it is monotonic function.
http://codeabbey.com/index/wiki/binary-search

So the task should not be difficult.
Ok so I'm a little overwhelmed not going to lie but I also read at some point that you can do by multiply by .01? I don't know the accuracy of this and this does not compile but it was something I was trying to mess with....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double HypoTriangle(float sideA, float sideB)
 {


int sides = (sideA*sideA + sideB*sideB);

double n = 0.01;
while ((n * n) < sides)
 {
 n += 0.01;
 }
return triResult;
}
If you are looking for an algorithm it isn't difficult to find the square root using an iterative process. Repeat this as required:

estimate = ((n/estimate) + estimate) * 0.5;

It is necessary to (a) choose an initial value for estimate and (b) decide when to terminate.
Last edited on
Topic archived. No new replies allowed.