嵌套for循环已停止工作

时间:2016-03-17 23:14:47

标签: 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];
}

2 个答案:

答案 0 :(得分:1)

<强> 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,然后只需更改一次。

答案 1 :(得分:1)

两个错误:

    x中的
  1. GetValue未初始化,因此temp[x]越过边界,因此程序遇到分段错误。
  2. 当您定义temp[x]时,x的值将取消定义,temp超出程序的堆栈。
  3. 正确的可能是这样的:

    int GetValue(int value[])
    {
        int x;
        x=1;
       int  temp[x];
    
    
        temp[x-1] = (int)((rand()%(100-60+1))+60);
    
        return temp[x-1];
    }
    

    结果显示:

    Temperature Conditions on October 9, 2015:
    Time of Day     Temperature in Degrees F
            0                                       65
            1                                       96
            2                                       60
            3                                       83
            4                                       67
            5                                       79
            6                                       66
            7                                       92
            8                                       83
            9                                       77
            10                                      87
            11                                      66
            12                                      77
            13                                      66
            14                                      68
            15                                      82
            16                                      74
            17                                      79
            18                                      63
            19                                      73
            20                                      86
            21                                      70
            22                                      80
            23                                      81
            24                                      80
    
    Midnight                                77
    
    Maximum Temperature for the day: 96 Degrees F at 1
    Minimum Temperature for the day: 60 Degrees F at 2
    Average Temperature for the day: 76.00 Degrees F
    
相关问题