c - C 编程中变量过多导致的错误
问题描述
我想做正数和负数的算术平均值,用户给出数字。我想再放 2 个变量来计算在正面和负面方面相加了多少个数字,然后做算术平均值。
但是当我把它们放进去时int x=0, q=0;
,程序停止工作,编译器没有任何错误。为什么?
int total, i, numere[total], negativeSum = 0, positiveSum = 0;
printf("The number of digits you want to calculate the arithmetic amount: : ");
scanf("%d",&total);
for(i=0; i<total; i++){
printf("Enter number %d : ",(i+1));
scanf("%d",&numere[i]);
}
for(i=0; i<total ; i++){
if(numere[i] < 0){
negativeSum += numere[i];
}else{
positiveSum += numere[i];
}
}
解决方案
在你的陈述序列中
int total, i, numere[total], negativeSum = 0, positiveSum = 0;
printf("The number of digits you want to calculate the arithmetic amount: : ");
scanf("%d",&total);
total
仍然未初始化,因此numere[total]
未定义。编译器可能会完全删除它。为了total
初始化为定义numere
,您必须在阅读后声明它total
:
int total, i, negativeSum = 0, positiveSum = 0;
printf("The number of digits you want to calculate the arithmetic amount: : ");
scanf("%d",&total);
int numere[total]; // now it is well-defined.
推荐阅读
- angular - 角度 *ngIf 中的 AND 运算符计算结果为 true,而它不应该
- python - Flask-wtforms 无法识别要在 Flask 应用程序中上传的附件
- android - 未创建接收方视图
- javascript - 如果可用空间,CSS Grid 自动放置子级
- terraform - Terraform - 重复输出定义,但没有重复输出
- mysql - 来自不同表的MySQL多个AND条件
- android - 如何单击flutter_facebook_login接受所有cookie弹出对话框与flutter小部件测试器
- asp.net-mvc - ASP.NET MVC 5 编辑器模板
- firebase - 为什么使用 Firebase 时我的广告加载缓慢?
- python - Selenium,模仿真实用户的最佳方式是什么?