0

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); }; 
3
  • 3
    Is this really the code you are running? Because you neither initialized a nor arrSize, causing the function call to be full of undefined behavior Commented Aug 3, 2022 at 9:52
  • 1
    whats wrong with the code? What do you mean with "//Those 2 lines getting skipped." ? How did you arrive at that conclusion? Commented Aug 3, 2022 at 9:53
  • Instead of hand coding swap use std::swap en.cppreference.com/w/cpp/algorithm/swap available in the standard library. Commented Aug 3, 2022 at 9:57

1 Answer 1

5

You probably meant to use = operator instead of ==.

Sign up to request clarification or add additional context in comments.

1 Comment

Lol, sorry, that was a dumbest mistake

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.