在将数组传递给函数时,我无法让冒泡排序在函数中工作

babySteps
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
    int size=10, numbers[size], i, order;

    for (i=0; i<10; i++)
    {
        printf("please enter a number:");
        scanf("%d", &numbers[i]);
    }
    ascending(numbers[], size);

}

void ascending(int numbers[], int size)
{
    int temp, i, sflag, count=0;

    do
    {
        sflag = 0;
        for(i=1; i <10; i++)
        {
            if (numbers[i-1] > numbers[i])
            {
                temp = numbers[i-1];
                numbers[i-1] = numbers[i];
                unmbers[i] = temp;
                sflag = 1;
            }
        }
        count++;
    }while(sflag);

    for (i=0; i<10; i++)
    {
        printf("%d\t", numbers[i]);
    }


}

代码在函数中的第一个 if 语句处失败,它表示分段错误。我不知道为什么,我认为我将数组传递给函数的方式可能存在错误。

羊羊毛
/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>



#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
    int size=10, numbers[size], i, order;

    for (i=0; i<10; i++)
    {
        printf("please enter a number:");
        scanf("%d", &numbers[i]);
    }
    ascending(numbers, size);

    return 0;
}

void ascending(int numbers[], int size)
{
    int temp, i, sflag, count=0;

    do
    {
        sflag = 0;
        for(i=1; i <10; i++)
        {
            if (numbers[i-1] > numbers[i])
            {
                temp = numbers[i-1];
                numbers[i-1] = numbers[i];
                numbers[i] = temp;
                sflag = 1;
            }
        }
        count++;
    }while(sflag);

    for (i=0; i<10; i++)
    {
        printf("%d\t", numbers[i]);
    }

}

https://www.onlinegdb.com/online_c_compiler# 中运行稍加修改的代码(使其能够编译)

我无法检测到任何错误

我检查了 3,7,8,8,9,10,11,200,317 和 1,1,1,1,1,1,1,1,1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章