Back to code:
1 2 3 4 5 6 7 8
|
bool compgrowth (data a, data b) {
if (a.growth!=b.growth)
return a.growth>b.growth;
}
bool compnow (data a, data b) {
if (a.now!=b.now)
return a.now>b.now;
}
| |
These two need work.
Never fall into the trap of thinking, "well, it works, so...."
First, what does the function do if a.growth == b.growth? You may never present that condition in your testing, and perhaps it never happens in practice, but is this a reason to make this test?
No.
There's no problem skipping the "!=" test in these functions.
The most common test is lessthan (<) for sorting. If you want a reverse order, then make it greaterthan. That much is fine.
So, the function should return true if that condition is met, and
false under all other circumstances.
So, both of these are configured to report > (greaterthan) as true. Just test for that, and ditch the "!=" - it doesn't really do anything at all.
If that test fails, return false. Now, you'd have a proper comparison.
Consider what happens if this code compiles and executes. What if the two parameters are neither > or ==?
The next step is undefined. The function could return anything...random results. It should return false, but without an actual explicated return of false, the calling code might just as easily receive a non-zero return (interpreted as true).
How would the algorithm function if these functions returned true when the two tests written both were false?
To say the least, it wouldn't be reliable.