运行结果正确,但还是会弹 DebugError , 题目:输入 10 个实数存入数组 , 求其中最小数 及其序号 (序号从 1 开始)

2016-03-19 18:00:52 +08:00
 icebitch

#include <stdio.h>
#include <math.h>
void main() {
	float ar[10], min;
	int i = 0,index;
	do {
		scanf_s("%f", &ar[i]);
		if (i == 0) {
			min = ar[i];
			index = i;
		}
		else if (ar[i] < min) {
			min = ar[i];
			index = i;
		}
	} while (i++ < 10);
	printf("min=%f,index=%d\n",min,index+1);
}

这是啥原因呢?

3354 次点击
所在节点    C
5 条回复
Andiry
2016-03-19 18:47:11 +08:00
循环跑了 11 次,数组越界
just4test
2016-03-19 20:00:45 +08:00
使用 while(++i < 10)
icebitch
2016-03-19 20:30:50 +08:00
@Andiry
@just4test 明白了 !!! 谢谢!
chairuosen
2016-03-19 20:34:27 +08:00
v2ex 啥时候代码能高亮了?
icebitch
2016-03-19 20:35:31 +08:00
@chairuosen Markdown~!~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/264763

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX