2 For循环一个结果

时间:2016-05-03 10:16:16

标签: c

我需要你的智慧。看起来不是一个大问题,但我需要一种方法。 首先,我将分享代码。这段代码是正确的,但我需要一些补充,如果电压大于百分比,那么for循环内部有标准,但是一切都正确我只需要一次写。我有2个循环,但只需要一个提示。 如果令人困惑,我可以分享原始问题。谢谢你们。

我提出原始问题:

电压读数每小时从变电站获得一次,持续六个小时(因此有六个 读数)。编写C程序以对变电站执行以下检查: a)显示与平均值相差超过平均值10%的所有电压。 b)显示所有成对的连续小时,其中一小时的电压发生变化     到下一个是平均值的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);
       }
   }

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

如果要在未遇到任何问题时打印出消息,则必须记住是否报告了任何错误或有多少错误。当然,你不能在循环中打印出这样的消息,因为说&#34;没有发生错误&#34;八次并报告错误是有点矛盾的。

您的预期输出显示了错误的枚举,因此保留错误计数是个好主意。请按以下步骤操作:

  • 每当您打印错误消息时,请增加错误计数。
  • 在执行此操作之前,请检查这是否是报告的第一个错误。如果是,请打印标题(&#34;发生以下错误&#34;)
  • 如果您已检查所有内容且未发生任何错误,请打印成功消息。

或者,在代码中:

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.");

答案 2 :(得分: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");
       }

}