【问题标题】:Can anyone tell me why my sorting doesn't work? (c code)谁能告诉我为什么我的排序不起作用? (c 代码)
【发布时间】:2016-12-26 06:15:48
【问题描述】:

我刚开始编程。 我刚写了这段代码,它不起作用。 你能告诉我为什么吗?

代码 =

int main(int argc, const char *argv[])
{
    int a, b, c, d, e, f, g, h, i, j;
    int s;

    printf("enter 10 numbers: ");
    scanf("%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);

    int numbers[10] = {a, b, c, d, e, f, g, h, i, j};

    //%d %d %d %d %d %d %d %d %d %d        a, b, c, d, e, f, g, h, i, j
    printf("before \n %d %d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i, j);

    for (int k = 0; k == 10; k++) {
        if (numbers[k] <= numbers[k + 1]) {
            numbers[k] = s;
            numbers[k] = numbers[k + 1];
            numbers[k + 1] = s;

        }
    }

    printf("after \n %d %d %d %d %d %d %d %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7], numbers[8], numbers[9]);

    return 0;
}

【问题讨论】:

  • 这直接违反了如何提问规则))
  • 它不起作用是什么意思?什么不工作?当您尝试运行代码时会遇到什么错误?一旦您在帖子中说明了这一点,您就可以从社区获得一些帮助。
  • 1.打开你的编译器警告。这至少会暴露你的一个问题。 2. 在调试器中运行代码,单步执行并检查变量值以跟踪代码中发生的情况。这至少会暴露一个问题。

标签: c sorting


【解决方案1】:

好吧,对于初学者,我很确定您的 scanf 函数的输入是错误的。第一个 '%d' 将咀嚼字符串中的所有数字,因此变量 b、c、d、e、f、g、h、i 和 j 将未初始化。

此外,当 's' 尚未初始化为任何内容时,您正在执行 numbers[k] = s;numbers[k + 1] = s;

【讨论】:

    【解决方案2】:

    上面的代码有很多错误。首先,您的“for”条件是错误的(k==10)。其次,仅遍历数组一次是不够的。您需要多次遍历数组,直到不需要进行交换。 (见冒泡排序)

    【讨论】:

      【解决方案3】:

      您的 for 循环条件在第一个实例中变为下降,在第一步 k 为 0 并且其检查值 10 (if (k==10) in (k=0;k==10;k++)) 这将不会执行您的 for 循环逻辑,并且首先您从 for 循环中出来。

      【讨论】:

        【解决方案4】:

        帮助您了解您的排序代码可能有什么问题

        英文/伪代码:

        Read 10 elements of the array
        For each 11 elements ( 0,1,2,3,4,5,6,7,8,9,10 ) of the array 
            if the current element is less of equals to the next element ( including the 12th element when the element index is 11 )
                then
                    Store the value in 's' in place of the current element ( what is in 's' might be up to the phase of the moon )
                    Store the value of the next element in the current element ( we will never know what was the the mystery value in 's', unless .... )
                    Store the value of 's' in the next element ( next comparison will be a big surprise )
        
        
        Print the 10 elements of the transformed array
        

        公平地说,可能仍然不足以表明你的错误

        我们来个卡片类比

        • 将你的 52 张牌洗牌
        • 从牌堆中挑选 10 张牌 (7 10 5 4 ...)
        • 查看您手上的第一张牌(例如钻石 7)
        • 看下一个(例如10的俱乐部)
        • 如果第一个小于或等于第二个,则:
          • 将第一张牌替换为牌组的顶牌(例如:红心皇后)
          • 扔掉第一张牌
          • 用第二张卡替换新卡
          • 把新卡放在第二位
        • 看第二张牌(红心皇后)
        • 看第三张牌(黑桃5)
          ...让我们跳到第 10 张卡片
        • 看第10张卡片(好的....)
        • 看第 11 张卡片(你扭曲维度可以看到第 55 维度的另一张卡片,它是玫瑰的 XZ)
        • 比较数值(通过应用多维数学,你会发现施罗丁格的猫是活生生的)
        • 太阳离地球越来越近,导致一些奇怪的冰河时代持续一秒钟 ...
        • 那些名字无人能说的人会给你带来第 12 张牌 ...
        • 你被一口粥吃掉了

        除了尝试查找不存在的值之外,有些人建议您查看 simple sorting algorithm 提供一些易于理解的伪代码

        祝你在学习 C 的过程中好运

        【讨论】:

          猜你喜欢
          • 2023-03-24
          • 1970-01-01
          • 2020-04-29
          • 2010-12-12
          • 1970-01-01
          • 1970-01-01
          • 2019-02-15
          • 1970-01-01
          • 2013-05-08
          相关资源
          最近更新 更多