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

て烟熏妆下的殇ゞ 提交于 2020-03-05 03:13:32

问题


#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.


回答1:


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

                            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




回答2:


There are at least two typos in your program.

The first one is in this statement

ascending(numbers[], size);
                 ^^^

There should be

ascending(numbers, size);

The second one in this statement

unmbers[i] = temp;
^^^^^^^^

There should be

numbers[i] = temp;

Also in this statement within the function

for(i=1; i <10; i++)

you are using a magic number 10 instead of the variable size.

Nevertheless your function is inefficient because the inner loop always iterate from 1 to size.

A more efficient its implementation can look as it is shown in the demonstrative program below.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void bubble_sort( int a[], size_t n )
{
    for ( size_t last = n; !( n < 2 ); n = last )
    {
        last = 0;
        for ( size_t i = 1; i < n; i++ )
        {
            if ( a[i] < a[i-1] )
            {
                int tmp = a[i];
                a[i] = a[i-1];
                a[i-1] = tmp;
                last = i;
            }
        }
    }
}

int main(void) 
{
    enum { N = 10 };
    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i++ )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            a[i] = rand() % N;
        }

        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", a[i] );
        }
        putchar( '\n' );

        bubble_sort( a, N );

        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", a[i] );
        }
        putchar( '\n' );

        putchar( '\n' );
    }

    return 0;
}

In the program an array of random numbers is sorted 10 times. The program output might look for example like.

4 0 1 0 5 7 1 1 5 2 
0 0 1 1 1 2 4 5 5 7 

8 1 1 0 7 1 3 1 1 0 
0 0 1 1 1 1 1 3 7 8 

6 0 8 2 8 3 7 4 7 8 
0 2 3 4 6 7 7 8 8 8 

2 1 0 3 4 5 3 7 8 0 
0 0 1 2 3 3 4 5 7 8 

9 6 3 0 9 0 4 4 4 5 
0 0 3 4 4 4 5 6 9 9 

5 2 7 5 4 7 0 1 2 7 
0 1 2 2 4 5 5 7 7 7 

1 4 1 4 9 5 1 4 4 0 
0 1 1 1 4 4 4 4 5 9 

6 5 8 0 7 9 2 1 4 6 
0 1 2 4 5 6 6 7 8 9 

9 1 9 6 6 5 4 8 9 8 
1 4 5 6 6 8 8 9 9 9 

5 2 4 6 6 5 3 0 2 7 
0 2 2 3 4 5 5 6 6 7 

If you are going to use the same sorting function to sort an array in ascending and descending orders then the function can look as it is shown in the demonstrative program below.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void bubble_sort( int a[], size_t n, int cmp( int, int ) )
{
    for ( size_t last = n; !( n < 2 ); n = last )
    {
        last = 0;
        for ( size_t i = 1; i < n; i++ )
        {
            if ( cmp( a[i], a[i-1] ) )
            {
                int tmp = a[i];
                a[i] = a[i-1];
                a[i-1] = tmp;
                last = i;
            }
        }
    }
}

int ascending( int x, int y )
{
    return x < y;       
}

int descending( int x, int y )
{
    return y < x;
}

int main(void) 
{
    enum { N = 10 };
    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i++ )
    {
        a[i] = rand() % N;
    }

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    bubble_sort( a, N, ascending );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    bubble_sort( a, N, descending );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );


    return 0;
}

The program output might look like

9 0 1 6 0 8 7 4 9 4 
0 0 1 4 4 6 7 8 9 9 
9 9 8 7 6 4 4 1 0 0 



回答3:


There are 2 mistakes in your code:

  1. on line 13 you are passing numbers[] to the ascending function. This is wrong, you can never pass anything with [], when you call a function. When you write int numbers[] in the argument list of a function, it means that you want the function to accept a pointer to a number, you are just declaring this to the compiler. So it should just be ascending(numbers, size);

  2. on line 30 you made a typo, you wrote unmbers[i] = temp;, while it should be numbers[i] = temp;

here is the correct code:

#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];
                numbers[i] = temp;
                sflag = 1;
            }
        }
        count++;
    }while(sflag);

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


}


来源:https://stackoverflow.com/questions/60121704/i-cant-get-bubble-sorting-to-work-in-function-while-passing-array-to-the-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!