For-loop不打印数组

时间:2016-09-21 20:30:18

标签: c

我已经成功地对values进行了排序,但是当尝试使用for-loop或printf打印它们时,程序什么都不做。

int main(void){
    int values[] = {23,19,1};
    int size = 3;
    int counter = 0;
    int i;
    int holder;

    for(i = 0; i < size; i++){
        counter = 0;
        if(values[i] > values[i + 1]){
            holder = values[i];
            values[i] = values[i + 1];
            values[i + 1] = holder;
            counter = 1;
            printf("index: %i = %i, index: %i = %i\n", i, values[i], i + 1, values[i + 1]);
            printf("Sorting..\n");
        }
        if((i + 2) == size){
            if(counter == 0){
                printf("The array is sorted! %i, %i, %i\n", values[0], values[1], values[2]);
                return true;
                break;
            }
            else {
                i = -1;
            }
        }
    }

    printf("%i\n", values[0]);

    for(i = 0; i < size; i++){
        printf("%i\n", values[i]);
    }
    return 0;
}

节目输出是:

index: 0 = 19, index: 1 = 23
Sorting..
index: 1 = 1, index: 2 = 23
Sorting..
index: 0 = 1, index: 1 = 19
Sorting..
The array is sorted! 1, 19, 23

1 个答案:

答案 0 :(得分:2)

数组排序后的return true弹出main函数,因此底部的打印代码永远不会到达。