gpt4 book ai didi

c - 显示错误列表,当连续数据与平均值相差太远时,

转载 作者:行者123 更新时间:2023-11-30 15:35:38 25 4
gpt4 key购买 nike

我想做两件事:显示与平均值相差超过平均值 10% 的所有电压,并显示一小时到下一小时电压变化大于平均值 15% 的所有连续小时对。我遇到了麻烦第二部分。

#include <stdio.h>
#include <math.h>
int i, problem = 0, index;
float voltage[6];
float average, average10, average15, dif, total;
int main(){
total = 0.0;
for( index = 0; index < 6; index++ ){
printf( "Enter a voltage for hour %d: ", index+1 );
scanf( "%f", &voltage[index] );
total += voltage[index];
}
average = total / 6.0;
average10 = average / 10;
average15 = average / 100 * 15;
printf("The average is %1.1f\n", average);
printf("10%% = %1.1f\n", average10);
printf("15%% = %1.1f\n", average15);

for(index = 0; index < 6; index++){
dif = fabs(voltage[index] - average);
if(dif > (average10)){
problem++;
if(problem == 1){
printf("The following problems occurred:\n");}
printf("%d. Voltage at hour %d was %1.1f (difference of %1.1f volts)\n", problem, (i ++)+1, voltage[index], dif);

}
}

for(index = 1; index < 6; index++){
dif = fabs((voltage[i] - voltage[i-1] > average15));
if(dif > average15){
problem++;
if(problem == 1){
printf("The following problems occurred:\n");}
printf("%d Voltage change from hour %d to %d was %1.1f", problem, i, (i ++)+1 , dif);
}
}


if(problem = 0) printf("No problems were encountered.");
}

这显示了第一部分,除了问题时间之外,并不总是显示正确的值(如此处所示,问题 2 没有足够的代表来嵌入抱歉)http://gyazo.com/34fa038b11bf85effa195232f952cd76

但如果没有出现问题,第二部分或 printf 绝对不会出现任何内容。你们对于如何使问题上的值正确排列以及为什么我没有从第二个 for 循环中得到任何结果有什么想法

最佳答案

这里只是一个错字:

 dif = fabs((voltage[i] - voltage[i-1] > average15));
/* This means dif = fabs((0));
* dif = fabs((1));
* as the result of the > operator is 0 or 1
**/

应该是

 dif = fabs(voltage[i] - voltage[i-1]);

关于c - 显示错误列表,当连续数据与平均值相差太远时,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22853532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com