I am assigned to do a problem that the book doesn't have an example for. I've know how to search when there are only integers, but this, I don't know how to do. I would really appreciated if someone can show me how this problem is done. It goes like this: Modify the "selectionSort" function presented in this chapter so it searches an array of strings instead of an array of ints. Test the function with a driver program. Use program 8-8 as a skeleton to complete. ---I have uploaded the .cpp file "skeleton" to the link http://www.filecrunch.com/file/~s5y8ik Thanks again for your time.
C++, sorting char strings and searching?
try asking for help in yahoo chat rooms.
Reply:Basically, I took the selection sort from Wikipedia, modified it to work with comparing char*s rather than ints, and changed variable names to work with your code skeleton.
Don't forget to test it.
int i, j, min;
for (i = 0; i %26lt; NUM_NAMES - 1; i++) {
min = i;
for (j = i+1; j %26lt; NUM_NAMES; j++) {
if (strcmp(names[j], names[min] %26lt; 0) {
min = j;
}
}
//swap the first element of this pass (i) with the minimum element of this pass (min)
char* temp = a[i];
a[i] = a[min];
a[min] = temp;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment