Bob Smith - 1 year ago 77

C Question

Function header is there as well as the calculation section

Basically, the correct result is not being returned into main where the value is always zero.

**Ideally answer in visual code for simplicity purposes.

(This code is reduced from the original source).

`#include <stdio.h>`

#include <math.h>

#define PI 3.14159265358979323846

#define GRAVITY 9.8

// Function header: air time

double CalculateAirTime(double maxVelocity, double userAngle, double airTime);

int main(void)

{

// Variables

double maxVelocity;

double userAngle;

double ansDistance;

double airTime;

// Run function: air time

airTime = CalculateAirTime(airTime);

return 0;

}

// Function calc: air time

double CalculateAirTime(double velocity, double angle, double time)

{

printf("Total air time: %f\n", time);

time = ((2*velocity)*sin(angle)/GRAVITY);

return time;

}

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

**1.** Your function signature takes 3 arguments, and you only supply 1. How it compiles, I do not know, as I don't think it should.

**2.** The `CalculateAirTime`

function prints a variable before calculating it. You first do this: `printf("Total air time: %f\n", time);`

, and then calculate the actual air time: `time = ((2*velocity)*sin(angle)/GRAVITY);`

**3.** As an advice, since the `CalculateAirTime`

function needs velocity and angle to return a calculated time, do not pass time as a parameter. It's useless. You can either declare a double variable inside the function or directly return the calculated time.

Replace `double CalculateAirTime(double velocity, double angle, double time){...}`

with `double CalculateAirTime(double velocity, double angle){...}`

***EDIT!**
For clarity purposes, here is how I would probably write your program:**

```
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define GRAVITY 9.8
/*
I renamed some variables. Just from the variable names, I think you do not understand the solution
for this "air time" problem well. I might be mistaken tough. If you have any questions, please
ask them. You are not leaving this place until you fully understand what is happening.
*/
// Function DECLARATION:
double CalculateAirTime(double Initial_Velocity, double Launch_Angle);
int main(void)
{
// variables
double Initial_Velocity;
double Launch_Angle;
// Initial_Velocity = 20.0 should work aswell. This is called a cast. You will learn about it later
Initial_Velocity = (double)(20.0);
// Converting 45 degrees to radians, because the sin() library function works with radians
Launch_Angle = (double)((45.0/180.0)*PI);
//Call function using our declared values as input
CalculateAirTime(Initial_Velocity,Launch_Angle);
return 0;
}
// Function DEFINITION
//Try to use the same name for paramaters here, as you did in the declaration!
double CalculateAirTime(double Initial_Velocity, double Launch_Angle)
{
double Air_Time;
Air_Time = (double)((2*Initial_Velocity)*sin(Launch_Angle)/GRAVITY);
printf("Total air time: %f\n", Air_Time));
return time;
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**