c - 数组中的随机数、最大值、最小值、平均值

标签 c arrays random max minimum

因此,我通过创建一个 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);

};

最佳答案

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

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

您还通过了 get_value() 中的 int 参数,其参数为 void... 在 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);

};

还有不要使用全局变量,除非局部变量失败...当您执行一些大型代码和文件处理时,这将有很大帮助

关于c - 数组中的随机数、最大值、最小值、平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35954215/

相关文章:

C 比较运算符优先级

php - 从每个数组中回显特定行

javascript - 如何使用 window.crypto.getRandomValues 获取特定范围内的随机值

drand48() 能返回 1 吗?

在循环控制条件内调用 void 函数

c - 反调试 : gdb does not write 0xcc byte for breakpoints. 知道为什么吗?

c - 简单的Linux管道程序,收不到数据

c - 通过C中的数组名称和指针访问2D数组元素

arrays - Swift 数组中的 AnyType 错误

python - 在python中设置程序范围内的随机种子