/* Program to illustrate sorting an array of strings in a function */ #include #include using namespace std; const int SIZE = 5; //function prototype void sortthearray(string str[], int n); int main() { string str[SIZE]; string tempstr; str[0] = "Jones"; str[1] = "Harrow"; str[2] = "Tenenbaum"; str[3] = "Arnow"; str[4] = "Raphan"; //print the original array cout << "Original Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl; // sort the array sortthearray(str, SIZE); //print the sorted array cout << endl << "Sorted Array" << endl; for (int i = 0; i < SIZE; i++) cout << str[i] << endl; // system("pause"); //un-comment when using DevC++ return 0; } /* Function sortthearray() * Input: * str - the array to be sorted * n - number of elements to sort * Process: * linear sort * Output: * str sorted into alphabetical order */ void sortthearray(string str[], int n) { string tempstr; // sort the array for (int pos = 0; pos < n-1; pos++) for (int cand = pos+1; cand < n; cand++) if (str[cand] < str[pos]) { tempstr = str[cand]; str[cand] = str[pos]; str[pos] = tempstr; } return; }