Bifalgorithm post · Computer science one · Function Pointers

How are generic getMax() functions used with integers and structures?

How are generic getMax() functions used with integers and structures? (Computer science one · Function Pointers) — "Suppose we have an array of integers and th…

Ideas in this flowchart

  • La fonction getMax() sélectionne l’élément « maximum » selon un comparateur.
  • getMax() retourne normalement l’indice de l’élément maximum.
  • Avec le tableau {8, 2, 9, 10, 4, 2, 2, 5, 6, 7}, getMax() reçoit la taille et la taille d’un entier.
  • Avec cmpInt(), la fonction retourne l’indice 3 et la valeur maximale affichée est 10.
  • Avec cmpIntDesc(), l’élément « maximum » devient le plus petit élément, soit 2.
  • La notion de « maximum » dépend de l’ordre défini par le comparateur.
  • Pour un tableau de Student, studentByNameCmp() classe les étudiants selon l’ordre lexicographique du nom.
  • Avec studentByNameCmp(), un étudiant nommé « Zadora » est considéré comme plus grand qu’un étudiant nommé « Anderson ».
  • Avec studentGpaCmp(), getMax() retourne l’étudiant ayant le GPA le plus faible, car le comparateur ordonne les GPA du plus élevé au plus faible.
  • Une variante retourne un pointeur vers l’élément maximum au lieu de retourner son indice.
  • La variante parcourt les éléments, met à jour maxIndex, puis retourne l’adresse de l’élément maximum.
  • Le tableau est converti en void * dans le retour final afin de rendre le type compatible et d’éviter les avertissements.
Feed/

How are generic getMax() functions used with integers and structures?

No cover

Channel

Computer science one

1 page

"Suppose we have an array of integers and the cmpInt() comparator function above. We can use our getMax() function as follows.\n\nint arr[] = {8, 2, 9, 10, 4, 2, 2, 5, 6, 7};\n\nint maxIndex = getMax(arr, 10, sizeof(int), cmpInt);\n\nprintf(\"maximum value: %d\n\n\", arr[maxIndex]);\n\nIn this example, the getMax() function would return the index 3 and print the maximum value stored there, 10. The getMax() function returns the index corresponding to the “maximum” element according to the comparator used. If instead we had used cmpIntDesc() the “maximum” element would have been the least element, (2 in the example above) because it would have been the element ordered “last” by the descending comparator.\n\nConsider another example with our Student structure.\n\nint n = 10;\n\nStudent *roster = (Student *) malloc(sizeof(Student) * n);\n\n...\n\nint maxIndex = getMax(roster, n, sizeof(Student), studentByNameCmp);\n\nSince studentByNameCmp() orders by lexicographic ordering, a student with the last name “Zadora” would have been “larger” than someone with a last name “Anderson.” Thus the code above will return an index corresponding to the last student in lexicographic ordering of their name. Similarly, if we had used the studentGpaCmp() comparator instead, getMax() would have returned an index for the student with the lowest GPA as this comparator ordered highest to lowest.\n\nAnother variation on this function would be to return a pointer to the maximum element rather than an index. The same logic would have applied, but the return type would be a void * pointer and the return statement would return the memory address of the maximum element instead. To avoid warnings, in the final return statement we cast arr as a void pointer (instead of a const void pointer) to make the return value compatible with the return type.\n\nvoid * getMax(const void *arr, int n, size_t size,\n\nint(*cmp)(const void *, const void *)) {\n\nint i, maxIndex = 0;\n\nfor(i=1; i<n; i++) {\n\nif(cmp(arr + maxIndex * size, arr + i * size) < 0) {\n\n//we've found something larger, update the max_index:\n\nmaxIndex = i;\n\n}\n\n}\n\nreturn (void *)arr + maxIndex * size;\n\n}"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.