【问题标题】:Random numbers in array, max, min, average数组中的随机数,最大值,最小值,平均值
【发布时间】:2016-06-27 12:35:04
【问题描述】:

因此,我通过在 60-100 的任意位置创建一个随机数生成器并将其中的 25 个存储在一个数组中来制作贫民区天气报告。然后我有一个函数可以计算最大值、最小值和平均值,并将所有这些都打印出来。

我让它运行没有错误,但我得到的只是显示中的一堆零,这意味着我在计算中的某个地方搞砸了很多时间,有什么建议吗?

此外,我正在尝试调用用户定义的函数,这就是为什么我有几个。

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


int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0;
float avg = 0;


int main () {


srand( (unsigned) time(NULL) );

for (i=0; i < 25; i++) {

get_value(i);
sum += temp[i];

}

calc_results(temp[25]);

return 0;
};



int get_value(void) {
    return((rand() % (100 - 60 + 1)) + 60);

};



int calc_results(int temp_number[], int number) {

avg = ((sum)/(25));
max = temp[0];
  for(i=1;i<25;i++){
      if(max<temp[i])
           max=temp[i];
        };
min =temp[0];
  for(i=1;i<25;i++){
      if(min>temp[i])
           min=temp[i];
  };

printf("Temperature Conditions on October 9, 2015 : \n");
printf("Time of day     Temperature in degrees F \n");
printf("     0                        %d\n",temp[0]);
printf("     1                        %d\n",temp[1]);
printf("     2                        %d\n",temp[2]);
printf("     3                        %d\n",temp[3]);
printf("     4                        %d\n",temp[4]);
printf("     5                        %d\n",temp[5]);
printf("     6                        %d\n",temp[6]);
printf("     7                        %d\n",temp[7]);
printf("     8                        %d\n",temp[8]);
printf("     9                        %d\n",temp[9]);
printf("     10                       %d\n",temp[10]);
printf("     11                       %d\n",temp[11]);
printf("     12                       %d\n",temp[12]);
printf("     13                       %d\n",temp[13]);
printf("     14                       %d\n",temp[14]);
printf("     15                       %d\n",temp[15]);
printf("     16                       %d\n",temp[16]);
printf("     17                       %d\n",temp[17]);
printf("     18                       %d\n",temp[18]);
printf("     19                       %d\n",temp[19]);
printf("     20                       %d\n",temp[20]);
printf("     21                       %d\n",temp[21]);
printf("     22                       %d\n",temp[22]);
printf("     23                       %d\n",temp[23]);
printf("     24                       %d\n",temp[24]);
printf("     25                       %d\n",temp[25]);
printf("Maximum Temperature for the day: %d Degrees F\n", max);
printf("Minimum Temperature for the day: %d Degrees F\n", min);
printf("Average Temperature for the day: %.1f Degrees F\n", avg);

};

【问题讨论】:

  • 如果出现大乱子也很抱歉!语法让我习惯了
  • calc_results(temp[25]); 这怎么可能? temp[25] 是越界数组访问。并且calc_results 被定义为采用两个参数:int calc_results(int temp_number[], int number)。猜猜应该是calc_results(temp, 25)
  • 注意:printf(" 0 %d\n",temp[0]); printf(" 1 %d\n",temp[1]); printf(" 2 %d\n",temp[2]); .... 呼唤一个循环。

标签: c arrays random max minimum


【解决方案1】:

碰巧这条线

get_value(i);

temp[i]=get_value(i);

因为您必须将随机值存储在temp[i] 中,所以您可以计算其他值。

当你传递数组时。你应该这样做--

calc_results(temp);

您可以通过for 循环打印所有数组值。像这样:

int cnt;
for(cnt = 0; cnt<=25; cnt++)
{
   printf("     %d                        %d\n",cnt,temp[cnt]);
}

【讨论】:

    【解决方案2】:

    首先,老兄使用循环打印 25 个 printf 语句...你的代码会小一点... 进行以下更改,它应该可以正常工作...

    time_t t;
    srand((unsigned) time(&t)); // this is a more standard way of using srand
    

    您也已经在参数为 void 的 get_value() 中传递了 int... 在 main 的 for 循环中执行此操作

    temp[i]=get_value();

    同时在你的代码上面声明你的函数...

    你不需要像这样的 calc_results()...

    void calc_results(void) 并且不需要传递 temp 因为它已经是全局的......不需要传递数字类型整数,因为你没有使用任何这样的东西......不需要使用返回类型作为 int 因为你不需要返回任何整数......

    最后的建议... 找一本关于函数的好书

    所以你的最终代码看起来像这样:-

        #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int get_value(void);
    void calc_results(void);
    int sum = 0;
    int min = 0;
    int max = 0;
    int temp[25];
    int i = 0;
    float avg = 0;
    
    
    int main () {
    time_t t;
    srand((unsigned) time(&t));
    for (i=0; i < 25; i++) {
    
    temp[i]=get_value();
    sum += temp[i];
    
    }
    
    calc_results();
    
    return 0;
    };
    
    
    
    int get_value(void) {
        return((rand() % (100 - 60 + 1)) + 60);
    
    };
    
    
    
    void calc_results(void) {
    
    avg = ((sum)/(25));
    max = temp[0];
      for(i=1;i<25;i++){
          if(max<temp[i])
               max=temp[i];
            };
    min =temp[0];
      for(i=1;i<25;i++){
          if(min>temp[i])
               min=temp[i];
      };
    
    printf("Temperature Conditions on October 9, 2015 : \n");
    printf("Time of day     Temperature in degrees F \n");
    for(int j=0;j<25;j++){
    printf("  %d      %d\n",i,temp[i]);
    }
    printf("Maximum Temperature for the day: %d Degrees F\n", max);
    printf("Minimum Temperature for the day: %d Degrees F\n", min);
    printf("Average Temperature for the day: %.1f Degrees F\n", avg);
    
    };
    

    除非局部变量失败,否则不要使用全局变量...这在您处理一些大代码和文件处理时会有很大帮助

    【讨论】:

    • 哇,我知道我有多糟糕了,哈哈,关于函数头的信息真的很有帮助,我几乎不知道如何传递和调用用户定义的函数>
    【解决方案3】:

    你没有给 temp[i] 赋值?

    for (i=0; i < 25; i++) {
    
    temp[i] = get_value(i);
    sum += temp[i];
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 2013-04-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 2012-04-13
      相关资源
      最近更新 更多