Search code examples
carraysfunctionsortingbubble-sort

i cant get bubble sorting to work in function while passing array to the function


#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]);
    }


}

the code fails at the the first if statement in the function, it says segmentation error. im not sure why, i think there may be an error in how i am passing the array to the function.


Solution

  • /******************************************************************************
    
                                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]);
        }
    
    }
    

    Running your code slightly modified (make it compile able) in https://www.onlinegdb.com/online_c_compiler#

    I was not able to detect any error

    I checkt 3,7,8,8,9,10,11,200,317 and 1,1,1,1,1,1,1,1,1