icebitch
V2EX  ›  C

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

  •  
  •   icebitch · Mar 19, 2016 · 4112 views
    This topic created in 3733 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
        1
    Andiry  
       Mar 19, 2016 via Android
    循环跑了 11 次,数组越界
    just4test
        2
    just4test  
       Mar 19, 2016
    使用 while(++i < 10)
    icebitch
        3
    icebitch  
    OP
       Mar 19, 2016
    @Andiry
    @just4test 明白了 !!! 谢谢!
    chairuosen
        4
    chairuosen  
       Mar 19, 2016
    v2ex 啥时候代码能高亮了?
    icebitch
        5
    icebitch  
    OP
       Mar 19, 2016
    @chairuosen Markdown~!~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3125 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:33 · PVG 21:33 · LAX 06:33 · JFK 09:33
    ♥ Do have faith in what you're doing.