Lab 7: Using
Dynamic Memory
Due: Wednesday, Oct. 15, 2014 @ 11:59 p.m.
Description:
In this lab you will once again modify your InsertionSort code from Lab 6 to accept an unknown number of integers from standard input (stdin) and sort them. You will not know the number of integers for each run, so you will use malloc() to dynamically allocate space in your array to hold them. You will start by dynamically allocating space for 100 integers. If there are more than 100 values read from stdin, you will "resize" the array by dynamically allocating 200 integers and switching the array pointer to the address of the newly allocated memory. Note that the values you have read so far will need to be copied from the old array to the new array. Also note that the memory from the old array must be freed.
You will continue reading data from stdin, resizing the array as
needed, until EOF is encountered. Note that this is the key difference
between this lab and Lab 6.
Background Preparation:
Specifications:
The specifications for this lab are similar to the specifications for Lab 6. However, since the InsertionSort program for this lab uses dynamic memory, you must not use static memory for any of the arrays in your program. Also, whenever resizing an array is necessary, create a new array that can hold 100 more integers than the array's current size.
Make sure you free any memory that is no longer in use.
You may find it useful to have a variable that keeps track of
the current array size, as well as a variable that keeps track of the
number of values currently stored in the array.
Note that you may NOT use realloc() for this lab.
Modify your GenNums program from Lab 6 to choose a number between 100 and 2000, inclusive (instead of 10-200) for the number of random values generated. Also change the range of random values to be from 0 to 5000, inclusive. These higher values will add stress to the testing of the malloc()/resize array procedure, giving evidence that your code is perhaps working correctly. Test your InsertionSort code with this new version of GenNums. The procedure for testing will be the same as for Lab 6.
Use a Makefile to compile your two programs.
Log in to mason or zeus and copy your lab's files into a directory named Lab7. Start a typescript session in the directory above Lab7. Type "uname -a" to show that you are on mason or zeus. Change to the Lab7 directory and use the ls and cat commands to list all your source files and Makefile. Compile your programs using the make command. Run your programs to show that they execute correctly.
To
get full credit for this lab you must generate five different sets of
random numbers using your GenNums program and sort them with
InsertionSort. You must also show that your program works with both of the
methods (redirection and pipes) as given in Lab 6.
Change back to the directory immediately above Lab7, and use the tar command to create an archive of all the files in the Lab7 directory. Name this archive Lab7.tar. Re-read the man page if necessary to discover the proper options to use.
Finally, exit the typescript session by typing crtl-d.
Verify that your script file was created correctly by using the more (or less) command (your choice) to see the contents of the file. As always, information on these commands can be obtained by using the manual pages ("man more" or "man less"). Once you are sure the file is correct, copy the script file and Lab7.tar back to your local computer and submit both of them to Blackboard as Lab 7.