数组中的随机数,最大值,最小值,平均值
Random numbers in array, max, min, average
因此,我通过在 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);
};
碰巧这条线
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]);
}
首先伙计使用循环打印 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 因为它已经是全局的......不需要传递数字类型整数,因为你没有使用任何这样的东西......不需要使用 return 类型作为 int 因为你不需要 return 任意整数...
最后的建议...找一本关于函数的好书
因此您的最终代码将如下所示:-
#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);
};
也除非局部变量失败,否则不要使用全局变量...这在您执行一些大代码和文件处理时会有很大帮助
您没有为 temp[i] 分配任何值?
for (i=0; i < 25; i++) {
temp[i] = get_value(i);
sum += temp[i];
}
因此,我通过在 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);
};
碰巧这条线
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]);
}
首先伙计使用循环打印 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 因为它已经是全局的......不需要传递数字类型整数,因为你没有使用任何这样的东西......不需要使用 return 类型作为 int 因为你不需要 return 任意整数...
最后的建议...找一本关于函数的好书
因此您的最终代码将如下所示:-
#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);
};
也除非局部变量失败,否则不要使用全局变量...这在您执行一些大代码和文件处理时会有很大帮助
您没有为 temp[i] 分配任何值?
for (i=0; i < 25; i++) {
temp[i] = get_value(i);
sum += temp[i];
}