【发布时间】:2016-03-18 02:23:37
【问题描述】:
此代码只是生成一个随机数作为温度并每小时记录一次。我的 for 循环在获取代码的最大值和最小值时遇到问题。它对我来说看起来是正确的,并且与我看到的所有示例相似,但它给了我错误的输出。
谢谢
#include <stdio.h>
#include <stdlib.h>
void GetValue(int[], int x);
#define array_size 25
int main() {
int x, max, min, temperature[25];
float sum;
float average;
int array[array_size];
printf("Temperature Conditions on October 9, 2015:\n");
printf("Time of Day \t Temperature in Degrees F\n");
for (x = 0; x < 25; x++) {
//if statements to get min and max
GetValue(temperature, x);
if (temperature[x] > max) {
max = temperature[x];
}
if (temperature[x] < min) {
min = temperature[x];
}
printf("%d \t \t \t %d\n", x,temperature[x]);
}
//prints statements
printf("\nMaximum Temperature for the day: %d Degrees F\nMinimum Temperature for the day: %d Degrees F\n", temperature[12],max, min);
//adds up all temps
sum=0;
for (x=0;x<25;x++){
sum=(sum+temperature[x]);
}
//prints and creates average
average=sum/25;
printf("Average Temperature for the day: %.2f Degrees F\n",average);
return 0;
}
//gets values and puts them into array
void GetValue(int value[], int x) {
value[x] = (rand()%(100-60+1))+60;
}
【问题讨论】:
-
你认为
max和min的值在循环的开头是什么?
标签: c loops if-statement for-loop nested