So i want to use AscendingSort() and DecendingSort() as an argument but it seems like after return the value the swap part just get skipped, hope someone explain to me, thanks!.
bool AscendingSort(int a, int b) { return a > b; } bool DecendingSort(int a, int b) { return a < b; } void SortArray(int* a, int size, bool(*func)(int, int)) { int saveElement; for (int x = 0; x < size; x++) { for (int y = x + 1; y < size; y++) { if (func(a[x], a[y])) { saveElement = a[x]; a[x] == a[y]; //Those 2 lines getting skipped. a[y] == saveElement; } } } } void main() { int a[1000]; int arrSize; SortArray(a, arrSize, AscendingSort); };
anorarrSize, causing the function call to be full of undefined behaviorstd::swapen.cppreference.com/w/cpp/algorithm/swap available in the standard library.