Homework 2

Due Date: Midnight, Friday October 6
(NOTE: NO LATE HOMEWORKS WILL BE ACCEPTED.)

Assignment:

Write a program that performs a selection sort on a one-dimensional array of integers. The number of array elements should be received as a command line argument. Fill the array with consecutive integers, starting with the number of array elements. For example, an array with 10 elements would be filled as follows:

array[0] = 10 array[5] = 5 array[1] = 9 array[6] = 4 array[2] = 8 array[7] = 3 array[3] = 7 array[8] = 2 array[4] = 6 array[9] = 1

Using the functions "time" and "clock", calculate the execution time and the CPU time of the selection sort (only). (Descriptions of these functions are given below.) Run the program 5 times using array sizes of 1,000, 5,000, 10,000, 15,000, and 20,000 . Capture the output of the 5 runs in a single text file (e.g., use a script file).

Homework Submission

Submit your program source code, makefile, and text (output) file electronically using "submit". Be sure that your program compiles to an executable file named "hw2".

Description of Function time

The prototype for this function is:

     time_t time(time_t *timer);

You must include the header file stdio.h. This function stores the current time in the variable pointed to by timer. The current time is in the form of elapsed seconds since 00:00:00 Greenwich mean time, January 1, 1970.

Example:

#include <iostream.h> #include <time.h> void main() { time_t timer; for (long i=0; i<200000; i++); cout << "Elapsed execution time: " << time(&timer) << endl; } Description of Function clock

The prototype for this function is:

     clock_t clock(void);

You must include the header file time.h. This function returns an approximation of the amount of processor time used since the start of program execution. The time is returned as the number of clock ticks. The elapsed time in seconds may be calculated by dividing the return value by the macro constant named CLOCKS_PER_SEC (number of clock ticks per second), defined in time.h.

Example:

#include <iostream.h> #include <time.h> void main() { clock_t ticks; for(long i=0; i<200000; i++); ticks = clock(); cout << "Elapsed CPU time: " << (long)ticks/CLOCKS_PER_SEC) << endl; }

Reference Used

Reference used for function descriptions: "Power C: ANSI Standard High-Performance C Compiler," Mix Software, Inc.

Last Modified: Thursday, 28-Sep-2000 16:14:44 EDT