首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2 For循环一个结果

2 For循环一个结果
EN

Stack Overflow用户
提问于 2016-05-03 18:16:16
回答 3查看 125关注 0票数 0

我需要你的智慧。看起来不是什么大问题,但我需要一个方法。首先,我将分享代码。这段代码是正确的,但我需要一些补充,里面的循环有标准,如果电压大于百分比,它是可以的,但都是正确的,我只需要写一次。我有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.6V)。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伏。

没有遇到任何问题。

代码语言:javascript
复制
#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);
       }
   }
EN

回答 3

Stack Overflow用户

发布于 2016-05-03 18:40:42

如果你只需要一个循环,试着这样做:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-05-03 19:35:03

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

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

  • 无论何时打印错误消息,都要增加错误数。
  • 执行此操作之前,请检查这是否是报告的第一个错误。如果是,请打印标题(“以下错误发生”)
  • 如果您已检查所有内容且未发生错误,请打印成功消息。

或者,在代码中:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-05-03 20:53:57

谢谢大家,我的问题已经解决了。祝你编码愉快!

代码为:

代码语言:javascript
复制
#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");
       }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37001445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档