【发布时间】:2015-10-19 03:27:43
【问题描述】:
我必须在 25x2 表中打印出数组的结果,但棘手的部分是数组必须在 main 中,而用户定义的函数必须输出数组。我不确定如何开始,更不用说获得所需的输出了:
Temperature Conditions on October 9, 2015:
Time of Day Temperature in degrees F
0 85
1 80
2 97
3 90
4 73
........
Midnight 68
这是我目前的代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_value (void)
{
return rand()%41+60;
}
int calc_results (int a[], int n)
{
int i, j, maximum, minimum, average,sum;
printf ("Temperature conditions on October 17th, 2015:\n");
int a[25][2]; //not sure what to do here
for(i = 0; i<25;i++)
{
for(j=0;j<2;j++)
printf ("%d\n"), a[i][j];
}
maximum = a[0];
for(i = 0; i < n; i++)
{
if(maximum < a[i])
maximum = a[i];
}
printf("Maximum Temperature for the day: %d\n", maximum);
minimum = a[0];
for(i = 0; i < n; i++)
{
if(minimum > a[i])
minimum = a[i];
}
printf("Minimum Temperature for the day: %d\n", minimum);
float avg;
sum = 0;
for(i = 0; i < 25; i++)
sum+=a[i];
avg=(float)sum/25;
printf("Average Temperature for the day: %f\n", avg);
return 0;
}
int main ()
{
srand(time(NULL));
int average,i;
//printf("These are the temperatures for the day\n");
int temp[25];
for (i=0;i<25;i++)
{
temp[i]= get_value();
printf("%d\n",temp[i]); //this prints out the temp results correctly but as a one dimensional array
}
calc_results(temp,i);
return 0;
}
所有这些循环都必须在 calc_results 中完成,我不能创建另一个用户定义的函数或将它们中的任何一个移到 main 中。任何帮助表示赞赏!此时我只想将它放入一个表格中,我不介意输出是否 100% 相同。
我也是编程新手,这是我第一次做这样的事情,所以如果我忽略了一些明显的错误或完全破坏我的代码,我真的很抱歉。
【问题讨论】:
-
你不需要二维数组。使用您传递的数组来函数为什么重新声明。而且在你的函数
calc_results中有一个错字在for loop中的printf。
标签: c arrays output user-defined-functions