请帮我找到最高和最低温度。我想我错过了让它有效的东西。差不多完成了。我是否需要使用if创建条件? 请帮我找到最高和最低温度。我想我错过了让它有效的东西。差不多完成了。我是否需要使用if来创建条件?
#include <stdio.h>
#define NUMS 3
int main(void) {
int high;
int low;
int totalhigh=0;
int totallow=0;
double average;
int i;
printf("---=== Temperature Analyzer ===---\n");
for (i = 0; i < 3; i++)
{
printf("Enter the high temperature for the day %d: ", i + 1);
scanf("%d", &high);
printf("Enter the low temperature for the day %d: ", i + 1);
scanf("%d", &low);
totalhigh = high + totalhigh;
totallow = low + totallow;
}
average = (double)(totalhigh + totallow) / 6 ;
printf("The average (mean) temperature was: %.2lf\n", average);
return 0;
}
答案 0 :(得分:1)
与我输入时收到的评论没有什么不同。
#include <stdio.h>
#define NUMS 3
int main(void) {
int high;
int low;
int highest =-42;
int highday = -1;
int lowday = -1;
int lowest = 42;
int totalhigh=0;
int totallow=0;
double average;
int i;
printf("---=== IPC Temperature Analyzer ===---\n");
for (i = 0; i < 3; i++)
{
printf("Enter the high value for day %d: ", i + 1);
scanf("%d", &high);
printf("Enter the low value for day %d: ", i + 1);
scanf("%d", &low);
printf("\n");
totalhigh = high + totalhigh;
totallow = low + totallow;
if (high>highest)
{ highest=high;
highday=i+1;
}
if (low<lowest)
{ lowest=low;
lowday=i+1;
}
}
average = (double)(totalhigh + totallow) / 6 ;
printf("The average (mean) temperature was: %.2lf\n", average);
printf("The lowest temperature was: %3d on day %d\n", lowest, lowday);
printf("The highest temperature was: %3d on day %d\n", highest, highday);
return 0;
}
答案 1 :(得分:0)
您需要记录最高(以及最低)。
struct hit
{
int value,
day;
};
struct hit highest = {-40; 0}, lowest = {+40; 0};
....
if (high > highest.value)
{
highest.value = high;
highest.day = i;
}
...
最后,您可以打印已创建的值:
print("The highest has been %d on day %d\n", highest.value, highest.day);