#include // File template2.cpp #include #include using namespace std; template // Define a template function to void Swap (T & first, T & second) // swap two variables of type T. { T temp = first; first = second; second = temp; } // Note: there is no need to make SIZE a template parameter because its value // is used only in executable statements and not in any declarations. It would // logically be passed as a function parameter as in the previous example. // The only reason for doing it this way is to demonstrate how it would be // done if an array declaration depended on its value. template // Define a template function void Sort (T array []) // to sort an array of SIZE { // values of type T. int i, j; for (i = 0; i < SIZE; i++) // Sort the array values into { // ascending order using a for (j = i; j < SIZE; j++) // bubble sort. if (array [i] > array [j]) Swap (array[i], array[j]); } } template // Define a template function to void Display (T array[]) // display elements of an array of { // SIZE values of type T. for (int i = 0; i < SIZE; i++) cout << array[i] << " "; } const int NumChars = 10; const int NumInts = 15; const int NumFloats = 6; void main () { char cArray [] = { 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' }; int iArray [] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; float fArray [] = {6.5, 5.5, 4.5, 3.5, 2.5, 1.5 }; cout << "\n\nThe original char values:\n"; Display (cArray); Sort (cArray); cout << "\n\nThe sorted char values:\n"; Display (cArray); cout << "\n\nThe original int values:\n"; Display (iArray); Sort (iArray); cout << "\n\nThe sorted int values: \n"; Display (iArray); cout << "\n\nThe original float values:\n"; cout << setprecision(1) << setiosflags(ios::fixed |ios::showpoint); Display (fArray); Sort (fArray); cout << "\n\nThe sorted float values: \n"; Display (fArray); cout << endl; system("pause"); }