Download presentation
Presentation is loading. Please wait.
1
... 1576 241833 66 Built into qsort is a function that can swap two given array elements.
2
... 1576 241833 66. elem_1. elem_2 Built into qsort is a function that can swap two given array elements.
3
... 1533 241876 66. elem_1. elem_2 Built into qsort is a function that can swap two given array elements.
4
... 1533 241876 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.
5
... 1533 241876 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?
6
... 1533 241876 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 *
7
... 1533 241876 66 Suppose array elements are of type int. How do you find the value of the elements?
8
... 1533 241876 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) {
9
... 1533 241876 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;
10
... 1533 241876 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
11
... 1533 241876 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
12
... 1533 241876 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; }
13
... 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) {
14
... 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
... 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; }
16
... 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;
17
... 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; }
18
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; }
19
Consider an array of pointers to int.... 15 7624 18 33 66
20
Consider an array of pointers to int.... 15. elem_1. elem_2 15 7624 18 33 66 int cmp(void *elem_1, void *elem_2) {
21
Consider an array of pointers to int.... 15. elem_1. elem_2 15 7624 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!!
22
Consider an array of pointers to int.... 15. elem_1. elem_2 15 7624 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!!
23
Consider an array of pointers to int.... 15. elem_1. elem_2 15 7624 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; }
24
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) {
25
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;
26
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; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.