函数 hot_days(),有两个参数:当前月份的气温数量和存储气温的数组。搜索 temp 数组并计算中午温度超过 32 的所有天数。返回此计数。
检查代码末尾的 hot_days 函数。 icounter 不工作,我认为问题出在 x < 32 上。
#include <stdio.h>
#include <conio.h>
#define HIGH 32
//function prototypes
void read_temps(int []);
void hot_days(int [], int []);
int main()
{
//Array
int TempsAry[31];
int hotAry[31];
//sending array
read_temps(TempsAry);
//sending hot array
hot_days(TempsAry, hotAry);
getch();
return 0;
}
void read_temps(int TempsAry [])
{
// variables
int tempnum ;
int x = 0 ;
int icount = 0;
while (x < 31)
{
// entering temperature
printf("Please enter today's temperature:\n ");
scanf("%d",&tempnum);
if (tempnum <= -500)
{
break;
}
TempsAry[x] = tempnum;
++x;
icount = icount +1;
}
//outputting array
for (x = 0 ; x<icount; ++x)
{
printf("%d\n",TempsAry[x]);
}
}
void hot_days(int TempsAry [], int hotAry [])
{
int x = 0 ;
int icount = 0;
while (x<31)
{
if (TempsAry[x] > HIGH)
{
hotAry[x] = TempsAry[x];
++icount;
}
++x;
}
printf("%d\n", icount);
}
最佳答案
这段代码没有任何问题,它工作正常。在 ideone 上查看代码片段。
关于c - 在我的函数数组中没有获得正确的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828642/