c - 嵌套 for 循环已停止工作

标签 c user-defined-functions nested-loops

此处的代码旨在获取随机数(温度)并创建一个表,其中包含记录温度的相应时间。这是我应该收到的输出示例。

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];
}

最佳答案

1

您在 GetValue 函数中做什么?


int GetValue(int value[])   {
    int x, temp[x];  // create an array of undeclared amount x....

    temp[x] = (rand()%(100-60+1))+60; // at invalid memory set it to this value.

    return temp[x]; // then return this memory that I have no control over
}

放弃这个,继续这个......

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

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

}

同样在主要变化之上

int GetValue(int[]);

void GetValue(int a[], int b);

然后在你的主

//if statements to get min and max

GetValue(temperature, x);
if (temperature[x] > max)    {

您还应该查看预处理器宏。 在这里阅读它们。 http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm

比如#define

#define array_size 25
int array[array_size];

然后,如果您更改代码,而不是 25,您需要 50,那么只需更改一次即可。

关于c - 嵌套 for 循环已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073354/

相关文章:

c - 有没有C的替代品?

vba - 允许在一个打开的工作簿中使用 UDF,但使其在另一工作簿中不可见/不可用/不可访问

javascript - 雪花 UDF 和数据加密

c - 我编写了一个程序来添加两个矩阵,但它不起作用。对出了什么问题有什么建议吗?

c - 通过快速浮点倒数高效计算 2**64/除数

c - 在 C 中读取执行程序标准输出的更好方法

c - 帮助!当输入 strtok 结果时,strcmp 在骗我

sql - 在多个函数之间创建序列

c++ - 内部嵌套 while 循环不会执行

c# - 这个嵌套循环如何输出一个分为 3 个部分的列表,每个部分有 3 个单词?