1
votes

I have to write a swap function for my bubble sort
That's what I've got:

void swap(int arr[], int size, int i, int j)
{
    int temp = *(arr+i);
    *(arr + i) = *(arr+j);
    *(arr+j) = temp;
}

When I'm trying to run I get the following errors:

warning C4013: 'swap' undefined; assuming extern returning int error C2371: 'swap' : redefinition; different basic types

When I do change the function to the type of int, it does work, any idea why?

I don't need a prototype because it's before the main function... do I?

Here's the whole code:

//BubbleSort

    void bubbleSort(int arr[], int size)
    {
        int i,j;
        for(i=0; i < size; i++)
        {
            for(j=i+1; j < size; j++)
            {
                if(*(arr+i) > *(arr+j))
                {
                     /*temp = *(arr+i);
                    *(arr + i) = *(arr + j);
                    *(arr + j) = temp;*/
                    swap(arr,i,j);
                }
            }
        }
    }
    void swap(int arr[], int i, int j)
    {
        int temp = *(arr+i);
        *(arr + i) = *(arr+j);
        *(arr+j) = temp;
    }
    void main()
    {
        int i, arr[] = {8,0,6,-22,9};
        bubbleSort(arr, sizeof(arr)/sizeof(int));
        for(i=0; i < sizeof(arr)/sizeof(int); i++)
        {
            printf("%d, ",*(arr+i));
        }
        printf("\n");
    }
7
You are returning 0 while the method has a void type. Only return a value when the type is not void.trumank
+1 to counter meaningless, unexplained downvote.ArjunShankar
No, you don't need an isolated prototype if the function is defined before it is used. A definition also provides a prototype.pmg
Post the whole code, including the caller.Lundin
If you are using a bubble sort, the best thing you can do is quit your job and get one that does not involve programming. If this is homework, mark it as homework.DwB

7 Answers

2
votes

You seem to lack a proper prototype for the function.

Add

void swap(int arr[], int size, int i, int j);

before the first call.

Also, there's really little point in using such pointer-centric notation for the indexing, especially confusing since you declared the arr argument as an array. It's cleaner to just use:

const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

Notice use of const for the temp value too, since it's not going to change after being assigned. Not a big deal in a 3-line function, but a good habit.

2
votes

You need place void swap(int arr[], int i, int j) on top of void bubbleSort() because you're using swap() inside bubbleSort().

If not you will run into the implicit declaration of C, that is, in main(), you're calling bubbleSort(), and bubbleSort() will call swap(), but at this point, bubbleSort() does not know about you declaration of swap() since it is declared below it. So, what your compiler understand is that you're calling a swap() which is implicitly declared.

And later, when your compiler comes across your real declaration of void swap(int arr[], int i, int j), it will complain that it is a redefinition.

Other than moving your swap() declaration on to the top most, you can also solve by making the function declaration at the top most, and the definition below separately.


Besides, you don't seem to use pointer the right way, because you already passed int arr[] into the swap() function, where you can do direct swapping as pointed out by @unwind:

const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

Consider this swap():

void swap(int *x, int *y){
int temp = *x;
*x=*y;
*y=temp;
}

This is important for you to learn that you can actually change the content of a variable by passing the address into a function, like swap(int *x, int *y), it will be very convenient. Example:

int x,y;
x=1;
y=1;
increment_both_coordinate(x,y);
//after this call, you want to have x = 2, y = 2

This can only be achieve using the method similar to swap(int *x, int *y). This is just an illustration, you will understand how useful this can be when you see them in future.

1
votes

Inside bubbleSort() you call a function named swap() but, at that point in the code, there is no function named swap() either defined or declared.

Solution 1: move the defintion of swap() to before the definition of bubbleSort()
Solution 2: specify the prototype of swap() before defining bubbleSort()

0
votes

If the function is void, you can't return a number.

Change your

return 0;

to

return;
0
votes

Because you're returning 0. Take out the return statement and you should be fine, especially since you're operating on pointers instead of copy values.

0
votes

Using "void" means the function doesn't return any value,but actually your function return "0",which is an int type.So you should use int instead of void before function definition.

0
votes

Here is a function for swapping integers:

void swap(int *x, int *y) {
    *x = *x ^ *y;
    *y = *x ^ *y;
    *x = *x ^ *y;
}

int main(int argc, char* argv) {

    int a,b;

    a = 5;
    b = 10;
    swap(&a, &b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

You can swap two array cells this way: swap(&arr[i], &arr[j]);