power

i Have a problem with using and declaring power for example (squared radius) how can i go about it?
Please explain the problem in more detail.
I think he wants to code an inverse square law in some way but obviously there's still no way to answer such a general, open ended question.
If you simply want to determine a certain power of a certain number, you can use the pow function(remember to include <math.h> )

1
2
3
4
5
6
7
8
9
10
11
#include <math.h>
#include <iostream>
using namespace std;

int main()
{
    double a, b;
    a = 5; // In your case a would be the radius
    b = 2; // b would be the power.
    cout<<"5 ^ 2 = " pow(a, b);
}


Correct me if I am wrong?
The answer will be 25.

Hope that helps

FR3DDIE
#include <cmath> is preferred, what with this being the nineties. :)
Is there a difference?
In general terms, the more modern ones such as cmath, cstring and all the rest tend to take advantage of C++, whereas math.h, string.h and so on are in straight C. The C++ versions use namespaces, which helps to keep things tidy. I have a feeling that in C++ the *.h set is deprecated, so there's the danger that one day they'll vanish and everyone's code will stop compiling :)

In this particular case, cmath contains extra functions for pow. The original C pow function only accepted double type, whereas the shiny new C++ version likes float and int in various combinations too. See also sqrt and probably lots of others.

Last edited on
Topic archived. No new replies allowed.