2016-05-03 79 views
0

我需要你的智慧。看起来不是一个大问题,但我需要一种方法。 首先,我会分享代码。这段代码是正确的,但我需要一些补充,内循环有条件,如果电压大于百分比它没关系,但都是正确的我只需要一个写作。我有2个循环,但只需要一个提示。 如果它很混乱,我可以分享原来的问题。感谢你们。2 For循环一个结果

我把原来的问题:

电压读数从一个变电站得到一次六小时每小时(所以有六个 读数)。写一个C程序在变电站上执行以下检查: a)显示与平均值相差超过平均值10%的所有电压。 b)显示所有连续小时对,其中从1小时的电压变化到下一个小时的电压的变化大于平均值的15%。

实施例1

输入6点的电压:210.1 223.2 189.6 206.2 235.1 215.0 平均值为213.2伏。 10%= 21.3伏。 15%= 32.0伏特。

发生以下问题: 1.第3小时的电压为189.6伏(相差23.6伏)。 2.第5小时的电压为235.1伏(相差21.9伏)。 3.从小时2到小时3的电压变化是33.6伏。

实施例2

输入6点的电压:233.1 201.0 221.5 240.2 222.7 208.1 平均值为221.1伏。 10%= 22.1伏。 15%= 33.2伏。

没有遇到任何问题。

#include <stdio.h> 
#include <math.h> 
#include <string.h> 

int i; 
float volt[6]; 
float avg, avg10, avg15, total, a, b; 

    int main() { 

    total= 0 ; 
    avg = 0; 
    printf("Enter 6 Volts of Machine\n"); 

    for (i=0; i<6; i++) { 
    printf("Type %d. volt", i+1); 
    scanf("%f",&volt[i]); 

    total = total + volt[i]; 
} 
avg = total/6; 
avg10 = (avg * 10)/100; 
avg15 = (avg * 15)/100; 
printf("------------------------------------------\n"); 
printf("The machine Avarage Voltage is %.2f\n", avg); 
printf("The Machine Avarage is%.2f\n", avg10); 
printf("The Machine 15 Avarage is%.2f\n\n\n", avg15); 


    for (i=0;i<6;i++) { 
     a = fabs(volt[i] - avg); 

     if(a > avg10) { 
    printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a); 
     } 
    } 


    for (i=0; i<5; i++) { 

     b = fabs(volt[i+1] - volt[i]); 
     if(b > avg15) { 
    printf("\nVoltage change from hour %d to hour %d was %.2f\n\n", i+1, i+2, b); 
     } 
    } 
+0

是你的问题如何打印“没有问题遇到”第二个例子? –

+0

是的,因为我只需要一个“没有遇到问题”的答案。我尝试了很多次,我看到了循环的所有可能性,所以11“没有遇到任何问题”或者当我在循环中添加3.if时,我看到电压正确后显示“没有遇到问题”结果。 – Axis

回答

0

如果你只需要一个循环尝试这样的事:

for (i=0;i<6;i++) 
{ 
    if((a = fabs(volt[i] - avg)) > avg10) 
    { 
     printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a); 
    } 
    if((i < 5 && (b = fabs(volt[i+1] - volt[i])) > avg15) 
    { 
     printf("\nVoltage change from hour %d to hour %d was %.2f\n\n", i, i+1, b); 
    } 
} 
+0

谢谢安德烈试图帮助。我试过你的代码,看起来像工作,但我得到了一个新的问题,首先如果语句和第二如果语句结果应该是分开的,首先如果语句结果然后第二,所以你的代码都嵌套。再次感谢您为您提供反馈。 – Axis

0

如果你想没有遇到问题时打印出一个消息,你必须记住,如果任何或有多少错误,报告。当然,你不能在循环内打印出这样的消息,因为八次说“没有发生错误”并报告三个错误是有点矛盾的。

您的预期输出显示了错误的枚举,因此最好保留一个错误计数。继续如下:

  • 无论何时打印错误消息,增加错误计数。
  • 在你这样做之前,检查这是否是报告的第一个错误。如果是,则打印标题(“发生以下错误”)
  • 如果您已经检查了所有内容并且没有发生错误,请打印成功消息。

或者,代码:

int nerror = 0; 

for (i = 0; i < n; i++) { 
    double v = fabs(volt[i] - avg); 

    if (v > avg10) { 
     if (nerror == 0) { 
      puts("The following problems occurred:"); 
     } 

     nerror++; 
     printf("%d. Voltage at hour %d was %.2f volts " 
      "(diffrence of %.2f volts)\n", 
      nerror, i + 1, volt[i], v); 
    } 
} 

for (i = 1; i < n; i++) { 
    double diff = fabs(volt[i - 1] - volt[i]); 

    if (diff > avg15) { 
     if (nerror == 0) { 
      puts("The following problems occurred:"); 
     } 

     nerror++; 
     printf("%d. Voltage change from hour %d to " 
      "hour %d was %.2f\n", 
      nerror, i, i + 1, diff); 
    } 
} 

if (nerror == 0) puts("No problems were encountered."); 
+0

您可以使用优化版本。 –

0

谢谢大家,我的问题得到了解决。快乐的编码!

代码是:

#include <stdio.h> 
#include <math.h> 


int i; 
float volt[6]; 
float avg, avg10, avg15, total, a, b; 

    int main() { 
    int voltageproblem1 = 0; 
    int voltageproblem2 = 0; 
    total= 0 ; 
    avg = 0; 
    printf("Enter 6 Volts of Machine\n"); 

    for (i=0; i<6; i++) { 
    printf("Type %d. volt", i+1); 
    scanf("%f",&volt[i]); 

    total = total + volt[i]; 
} 
avg = total/6; 
avg10 = (avg * 10)/100; 
avg15 = (avg * 15)/100; 
printf("------------------------------------------\n"); 
printf("The machine Avarage Voltage is %.1f\n", avg); 
printf("The Machine Avarage is%.1f\n", avg10); 
printf("The Machine 15 Avarage is%.1f\n\n\n", avg15); 


    for (i=0;i<6;i++) { 
     a = fabs(volt[i] - avg); 

     if(a > avg10) { 
    printf("\nVoltage at hour %d was %.1f volts (diffrence of %.1f volts)\n\n", i+1, volt[i], a); 
    voltageproblem1 =1; 
     } 
    } 
    for (i=0; i<5; i++) { 

     b = fabs(volt[i+1] - volt[i]); 
     if(b > avg15) { 
    printf("\nVoltage change from hour %d to hour %d was %.1f\n\n", i+1, i+2, b); 
    voltageproblem2 = 1; 
     } 
    } 
    if ((voltageproblem1==0)&&(voltageproblem2==0)) { 
    printf("No problems were encountered.\n\n"); 
     } 

}