Lab 3: Nested Loops and Sorting
Due: Wednesday, Sept. 17, 2014 @ 11:59 p.m.
Description:
In this lab, you will write a simple C program that will use nested loops. Your
program will use a random number generator to initialize an array. It
will then call a function to sort the array. After returning from the
function, the program will print out the sorted array.
Your program will actually generate floating point values in the range of [0..1000]. To do this, you will need to divide the value returned from the rand() function by (RAND_MAX/1000.0):
double my_random_val = rand()/(RAND_MAX/1000.0);
Insertion Sort:
The program will sort the array using the Insertion Sort algorithm. It works as follows:
InsertionSort(A) /* sort A[1..n] in place */
for j <-- 2 to n do
key <-- A[j] /* insert A[j] into sorted sublist A[1..j-1]
i <-- j-1
while ((i > 0) and (A[i] > key)) do
A[i+1] <-- A[i]
i <-- i-1
A[i+1] <-- key
Note that the above algorithm uses the value 1 to index the first element in the array. Remember that C does this differently.
Specifications:
Your program will start by printing an informational message, stating your name, lab section, and what the program will do.
It will declare in main() a local array numArray, that holds 25 values of type double.
It will seed the random number generator as described above.
The program will then perform the following five times using a loop:
Your program will not use any global variables.
Any constants used in the program (5, 25, your G# and the range of the random values (1000.0)) will be given as #define macros (this doesn't include the value used in the for loop of the sorting algorithm or the 4 used for the number of decimal places printed)
You
will compile your program with a Makefile using the gcc compiler.
You may edit a Makefile taken from Lab 1 or Lab 2, but your Makefile must
not contain any references to any programs and files other than those
necessary for this lab.
Log in to mason or zeus and copy your program there. Start a typescript session. Type "uname -a" to show that you are on mason or zeus. Use the cat command to list both your source file and Makefile. Compile your program using the make command. Run your program to show that it executes correctly. Then exit the typescript session by typing cntl-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. 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, source file and Makefile back to your local computer and submit all three of them to Blackboard as Lab 3.