I have the code below and I keep getting an error saying that swap isnt a function. Just wondering if anyone had any idea why. Thanks

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

void sort(double h[], int size){
    int i, j;
    for (i=(size-1); i>=0; i--){
        for(j=1; j<=i; j++){
            if (h[j-1] > h[j]){
                swap(*(h+(j-1)),*(h+j);
            }
        }
    }

}

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

that is one hideous looking bubble sort.

Search the forums for a simpler solution which uses temporary variables.

1. Where is the closed brace terminated swap function body?
2. Use code tag properly:
[code=c] ... snippet ...

[/code]
3. Keep it simpler (avoid index minus offset where possible):

for (j=0; j<i; j++) {
    if (h[j] > h[j+1]) {

4. Think about swap call arguments: pass pointers, not values...

Your swap function has a missing } at the end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.