/* teststring3.cpp CIS 150 David Klick 7/3/08 This program demonstrates sorting an array and converting strings to numbers. */ #include #include #include using std::cin; using std::cout; const int ARRAY_SIZE = 5; void sort(char nm[][10], int len); void numsort(char nm[][10], int len); void cont(); int main() { // It may not be obvious, but C-style strings can // NOT be used as numbers. They are merely a // collection of digits. char nums[ARRAY_SIZE][10]; // storage for five number strings int i; cout << "Entering " << ARRAY_SIZE << " integers\n"; // get numbers from user for (i=0; i 0) { strcpy(temp, nm[i]); strcpy(nm[i], nm[j]); strcpy(nm[j], temp); } } } } /* void sort(char nm[][20], int len); Arguments: char nm[][20]: the array of strings to be sorted int len: the length of the array Returns: nothing This function modifies the input array (nm[][]) by sorting it into case-insensitive numerical ascending order. Note: The data is strings, but sorted numerically. */ void numsort(char nm[][10], int len) { char temp[10]; // holding area for number when swapping int i, j; for (i=0; i atol(nm[j])) { strcpy(temp, nm[i]); strcpy(nm[i], nm[j]); strcpy(nm[j], temp); } } } } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Entering 5 integers Enter integer #1: 57 Enter integer #2: 1002 Enter integer #3: 9 Enter integer #4: 422 Enter integer #5: 18 The integers you entered were: 57 1002 9 422 18 The integers in alphabetic sorted order are: 1002 18 422 57 9 The integers in numeric sorted order are: 9 18 57 422 1002 */