1576 241833 66 Built into qsort is a function that can swap two given array elements

Preview:

Citation preview

...15 76 24 18 33 66

Built into qsort is a function that can swap two given array elements.

...15 76 24 18 33 66

. elem_1 . elem_2

Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

What general mechanism does c++ provide for passing objects to functions?

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

What general mechanism does c++ provide for passing objects to functions? Answer: void *

...15 33 24 18 76 66

Suppose array elements are of type int. How do you find the value of the elements?

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) {

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2;

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2;

No Good! Cannot find value of unknown element type

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2;

That's it!! Cast the pointer to be a pointer to type int

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) {

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost;

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost;

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

int *A = new int[10]; for (int i=0 ; i < 10 ; i++) A[i] = rand() % 100; qsort(A, 10, sizeof(int), cmp);

...

int cmp (const void *a, const void *b) { if (*(int *)a < *(int *)b) return -1; if (*(int *)a > *(int *)b) return 1; return 0; }

Consider an array of pointers to int.

...

1515 76 24 18 33 66

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) {

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2;

No Good! elem_1 is a pointer to a pointer!!

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2;

That's it! elem_1 needs to be dereferenced twice!!

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) {

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost;

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

Recommended