icebitch
V2EX  ›  C

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

  •  
  •   icebitch · Mar 19, 2016 · 4447 views
    This topic created in 3882 days ago, the information mentioned may be changed or developed.

    #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);
    }
    

    这是啥原因呢?

    5 replies  •  2016-03-19 20:35:31 +08:00
    Andiry
    Andiry
    Mar 19, 2016 via Android
    1
    循环跑了 11 次,数组越界
    just4test
    just4test
    Mar 19, 2016
    2
    使用 while(++i < 10)
    icebitch
    icebitch
    OP
    Mar 19, 2016
    3
    @Andiry
    @just4test 明白了 !!! 谢谢!
    chairuosen
    chairuosen
    Mar 19, 2016
    4
    v2ex 啥时候代码能高亮了?
    icebitch
    icebitch
    OP
    Mar 19, 2016
    5
    @chairuosen Markdown~!~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3033 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 04:40 · PVG 12:40 · LAX 20:40 · JFK 23:40
    ♥ Do have faith in what you're doing.