Dynamic Array
In this lab, we will write a program that dynamically allocate memory using either 'malloc' function in C or 'new' operator in C++.
Here's two reference pages:
· C, 'malloc' function: http://www.cplusplus.com/reference/cstdlib/malloc/
· C++, 'new' operator: http://www.cplusplus.com/doc/tutorial/dynamic/
(If you are using C++, remember to name your file ".cpp".)
The execution of the finished program should look what is shown below (user input is shown in bold).
Author: Hong Wang
Lab: Thursday 2pm
Program: Dynamic Array
Please enter the size of interger array: 5
Now allocating space for an array of 5 integers...
Please enter 5 integers: 9 2 100 0 5
In reverse order those are: 5 0 100 2 9
In sorted order those are: 0 2 5 9 100
· Stage 1 (1 point):
· Ask user to input the size of integer array.
· Dynamically allocate an array of given size.
· Ask user to input each integer.
Example:
Please enter the size of interger array: 5
Now allocating space for an array of 5 integers...
Please enter 5 integers: 9 2 100 0 5
· Stage 2 (1 point):
· Store each user input integer into the array.
· Display the elements within the array in reverse order.
· (If you stop at this stage, free the allocated memory.)
Example:
In reverse order those are: 5 0 100 2 9
Notes:
· REMEMBER TO FREE the allocated memory when it is no longer needed (using either 'free' function in C, or 'delete' operator in C++).
· Allocating memory without freeing is called ' memory leak ', it is a common bug in computer programs!
· Sorting an array can be done using function 'qsort'.