How to use "pow"?

This is my first post, so sorry if I´m sounding very noobish... I will hopefully get better in time :-)

Am I using pow in the wrong way, or did I set up the calculation in a bad way?

#include <iostream>
#include <math.h>

using namespace std;

int numberOfSeconds;
int numberOfMeters;
int acceleration = 32;

int main (int argc, const char * argv[])
{
cout << "Enter the number of seconds the object should fall: ";
cin >> numberOfSeconds;

cout << "The object will fall: ";
cout << numberOfMeters;

numberOfMeters = ((acceleration * numberOfSeconds)pow(2)/2);



return 0;
}
Yes. Pow accepts two parameters: base and exponent.
http://www.cplusplus.com/reference/clibrary/cmath/pow/

Ok thx. And I can use a variable as base?

Any idea what is wrong with this then?:

#include <iostream>
#include <math.h>

using namespace std;

int numberOfSeconds;
int numberOfMeters;
int acceleration = 32;
float sum;
int totalDistance;

int main ()
{
cout << "Enter the number of seconds the object should fall: ";
cin >> numberOfSeconds;

cout << "The object will fall: ";
cout << totalDistance;

numberOfMeters = (acceleration * numberOfSeconds);
sum = pow(numberOfMeters, 2.0f);

totalDistance = sum/2;


return 0;
}
For some reason, pow() won't accept ints as base. You'll have to cast it:
pow((float)numberOfMeters),2);
@victorpax

I had no problem compiling, and running, your program, after moving
1
2
cout << "The object will fall: ";
cout << totalDistance;
past the code that creates the value for totalDistance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// using POW.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

int numberOfSeconds;
int numberOfMeters;
int acceleration = 32;
float sum;
int totalDistance;

int main ()
{
cout << "Enter the number of seconds the object should fall: ";
cin >> numberOfSeconds;

numberOfMeters = (acceleration * numberOfSeconds);
sum = pow(numberOfMeters, 2.0f);

totalDistance = sum/2;

cout << "The object will fall: ";
cout << totalDistance << endl;

return 0;
} 
On a physical point of view on the other hand (nothing to do with programming), it seems to me there might be a flaw in the formula. Just look at the units : (acceleration (m.s^-2)*duration(s))^2 you'll get a result in m^2/s^2 --not a distance
...How? The exponent has higher precedence. It's m*(s^-2), not (m*s)^-2, so there is only one 'm'. The s^2 and s^-2 cancel each other out.
Not if you use parenthesis as you did :

1
2
3
4
numberOfMeters = (acceleration * numberOfSeconds);
sum = pow(numberOfMeters, 2.0f);

totalDistance = sum/2;

which is equivalent to :

totalDistance = pow(acceleration * numberOfSeconds, 2.0f) / 2;

when it should be :

totalDistance = acceleration * pow(numberOfSeconds, 2.0f) / 2;
to get a result in meters
Last edited on
Yes, you're right, I wasn't looking at his code and apparently failed to read your formula correctly. My bad.
Topic archived. No new replies allowed.