0

我想做两件事:显示与平均值相差超过平均值 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 循环中得到任何东西有任何想法吗?

4

1 回答 1

1

这只是一个错字:

 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]);
于 2014-04-04T05:50:48.587 回答