为什么这些函数在75%的时间内给出正确的输出? [英] Why do these functions give the correct output 75% of the time?
问题描述
(此函数是更大程序的一部分,但独立于其他函数运行。)
Ok我有一个函数夹克,给定3个输入,只有75%的时间。
我不知道输入,但我知道输出是错误的。
我假设每次将函数提交到myProgrammingLab时输入的值相同。
这可能是一个特定输入的问题。
谢谢。
说明:
夹克尺寸(以英寸为单位的胸部)=身高乘以重量除以288,然后通过每10年增加1/8英寸(注意,调整只发生在整整10年后,因此,30到39岁之间没有任何调整,但是40岁时加上1/8英寸。)
编辑:将tmp更改为float仍会产生错误。
float jacket(float weight,float height,int age){
double result =(height * weight)/ 288;
/ *现在每过去30年添加(1/8)* /
if((age-30)> 0){
int temp = 30)/ 10;
result = result +(temp * .125);
}
return result;
}
这是相同的问题写成不同的函数。
float jacket(double jWeight,double jHeight,int jAge)
{
double jSize =((jWeight * jHeight) /288.0);
int i = jAge / 10 - 3;
if((jAge / 10)> 3)
jSize + = 0.125 * i;
return jSize;
}
这是同样问题的第三个函数
float夹克(双重,双高,int年龄)
//计算夹克尺寸, b $ b //十年,如果客户超过30岁。
{
int age_factor;
double j_size;
j_size =(height * weight)/288.0;
if(age> = 30)
{
age_factor =(age-30)/ 10; //注意可能的截断。
j_size + = age_factor / 8.0;
}
return j_size;
}
第一次调用函数时会产生不正确的返回值。保留3次它被调用的返回值是正确的。
观察:
预期输出:
夹克·尺寸·=·24.17↵
夹克·尺寸·=·40.00↵
夹克·尺寸·=·46.04↵
夹克·尺寸·=·35.42↵
实际输出:
夹克·尺寸···24.29↵
夹克·尺寸·=·40.00↵
夹克·尺寸·=·46.04↵
夹克·尺寸·=·35.42↵
*所有三个函数给出相同的输入,产生相同的输出
int temp =(age - 31) / 10;
。我个人认为这是错误的,和其他人一样,但有人可能会说,一刻31一个只是几秒钟超过30.这是值得一试,无论如何。 (this function is part of a larger program but operates independently of other functions.)
Ok I have a function jacket and given 3 inputs it produces the correct output only 75% of the time.
I do not know the inputs but I know the output is wrong.
I do not know what is wrong and have no idea how to fix it.
I assume it is the same 12 values entered each time the function is submitted to myProgrammingLab.
So it may be a problem with a specific input.
Thanks.
The Description:
Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30. (note that the adjustment only takes place after a full 10 years. So, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)
edit: changing tmp to float still produced the error.
float jacket(float weight, float height, int age) {
double result = (height * weight) / 288;
/*now for every 10 years past 30 add (1/8) to the result*/
if((age - 30) > 0){
int temp = (age - 30) / 10;
result = result + (temp * .125);
}
return result;
}
This is the same function written differently with the same problem.
float jacket(double jWeight, double jHeight, int jAge)
{
double jSize = ((jWeight*jHeight)/288.0);
int i = jAge/10 - 3;
if((jAge/10)>3)
jSize += 0.125*i;
return jSize;
}
This is a third function with the same problem
float jacket(double weight, double height, int age)
// calculates the jacket size, adjusting for age in increments
// of ten years, if customer is over 30 years of age.
{
int age_factor;
double j_size;
j_size = (height*weight)/288.0;
if (age >=30)
{
age_factor = (age-30)/10; //note possible truncation.
j_size += age_factor/8.0;
}
return j_size;
}
The first time the function is called it produces an incorrect return value. the remain 3 times it is called the return value is correct.
Observe:
Expected Output:
jacket·size·=·24.17↵
jacket·size·=·40.00↵
jacket·size·=·46.04↵
jacket·size·=·35.42↵
Actual Output:
jacket·size·=·24.29↵
jacket·size·=·40.00↵
jacket·size·=·46.04↵
jacket·size·=·35.42↵
*All three functions given the same input produce the same output
I guess one could say that 31 isn't necessarily 1 year over 30. Try int temp = (age - 31)/10;
. Personally I think that's wrong, as does everyone else here, but someone could argue that the moment one turns 31 one is only a few seconds over 30. It's worth a try, anyway.
这篇关于为什么这些函数在75%的时间内给出正确的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!