(Alphabetizing Animal Names) Write a program that uses the comparison capabilities in- troduced in this chapter to alphabetize a series of animal names. Only uppercase letters should be used for the comparisons.
What will be an ideal response?
```
// NOTE: The problem description should have asked
// the programmer to use a quicksort.
#include
#include
using namespace std;
// prototypes
void output( const string *, const int );
void quickSort( string [], int, int );
int partition( string [], int, int );
int main()
{
const int SIZE = 19;
// an array of strings containing animal names
string animals[] = { "Macaw", "Lion", "Tiger", "Bear", "Toucan",
"Zebra", "Puma", "Cat", "Yak", "Boar", "Fox", "Ferret",
"Crocodile", "Alligator", "Elk", "Ox", "Horse", "Eagle", "Hawk" };
cout << "before:";
output( animals, SIZE ); // call output to display string array
quickSort( animals, 0, SIZE ); // sort them in order
cout << "\nafter:";
output( animals, SIZE ); // call output to display array of animal
} // end main
// function to print out each string in the array
void output( const string * const ani, const int length )
{
// loop through the array with the given length
for ( int j = 0; j < length; ++j )
cout << ( j % 10 ? ' ': '\n' ) << ani[ j ];
cout << endl;
} // end function output
// function to sort the array
void quickSort( string a[], int first, int last )
{
// call function partition
int currentLocation;
if ( first >= last )
return;
currentLocation = partition( a, first, last );
// recursive calls to quickSort to continue the search
quickSort( a, first, currentLocation - 1 );
quickSort( a, currentLocation + 1, last );
} // end function quickSort
int partition( string b[], int left, int right )
{
int pos = left;
// while loop
while ( true )
{
// move through the array from left to right
while ( b[ pos ] <= b[ right ] && pos != right )
right--;
// if the right is reached, return that position
if ( pos == right )
return pos;
// if the element from the left is greater, swap the positions
if ( b[ pos ] > b[ right ] )
{
b[ pos ].swap( b[ right ] );
pos = right;
} // end if
// compare from the beginning to the pos index
while ( b[ left ] <= b[ pos ] && pos != left )
left++;
if ( pos == left )
return pos;
if ( b[ left ] > b[ pos ] )
{
b[ pos ].swap( b[ left ] );
pos = left;
} // end if
} // end while
} // end function partition
```
before:
Macaw Lion Tiger Bear Toucan Zebra Puma Cat Yak Boar
Fox Ferret Crocodile Alligator Elk Ox Horse Eagle Hawk
after:
Alligator Bear Boar Cat Crocodile Eagle Elk Ferret Fox Hawk
Horse Lion Macaw Ox Puma Tiger Toucan Yak Zebra
You might also like to view...
Queries that include more than one criterion are referred to as ________ queries
Fill in the blank(s) with correct word
Create an HTML document which uses the XMLHttpRequest object to submit the customer profile created in Exercise 28.4. Also, create an ASP page which accepts the submitted data and inputs it into the database.
What will be an ideal response?
____ is the default format for a new email message.
A. Plain Text B. STMP C. Rich Text D. HTML
Separator pads or rollers are designed to do which of the following?
A. Ensure proper spacing between the fuser assembly rollers and media B. Ensure that only one page at a time enters the printing process C. Provide spacing for different media types when loaded in the same printer D. Provide even line spacing throughout the page during the printing process