In: Computer Science
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number in the array with the second parameter +1 (so in our above example, it would be 51. ) Continue to do this until every number in the original array is printed out in order
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int size, n, mindInd, temp; cout << "Enter length of the array: "; cin >> size; cout << "Enter max value: "; cin >> n; int *arr = new int[size]; for (int i = 0; i < size; ++i) { arr[i] = rand() % n; } cout << "Original array is:"; for (int i = 0; i < size; ++i) { cout << " " << arr[i]; } cout << endl; for (int i = 0; i < size; ++i) { mindInd = i; for (int j = i + 1; j < size; ++j) { if (arr[j] < arr[mindInd]) { mindInd = j; } } temp = arr[mindInd]; arr[mindInd] = arr[i]; arr[i] = temp; } cout << "Sorted array is:"; for (int i = 0; i < size; ++i) { cout << " " << arr[i]; } cout << endl; return 0; }