Built into qsort is a function that can swap two given array elements.
elem_1. elem_2 Built into qsort is a function that can swap two given array elements.
elem_1. elem_2 Built into qsort is a function that can swap two given array elements.
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.
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?
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 *
Suppose array elements are of type int. How do you find the value of the elements?
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) {
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;
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
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
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; }
elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) {
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;
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; }
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;
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
Consider an array of pointers to int elem_1. elem_ int cmp(void *elem_1, void *elem_2) {
Consider an array of pointers to int elem_1. elem_ 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 elem_1. elem_ 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 elem_1. elem_ 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; elem_1. elem_ int cmp(void *elem_1, void *elem_2) {
typedef struct { int city_1,city_2,cost; } Cable; elem_1. elem_ 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; elem_1. elem_ 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; }