Solved

More qsort help

Posted on 2007-10-15
237 Views
I am still having problems with my qsort function.  I am not receiving any errors, but it is not sorting by the percentage in descending order.  Actually, it does not appear to be sorting at all.
Here is my function:
int SortPercent (const void* ptrpercent1, const void* ptrpercent2){
VEHICLE* ptrp1 = (VEHICLE*) ptrpercent1;
VEHICLE* ptrp2 = (VEHICLE*) ptrpercent2;
if (ptrp1->percent > ptrp2->percent)
return -1;
if (ptrp1->percent > ptrp2->percent)
return 1;
return 0;
}

Here is how I am trying to call it in main:
// I assume I need to call the qsort function before "DisplayVehicleMatch"?
qsort ((void*)vehRec, vehCount, sizeof (VEHICLE), SortPercent);
DisplayVehicleMatch (vehRec, request, vehCount);

Here is my DisplayVehicleMatch function:
void DisplayVehicleMatch (VEHICLE vehRec[], VEHICLE request, int limit) {

for (int i =0; i < limit; i++){
//set up percentage calculations
vehRec[i].percent = vehRec[i].count/12.*100;
//print results of query
if (vehRec[i].count > 0)
{

cout << "Make: " <<  vehRec[i].make;
cout << endl << "Model: " << vehRec[i].model;
cout << endl << "Count: " << vehRec[i].count;
cout << endl  << fixed << setprecision(2)  << "Percent: " << vehRec[i].percent << "%";
cout << endl << "_________________________________\n\n";
}
}
}

Any advice would be greatly appreciated.
0
Question by:gndcdosps

LVL 22

Expert Comment

The problem is here:
int SortPercent (const void* ptrpercent1, const void* ptrpercent2){
VEHICLE* ptrp1 = (VEHICLE*) ptrpercent1;
VEHICLE* ptrp2 = (VEHICLE*) ptrpercent2;
if (ptrp1->percent > ptrp2->percent)
return -1;
if (ptrp1->percent > ptrp2->percent)
return 1;
return 0;

Both are looking for ptrp1->percent > ptrp2->percent. Change the first to:
ptrp1->percent < ptrp2->percent

Jim
0

Author Comment

Thanks, but no luck yet... Still no errors.  I do not think I am calling it right.  How do I point this function and tell it that it needs to point to the precent field to sort?  I have not worked with pointers before, so this is all new to me.  Let me know if you need to see any more of my code.
0

LVL 22

Expert Comment

Can you post qsort?

Jim
0

Author Comment

Well, I am doing the comparison in the function SortPercent:
int SortPercent (const void* ptrpercent1, const void* ptrpercent2){
VEHICLE* ptrp1 = (VEHICLE*) ptrpercent1;
VEHICLE* ptrp2 = (VEHICLE*) ptrpercent2;
if (ptrp1->percent < ptrp2->percent)
return -1;
if (ptrp1->percent > ptrp2->percent)
return 1;
return 0;

}
Then  in my main I am calling the qsort function, as its prototype specified in my text:
qsort ((void*)vehRec, vehCount, sizeof (VEHICLE), SortPercent);
// Next I call the display
DisplayVehicleMatch (vehRec, request, vehCount);

I am sure my my problem has something to do with not telling my program which vehRec structure I am wanting to point to.  I just do not understand how?
0

LVL 22

Accepted Solution

Here's another possibility. Inside DisplayVehicleMatch, you set:
vehRec[i].percent = vehRec[i].count/12.*100;

What does vehRec[i].percent  contain before this? Was it not already set befor the qsort invocation?

Jim
0

Author Comment

THANKS A BUCH!  THAT DID THE TRICK!
0

LVL 22

Expert Comment

My pleasure.  Good luck,

Jim
0

Featured Post

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.