Programming Noob - 6 months ago 35

C Question

So I'm pretty new to programming and I just can't seem to see anything wrong with my code:

`#include<stdio.h>`

float fallingDistance (int t);

int main () {

int t;

float result=0.0;

printf("t <seconds>\td <meters>\n");

for(t=1;t<=10;t++) {

result = fallingDistance (t);

printf("\t%d\t%.2f\n",t,result);

}

}

float fallingDistance (int t) {

const float Grav = 9.8;

float fallD = 0.5*Grav*(t^2);

return fallD;

}

Now if t=1, fallD should be 0.5*9.8*(1^2) = 4.90 but the output is 14.70. Any idea why?

Answer

In C, there is no `^`

operator for expotentation. Use `Grav(t*t)`

. There is no syntax error though, because `^`

is bitwise XOR operator. It takes two numbers and perform XOR on their corresponding bits.

for example, when `t=6`

`t^2`

will be:

```
6: 110
2: 010
6^2: 100 which in dec is 4.
```

You can also use `pow`

function from `Math`

library. Some introduction can be found on tutorialspoint.