【问题标题】:Nested for loop has stopped working嵌套 for 循环已停止工作
【发布时间】: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


【解决方案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,那么只需更改一次。

【讨论】:

  • 当我加入您对 GetValue 函数的建议时,我得到一个错误
  • main上面的减速改了吗?
【解决方案2】:

两个错误:

  1. GetValue 中的x 没有初始化,所以temp[x] 越界了,所以程序遇到分段错误。
  2. 定义temp[x]时,x的值为未定义,temp超出程序的堆栈。

正确的可能是这样的:

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

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多