This example will illustrate the key steps in creating and using a dynamic array. It implements the ideas above for reading and sorting an unknown number of integer values.
Here is an initial version. (It works, but has a potential memory leak. Here it doesn't matter since the memory is released when the program terminates. However, in a larger program that continues after these steps, the memory leak would be a problem. In general, dynamic memory allocated from the heap by a program should be explicitly released when no longer used.)
int main() { int sz; printDirections(); cout << "How many integers to generate and sort? "; cin >> sz; int *nums = new int[sz]; init(nums, sz); sort(nums, sz); print(nums, sz); return 0; }