Thread: Square roots / Powers etc.

1. Square roots / Powers etc.

I am an ultra newbie to all this, and I'm learnin in my own free time, so keep it simple plz. I know it is something to do with including <math.h>, which I can do, but then how do I use the functions within it? do I simply have to type num^3 or something and it will work, what about square roots?

I have seen some code on this board for square roots but it looks to be in C, and I'd like C++ (I dont know anything about C so it is difficult for me to understand and learn from)

Thanks for any help

2. Code:
```#include <iostream.h>
#include <math.h>

int main ( ) {
double a, b, c;
a = 7.0;
b = pow( a, 2 );
c = sqrt( b );
cout << a << " squared is " << b << endl;
cout << c << " is the square root of " << b << endl;
return 0;
}```

3. Thanks Salem I can now carry on with that maths program I've been working on, I get £5 from my maths teacher if I make a program that can solve linear, quadratic and cubic equations.

4. Equation solver

There exist formulae which can solve quadratic and cubic equations. It is mathematically impossible for a simple formula to exist to solve quintic (not sure about quartic) polynomial equations.

If the input is going to be like this, then it should be easy:

If equation is: x^4 + 2x^3 - 9 = 0

Then you input: 1, 2, 0, -9

If you have to be able to solve more complicated ones, then you're in trouble! You'd have to write an equation parser, and then I guess you'd just have to graph it over some massive domain and find intersections with 0, using a binary search to narrow down on the exact X values of the intersections. Of course, that's ignoring imaginary roots. And if your roots are outside the domain, then you have another problem.

5. Thanks to you too, TerranFury, I was under the impression that cubic equations could only be solved by trial and error, until I went out and looked for a formula. I had written a program for the trial and error method but it wasnt always reliable and was very slow. Now I can do it properly.

I'm not intending to make a program to solve quartic and quintic equations just yet so I should be ok, I'm only just getting started. Win32API is my next step which I'm learning now.

Popular pages Recent additions