#include #include #include #include using namespace std; int main() { string tostring(int i); int intCompare(const void* a, const void* b); int stringCompare(const void* a, const void* b); int n, min, max, num; string sortType; srand(time(0)); cout << "How many numbers do you want to sort? "; cin >> n; cout << "\tmin: "; cin >> min; cout << "\tmax: "; cin >> max; cout << "How do you want to sort the numbers, as \"string\" or as \"numbers\": "; cin >> sortType; // based on the command line argument, we will sort either string or // integer data. if string data, then use qsort() to sort. if integer // data, then use either qsort() or sort() depending on the user preference if (sortType == "strings" || sortType == "Strings") { string *seq = new string[n]; for (int i = 0; i < n; i++) { num = rand() % (max - min + 1) + min; seq[i] = tostring(num); } // sort in string order qsort(seq, n, sizeof(string *), stringCompare); {for (int i = 0; i < n; i++) { cout << seq[i] << endl; }} } else { int *seq = new int[n]; for (int i = 0; i < n; i++) { num = rand() % (max - min + 1) + min; seq[i] = num; } // sort in numeric order qsort(seq, n, sizeof(int), intCompare); {for (int i = 0; i < n; i++) { cout << seq[i] << endl; }} } return 0; } int intCompare(const void* a, const void* b) { const int *x = (int *) a; const int *y = (int *) b; return (*x - *y); } int stringCompare(const void* a, const void* b) { const string *x = (string *) a; const string *y = (string *) b; if (*x < *y) return -1; if (*x > *y) return 1; return 0; } string tostring(int i) { string s; char num[20]; sprintf(num, "%d", i); s = num; return s; }