【问题标题】:Displaying a list of errors,when consecutive data is too far from the average,显示错误列表,当连续数据离平均值太远时,
【发布时间】:2014-04-04 04:23:57
【问题描述】:

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

【问题讨论】:

    标签: c if-statement for-loop floating-action-button


    【解决方案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]);
    

    【讨论】:

    • 是的,谢谢,为那个放屁,为了完成,由于是 = 而不是 == 并且通过一些测试我似乎已经设法得到它运行正常,感谢帮助
    • 是的,棘手的是它是有效的 C,没有编译器警告或错误警告你,所以这是一个不幸的错字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多