c - 我的程序有几个错误,我不知道如何修复它们

标签 c arrays loops

我必须按升序对数组中的三个用户输入的数字进行排序。有几个错误。

A) 我将其设置为只要求三个数字,但我必须输入 4,即使程序只对前三个进行排序。

B)我收到一条警告,说我的变量 temp 未使用,尽管它显然是这样。

C) 程序对前两个数字进行排序,然后再次打印第二大的数字,而不是最大的数字。

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

int main(){

    int numbers = 3;
    int NUM_ARRAY[numbers];
    int counter = 0;
    int sort = 0;
    int temp = 0;

                printf("Please enter three integers\n");
            for(counter = 0; counter < numbers; counter++){
                scanf("%d ", &NUM_ARRAY[counter]);
            }
                printf("Original Numbers\n");
            for(counter = 0; counter < numbers; counter++){
                printf("%d\n", NUM_ARRAY[counter]);
            }

            while(1){
                sort = 0;
                for(counter = 0; counter < numbers-1; counter++){
                    if(NUM_ARRAY[counter] > NUM_ARRAY[counter+1]){
                        int temp = NUM_ARRAY[counter];
                        NUM_ARRAY[counter] = NUM_ARRAY[counter+1];
                        NUM_ARRAY[counter+1] = NUM_ARRAY[counter];
                        sort = 1;

                    }
            }
            if(sort == 0){
                break;
            }

        }
            printf("Sorted Numbers\n");
                for(counter = 0; counter < numbers; counter++){
                   printf("%d\n", NUM_ARRAY[counter]);
                }

    return 0;
}

最佳答案

您应该将温度设置为 NUM_ARRAY[counter+1] 而不是 NUM_ARRAY[counter]

试试这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

int main(){

    int numbers = 3;
    int NUM_ARRAY[numbers];
    int counter = 0;
    int sort = 0;
    int temp = 0;

                printf("Please enter three integers\n");
            for(counter = 0; counter < numbers; counter++){
                scanf(" %d", &NUM_ARRAY[counter]); // changed spacing on %d
            }
                printf("Original Numbers\n");
            for(counter = 0; counter < numbers; counter++){
                printf("%d\n", NUM_ARRAY[counter]);
            }

            while(1){
                sort = 0;
                for(counter = 0; counter < numbers-1; counter++){
                    if(NUM_ARRAY[counter] > NUM_ARRAY[counter+1]){
                        int temp = NUM_ARRAY[counter];
                        NUM_ARRAY[counter] = NUM_ARRAY[counter+1];
                        NUM_ARRAY[counter+1] = temp;  <--- /* Since NUM_ARRAY[counter] will be set to next value in the previous step */
                        sort = 1;

                    }
            }
            if(sort == 0){
                break;
            }

        }
            printf("Sorted Numbers\n");
                for(counter = 0; counter < numbers; counter++){
                   printf("%d\n", NUM_ARRAY[counter]);
                }

    return 0;
}

关于c - 我的程序有几个错误,我不知道如何修复它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198716/

相关文章:

c++ - char* 数组的问题

optimization - 如何优化 MATLAB 循环?

c - 从 stdin (C) 获取多个文件的文件大小

c - 使用 -std=c99 编译时,我需要使用 -pedantic 还是 -ansi?

c - gprof 中的问题(始终记为零)

PHP Ajax 分页数组不起作用

php - 使用两个不同的 foreach 循环 PHP/SQL 添加到同一个数组

c - 用厘秒修复 FPS

c# - 循环速度有什么问题?

ruby - 这个 "Telephone Words"算法的大 O 复杂度是多少?