Where is my function going wrong?
Choice 1 returns the correct value of 760
Choice 2 returns an input value of inches properly converted to millimeters.
Choice 3 returns an incorrect value of 760. The math for choice 3 returns a correct value as a standalone function
The last else if executes properly by asking for user input. But the variable atm_press does not take on the value presumably returned by choice 3.
Thanks for your help.
[CODE lang="c" title="initial and call"]double atm_press = 0;
atm_press = get_local_press();[/CODE]
[CODE lang="c" title="Function"]double get_local_press()
{
int response = 0;
double h, temp, input = 0;
printf("\n1\tUse Standard Atmospheric Pressure (inHg) 29.92");
printf("\n2\tUse Local Atmospheric Pressure (inHg)\n");
printf("3\tAltitude and air temperature known\n");
printf("\nPlease enter a choice (1, 2 or 3): ");
scanf("%d", &response);
if (response == 1)
return 760;
else if(response == 2)
{
printf("\n Enter Local Atmospheric Pressure [inHg] ");
scanf("%lf", &input);
input = input * 25.4;
printf("\n input = %.2lf", input);
return input;
}
else if(response == 3)
{
printf("\n Enter Altitude [Meters]:");
scanf("%d", &h);
printf("\n Enter Air Temperature [Celcius]: ");
scanf("%d", &temp);
double a_p = 0;
const double lr = 0.0065; /*lapse rate - change in temperature with altitude*/
const double eq_exp = 5.257; /*exponent for hypsometric equation*/
const double stp = 1013.25; /*Standard Temperature and Pressure (0 deg C at 760 mmHg - Units in kPa*/
const double pa_to_mmhg = 0.750062; /*conversion factor pascals to mmHg*/
const double c_to_k = 273.15; /*Celsius to Kelvin offset*/
return a_p = stp * pow((1 - (lr * h) / (temp + lr * h + c_to_k)), eq_exp) * pa_to_mmhg;
}
}[/CODE]
Choice 1 returns the correct value of 760
Choice 2 returns an input value of inches properly converted to millimeters.
Choice 3 returns an incorrect value of 760. The math for choice 3 returns a correct value as a standalone function
The last else if executes properly by asking for user input. But the variable atm_press does not take on the value presumably returned by choice 3.
Thanks for your help.
[CODE lang="c" title="initial and call"]double atm_press = 0;
atm_press = get_local_press();[/CODE]
[CODE lang="c" title="Function"]double get_local_press()
{
int response = 0;
double h, temp, input = 0;
printf("\n1\tUse Standard Atmospheric Pressure (inHg) 29.92");
printf("\n2\tUse Local Atmospheric Pressure (inHg)\n");
printf("3\tAltitude and air temperature known\n");
printf("\nPlease enter a choice (1, 2 or 3): ");
scanf("%d", &response);
if (response == 1)
return 760;
else if(response == 2)
{
printf("\n Enter Local Atmospheric Pressure [inHg] ");
scanf("%lf", &input);
input = input * 25.4;
printf("\n input = %.2lf", input);
return input;
}
else if(response == 3)
{
printf("\n Enter Altitude [Meters]:");
scanf("%d", &h);
printf("\n Enter Air Temperature [Celcius]: ");
scanf("%d", &temp);
double a_p = 0;
const double lr = 0.0065; /*lapse rate - change in temperature with altitude*/
const double eq_exp = 5.257; /*exponent for hypsometric equation*/
const double stp = 1013.25; /*Standard Temperature and Pressure (0 deg C at 760 mmHg - Units in kPa*/
const double pa_to_mmhg = 0.750062; /*conversion factor pascals to mmHg*/
const double c_to_k = 273.15; /*Celsius to Kelvin offset*/
return a_p = stp * pow((1 - (lr * h) / (temp + lr * h + c_to_k)), eq_exp) * pa_to_mmhg;
}
}[/CODE]