【发布时间】:2016-03-17 23:14:47
【问题描述】:
此处的代码旨在获取随机数(温度)并创建一个表格,其中包含记录温度的相应小时。这是我应该收到的输出示例。
Temperature Conditions on October 9, 2015:
Time of Day Temperature in degrees F
0 85
1 80
2 97
3 90
4 68
5 75
6 77
7 98
8 97
9 62
etc...
Maximum Temperature for the day: <whatever> Degrees F
Minimum Temperature for the day: <whatever> Degrees F
Average Temperature for the day: <whatever.whatever> Degrees F
我的问题是,当我运行代码时,会出现一个对话框,说程序已停止工作,我不知道为什么。
我们将不胜感激所有帮助。
#include <stdio.h>
#include <stdlib.h>
int GetValue(int[]);
int main() {
int x, n, max = 0,min = 100, ArrayNumMax, ArrayNumMin, temperature[25];
float sum;
float average;
int num[25];
printf("Temperature Conditions on October 9, 2015:\nTime of Day \tTemperature in Degrees F\n");
for (x = 0; x <= 24; x++) {
//if statements to get min and max
temperature[x] = GetValue(temperature);
if (temperature[x] > max) {
max = temperature[x];
ArrayNumMax = x;
}
if (temperature[x] < min) {
min = temperature[x];
ArrayNumMin = x;
}
printf("\t%d\t\t\t\t\t%d\n", x,temperature[x]);
}
//prints statements
printf("\nMidnight\t\t\t\t%d\n\nMaximum Temperature for the day: %d Degrees F at %d\nMinimum Temperature for the day: %d Degrees F at %d\n", temperature[12],max,ArrayNumMax, min, ArrayNumMin);
//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
int GetValue(int value[]) {
int x, temp[x];
temp[x] = (rand()%(100-60+1))+60;
return temp[x];
}
【问题讨论】:
-
在
GetValue()中,您在初始化之前使用自动变量int x。这是未定义的行为。 -
您还应该启用编译器警告。
GetValue完全没有意义。它传递了一个指向数组的指针,但它完全忽略了该参数。相反,它声明了一个本地数组,其大小由未定义的变量指定,但随后它像标量一样使用它。甚至很难猜测其意图可能是什么。 -
获取值的重点是获取一个随机数,本例为温度。
-
@EOF 我该如何初始化它?
标签: c user-defined-functions nested-loops