CS101 - LAB ACTIVITY 11

This week's lab deals with sorting, which is an interesting problem on its own but more importantly, it is an essential part of MP2. In the first part, you will use the standard library function qsort to sort an array of integers. In the second part, we will sort strings instead of integers.

Objectives


References


Part 1: Using QuickSort

1. Requirements Specification (Problem Definition)
In this part, we're going to generate an array of 10 integers at random. First we display the list of 10 integers. Then we will ask the user if they want to have the integers sorted in ascending or descending order. We will use the function named qsort found in the standard library (#include <stdlib.h>) to sort integers.
To see a demo of the program you will download the following program: sort
To run this program:

2. Analysis---Refine, Generalize, Decompose the problem definition
Our program will have four functions:  main , menu , compAsc , compDesc .


In main we will need to call the qsort function. qsort takes four parameters, in this particular order :

qsort(name of the array to be sorted,   number of elements in the array,   size in number of bytes of each element of the array,   comparison function name) 

3. Design---Develop Algorithm
You will need to code four functions: main, menu, compAsc and compDesc.

menu function
The menu function has no input arguments and it returns an integer. So its code looks like:
int  menu(void)
{


}
Here is the algorithm for the menu function in pseudo-code:
              Enter your choice:
compAsc function
We will give you the code for the compAsc function:
   int   compAsc( int * ptr1 , int * ptr2)
   {
        return  *ptr1 - *ptr2 ;
   }

compDesc function
The code for the compDesc function is almost identical to the code for the compAsc function except that it returns an integer value of the opposite sign. Copy and paste the code for the compAsc function and change its name to compDesc and make the necessary changes.




main function

Here is the algorithm for main in pseudo-code:
4. Implementation --- Write the "Program" (Code)

We will use eclipse to do this lab.