How do I fix my CountSort code to sort in descending order?
Hello!
My task: I have to implement a count sort in such a way that when calling the program you can select in the argument whether the array should be sorted in ascending or descending order.
My problem : Sorting ascending works, but sorting descending also sorts ascending, even though it should sort descending.
My assumptions: I actually assume that my "count_sort_write_output_array" function, which creates the sorted array based on the specified sort direction, is correct. After all, it's simply the same code as for ASCENDING (ascending), with minor changes.
I think it's more likely that my code doesn't 'select' DESCENDING when "desc" is given as an argument. I've set the default behavior to ASCENDING (so if no argument is given, or an incorrect argument is given, it sorts in ascending order), so I thought that maybe either
if (strcmp(order, "asc") == 0)
or
else if (strcmp(order, "desc") == 0)
(in the SortDirection function) are somehow " wrong ," so the sort function always selects the default behavior (i.e., ASCENDING).
As a debugging attempt I have:
printf("Sortdirection must be defined. Using default: ASCENDING\n");
to see if that's really the case. However, this printf is never displayed, even if I don't enter any arguments. Which is quite confusing. Does it terminate before that?
My 'question' to you: I would really appreciate it if someone could take a look. Maybe someone can see what my semantic error is.
But I would also appreciate it if someone could suggest other debugging ideas. I haven't really explored debugging methods yet and have mostly solved my problems with printfs. But maybe there's something easier!
Thank you!
Code as images are naturally pfui.
What is missing: Where is SortDriection defined as type and what are ASCENDING and DESCENDING.
And depending on this, the procedure looks okay, you could use in extract_sort_dirdction() mal printf():
Instead of the points, of course, the right qualifier for the type should be pure.
However, the following would be obvious:
If you want to complete everything with expenses (and make one flusch each after), then you should see where it goes wrong or which progam part is executed.
At first glance it looks correct.