c - 使用用户定义的函数用随机数/错误填充数组

标签 c arrays random initialization

当然,除了 printf() 语句之外,我的程序在执行任何内容之前就崩溃了。我还试图包含一个用户定义函数,但我做错了什么,因为它给了我未定义的错误,我在 int main() 上面有它,但它仍然标记相同错误。我需要随机数来填充数组。

#include <stdio.h>
#include <stdlib.h>

#define ARR SIZE    25
#define MAXIMUM     100
#define MINIMUM     60

int main ()
{
int temp_sum;               //Initializes sum of all temperatures in order to calculate average total.
int temp_min, temp_max;     //Initializes minimum and maximum temperature variables.
int hour_count;             //Initializes actual hour of the day.
int temperature[25];        //Initializes array to have 25 elements.
int x, maximum, minimum;
float temp_avg;             //Sets variable to accept a decimal.

hour_count=0;

printf("Temperature Conditions on October 9th, 2015:\n");
printf("Time of Day \t \t Temperature in Degree Farenheit\n");

//Starts for loop to add one to each hour of the day.
for (hour_count=0; hour_count<=24; hour_count++)
    {

    if (temperature[x]>maximum)
        {
            temp_max=temperature[x];
            temp_max=x;
        }
    if (temperature[x]<minimum)
        {
            temp_min=temperature[x];
            temp_min=x;
        }

    printf("%d \t\t\t %d\n", hour_count, temperature);
    }

temp_sum=0;

for (x=0; x<=24; x++)
        {
            temp_sum=temp_sum+temperature[x];
        }
 temp_avg=temp_sum/25;


void GetValue(int value[], int x)
{

value[x] = (rand()%(100-60+1))+60;
}

printf("The maximum temperature for the day is %d degrees Farenheit.\n", temp_max);
printf("The minimum temperature for the day is %d degrees Farenheit.\n", temp_min);
printf("The average temperature for the day is %d degrees   Farenheit.\n", temp_avg);

return 0;
}

最佳答案

在你的代码中,语句中使用的所有变量

  if (temperature[x]>maximum)

xtemperature[x]maximum 未初始化。

作为未初始化的自动局部变量,它们的初始值是不确定的。任何使用这些值的尝试都会调用 undefined behavior .

关于c - 使用用户定义的函数用随机数/错误填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36088316/

相关文章:

c++ - MySQL/C++ 连接器 mysql_real_escape_string 函数

c - 如何在C中更新全局链表?

java.lang.UnsatisfiedLinkError - 运行 z/OS 应用程序时

android - 方法 'GetArrayLength'/'GetByteArrayRegion' 无法在 JNI C 文件中解析

arrays - 如何使用 spring JdbcTemplate 更新 postgresql 数组列?

PHP mysql : inserting data from an array

arrays - 当我运行程序时, fgets() 被忽略,并且 scanf() 不会将数据输入到结构中

python - 如何创建特定大小的二项式数组

testing - 随机场景端到端测试

random - LSFR 随机数计数器