0

# include<stdio.h>

int main(void)
{
int x;
double sum = 0;
int cnt;
int number[100];
printf("请输入数字:");
scanf("%d", &x);
while (x != -1) {
number[cnt] = x;
sum += x;
cnt++;
scanf("%d", &x);
}
if (cnt > 0) {
double average = sum / cnt;
for (int i = 0; i < cnt; i++) {
if (number[i] > average) {
printf("%d ",number[i]);
}
}
}

``````return 0;
``````

}

0

cnt没有初始化，后续直接使用 `number[cnt] = x;` ，这一点就不可以吧。改成`int cnt = 0;` 后我这里就能正常运行了。

@新手小白。: 调试吧

@给给龙: 找到问题了，一调试看出来了，万分感谢！

0

0

# include<stdio.h>

int main(void)
{
int x;
double sum = 0;
int cnt;
int number[100];
printf("请输入数字:");
scanf("%d", &x);
while (x != -1) {
number[cnt] = x;
sum += x;
cnt++;
scanf("%d", &x);
}
if (cnt > 0) {
double average = sum / cnt;
for (int i = 0; i < cnt; i++) {
printf("num:%d ,ave:%d",number[i],average);
if (number[i] > average) {
printf("%d ",number[i]);
}
}
}

return 0;
}

printf("num:%d ,ave:%d",number[i],average);这一句输出了

0

echo_lovely | 园豆：311 (菜鸟二级) | 2021-08-11 17:16
``````#include <stdio.h>
int main(void)
{
int x;
double sum = 0;
int cnt = 0;//加了0
int number[100];
printf("请输入数字:");
scanf("%d", &x);
while (x != -1)
{
number[cnt] = x;
sum += x;
cnt++;
scanf("%d", &x);
}
if (cnt > 0)
{
double average = sum / cnt;
int i = 0;//我这ide不能在for里声明变量
for (; i < cnt; i++)
{
if (number[i] > average)
{
printf("%d ", number[i]);
}
}
}
return 0;
}
``````

## 养成好习惯，变量声明的时候顺手初始化变量的值

vs2019 devc++都用过了，这是一个练习题，要求输出大于平均值的数字

0

cnt没有初始化，直接访问number[cnt]=x有风险

您需要登录以后才能回答，未注册用户请先注册