Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# More qsort help

Posted on 2007-10-15
Medium Priority
265 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
• 4
• 3

LVL 22

Expert Comment

ID: 20082359
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

ID: 20082425
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

ID: 20082432
Can you post qsort?

Jim
0

Author Comment

ID: 20082512
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

JimBrandley earned 2000 total points
ID: 20082749
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

ID: 20082788
THANKS A BUCH!  THAT DID THE TRICK!
0

LVL 22

Expert Comment

ID: 20082795
My pleasure.  Good luck,

Jim
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
###### Suggested Courses
Course of the Month14 days, left to enroll