Template Functions - An Example of Sorting


One of the many things that we in business programming need to be able to do is to put collections of data in a particular order. If a database is available, then the database has features that can be used to sort the data. However if a database is not available, a C++ program can be written which can order data. For example suppose that a collection of integral values were available in a list like the following which could be put into an array:

image from book

 {2, 6, 4, 8, 10, 12, 89, 45, 37} 

image from book

Notice that the elements of this list are not in numeric order. To handle this sorting problem there are many different approaches. One sorting method is to use the algorithm called: the Bubble Sort. An example of a bubble sort uses the following function. The function: bubbleSort() moves through the elements of the array which contains the number one pass for each element to find those values which are larger than the element in question:

image from book

 void bubbleSort(int array[], const int size) {   for(int pass = 0; pass < size - 1; pass++)     for(int k = 0; k < size - 1; ++k)       if(array[k] >array[k+1])       {          int hold = array[k];          array[k] = array[k+1];          array[k+1] = hold;     } } 

image from book

and then if an element is larger than the next element, the function exchanges the larger with the smaller of the two values:

  • To see an example of this link to bubble.cpp then compile and run it.

Q: The function listed above solved the problem for an array of integers but what if you had an array of doubles, or an array of strings or an array of objects of a class like Date? How could the above function be modified to permit this to happen?

A: Redefine the function bubbleSort() so that they would work for each data type.

Modify the program: bubble.cpp and call the new program: toSort.cpp. In this program overload the function: bubbleSort() so that the program will sort lists of data when they are placed into arrays. Use the program to demonstrate that it works by including in the program each of the following arrays:

  • {"The Mystery Storm", "An example of using a drill press.", "Xanadeu: a place of mystery", "Somewhere over the rainbow", "How to lose 10 pounds in 10 days", "Where East meets West"},

  • {234.44, 4353.44, 41.34,-3453.33, 548.234},

  • {day1, day2, day3, day4} where this array contains objects of the class Date stored in the files: date.h and date.cpp. The object day1 represents 9/11/2001, the object day2 represents 12/7/1941, the object day3 represents 7/4/2005, and the object day4 represents 11/2/2004.

This process of rewriting the program toSort.cpp by redefining bubbleSort() for each data type is too much work. A better approach would be to use a template function. What modifications would be necessary in order to get the function to be a template function? How about the following:

image from book

 template<typename T> void bubbleSort(T array[],const int size) {   for(int pass = 0;pass < size - 1; pass++)      for(int k = 0; k< size - 1 ; ++k)        if(array[k] > array[k+1])        {           T hold = array[k];           array[k] = array[k+1];           array[k+1] = hold;        } } 

image from book

Take this template function and replace it for all of the functions: bubbleSort() in your program: sortIt.cpp to see how it works with all of the different data types.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net