Gå til innhold

Feil i C-kode snutt?, trenger hjelp


Anbefalte innlegg

Hei.

 

Jeg får opp følgende 2 feil når jeg kompilerer min c fil i visual studio.net.

error: identifier "swap" is undefined swap(&a[lp], &a[rp]);

error: identifier "swap" is undefined swap(&a[lp], &a

);

 

Her er min kode:

/* Standard quicksort. */

void quicksort(int a[], int left, int right)

{

int lp = left - 1;

int rp = right;

int v = a

; /* the partitioning element */

 

if (right <= left)

/* array has one or null elements to sort */

return;

 

/* No elements to the left of lp are greater than the partitioning element. */

while (1) {

while (a[++lp] < v)

;

 

while (v < a[--rp])

/* In case the partioning element is the smallest in the array */

if (rp == left)

break;

 

if (lp >= rp)

break;

 

/* Deadlock, switch elements and continue */

swap(&a[lp], &a[rp]);

}

/* This completes the partioning */

swap(&a[lp], &a

);

 

/* Now every element to the left of a[lp] are smaller than a[lp],

* and every element to the right is larger. */

 

quicksort(a, left, lp - 1);

quicksort(a, lp + 1, right);

}

 

static void swap(int *x, int *y)

{

int temp = *x;

*x = *y;

*y = temp;

}

 

Hvorfor får jeg denne feilen, og evt. hva må rettes på? Filen skulle i utgangspunktet være korrekt...

 

Mvh

Terje

Lenke til kommentar
Videoannonse
Annonse

swap er definert etter du prøver å kalle den. legg definisjonen før quicksort eller deklarer den før.

f.eks

static void swap(int *x, int *y);

 

et lite tips, er ikke vits å bruke temp variabel i swap.

void swap(int *x, int* y)

{

*x ^= *y;

*y ^= *x;

*x ^= *y;

}

Lenke til kommentar

Her er din kode:

 

/* Standard quicksort. */
void quicksort(int a[], int left, int right)
{
int lp = left - 1;
int rp = right;
int v = a[right]; /* the partitioning element */

if (right <= left)
/* array has one or null elements to sort */
return;

/* No elements to the left of lp are greater than the partitioning element. */
while (1) {
while (a[++lp] < v)
;

while (v < a[--rp])
/* In case the partioning element is the smallest in the array */
if (rp == left)
break;

if (lp >= rp)
break;

/* Deadlock, switch elements and continue */
swap(&a[lp], &a[rp]);
}
/* This completes the partioning */
swap(&a[lp], &a[right]);

/* Now every element to the left of a[lp] are smaller than a[lp],
* and every element to the right is larger. */

quicksort(a, left, lp - 1);
quicksort(a, lp + 1, right);
}

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

 


Her er fungerende kode: (funksjonen må defineres før du bruker den.)


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

/* Standard quicksort. */
void quicksort(int a[], int left, int right)
{
int lp = left - 1;
int rp = right;
int v = a[right]; /* the partitioning element */

if (right <= left)
/* array has one or null elements to sort */
return;

/* No elements to the left of lp are greater than the partitioning element. */
while (1) {
while (a[++lp] < v)
;

while (v < a[--rp])
/* In case the partioning element is the smallest in the array */
if (rp == left)
break;

if (lp >= rp)
break;

/* Deadlock, switch elements and continue */
swap(&a[lp], &a[rp]);
}
/* This completes the partioning */
swap(&a[lp], &a[right]);

/* Now every element to the left of a[lp] are smaller than a[lp],
* and every element to the right is larger. */

quicksort(a, left, lp - 1);
quicksort(a, lp + 1, right);
}

 

:)

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...