Re: qsort question, urgent!
First, I suggest using a little ingenuity. To sort the numbers, combine them into a single number and compare.
Second, since I code in C++, I never use qsort. Since this is C++ (and I'm assuming you are programming in C++), you should use what C++ provides -- and that is the std::sort function.
Here is an example of how you would sort the numbers. Please study this -- you may never get a chance to learn this in any class you take in school (unfortunately):
#include <algorithm>
#include <iostream>
using namespace std;
//...
struct Times
{
int hours;
int minutes;
int seconds;
};
//...
unsigned long GetSingleValue(const Times& TValue) {
return (long)TValue.hours * 10000L +
(long)TValue.minutes * 100L +
(long)TValue.seconds;
}
//...
bool lessTime(const Times& T1, const Times& T2)
{
return GetSingleValue(T1) < GetSingleValue(T2);
}
//...
int main()
{
Times TotalTimes[10];
//...
std::sort(TotalTimes, TotalTimes + 10,
lessTime);
}
I defined a function that combines the components (hours, minutes, seconds) into a single number. This number will then be used to compare whether one time is less than another. The other thing I did was to define a function that returned true or false, depending on whether one time is less than another time. In main(), I called the standard C++ std::sort() function. This function takes a beginning and end range arguments, and a pointer to a function used to compare two elements.
This is the way that a C++ should use sort. qsort should only be used for 'C' programs in this day and age. Even if you do decide to use qsort, you should still combine the components into one, and sort the single component.
Regards,
Paul McKenzie
Re: qsort question, urgent!
Hi, you should use not strcmp in qsort, but your own routine that will compare your structure.
Sincerely, Andrew Koudryavtsev