【发布时间】:2020-10-04 17:21:14
【问题描述】:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define NUMS 3
int main(void) // call main
{
int high[NUMS] = { 0 }; // single int for high
int low[NUMS] = { 0 }; // single int for low
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
double f = 0;
double total = 0;
double avg = 0;
int i = 0;
for (i = 0; i < NUMS; i = i + 1) // for loop running to have the user input 3 day's worth of temperatures
{
printf("Enter the high value for day %d: ", i + 1);
int tempHigh = scanf("%d", &high[i]);
printf("Enter the low value for day %d: ", i + 1);
int tempLow = scanf("%d", &low[i]);
while (high[i] < low[i] || high[i] > 40 || low[i] < -40) // while loop checks that each temperature fits the criteria
{
printf("Incorrect values. Try again.\n");
printf("Enter the high value for day %d: ", i + 1);
int tempHigh2 = scanf("%d", &high[i]);
printf("Enter the low value for day %d: ", i + 1);
int tempLow2 =scanf("%d", &low[i]);
}
}
a = (double)high[0]; // each temperature stored as its own variable outside of the array and converted to double
b = (double)high[1];
c = (double)high[2];
d = (double)low[0];
e = (double)low[1];
f = (double)low[2];
total = a + b + c + d + e + f; // calculating total
(double)avg = total / ((double)NUMS * 2); // calculating average
printf("The average temperature of the three days is: %p", &avg); // printing average.
return 0;
}
当我从数组中打印出平均值、总数或直接值时,它最终会打印为一串混合字符,我不知道为什么。没有错误或警告可供我处理,因此我发现很难找出问题所在。
【问题讨论】:
-
...%p", &avg)正在输出该变量的 地址。我建议...%f", avg)。函数printf()与scanf()相似,但完全不同。 -
输出中有什么,你不想要什么?
-
@WeatherVane 我将其更改为 %f,现在它会打印数字,但它只会导致 0,所以我认为这些值不会被存储
-
将违规行更改为
printf("The average temperature of the three days is: %f", avg);后无法重现。这是两个变化。当然,平均值可能是0.000000,但不会是你声称的0。 -
@WeatherVane 我每天的测试值是 5 和 2,所以平均值绝对不是 0。