I have to sort the 5 elements in a, the Days and the Vowels. How do I use the template <class T> with the way I put this??

#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;

// Function prototypes
 void DisplayA(int[], int);
 void showArray(int[], int);
 void DisplayB(string[], int);
 void showArrayDays(string[], int);

int main()
{
    int a[5] = { 10, 4, 9, 55, 11 };
    string Days[7] = { "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun" };
    char Vowels[5] = { 'U', 'O', 'I', 'E', 'A' };

    cout << "Original array\n";
    cout << "a:   ";
    showArray(a, 5);

    cout << "Days: ";
    showArrayDays(Days, 7);

    // Sort the array
    DisplayA(a, 5);

    // Display the values again
    cout << "After being sorted\n";
    cout << "a:   "; 
    showArray(a, 5);


    system("pause");
    return 0;

}

void DisplayA(int array[], int size)
{
    int i, minIndex, minValue;
    for (i = 0; i < (size - 1); i++)
        {
        minIndex = i;
        minValue = array[i];
        for (int index = i + 1; index < size; index++)
        {
            if (array[index] < minValue)
                {
                 minValue = array[index];
                 minIndex = index;
                 }
             }
        array[minIndex] = array[i];
        array[i] = minValue;
         }

}

void showArray(int array[], int size)
{
    for (int count = 0; count < size; count++)
    cout << array[count] << " ";
    cout << endl;
}

Maybe you are looking for something like this:

std::sort(a, a+5);

Edited 1 Year Ago by jencas

For a template <class T> function all you need to do is substitute the type 'T' for the POD type.
e.g.

template <class T>
void showArray(T const arr[], const size_t size)
{
    for (size_t i = 0; i < size; ++i)
        std::cout << arr[i] << ' ';

    std::cout << std::endl;
}
This article has been dead for over six months. Start a new discussion instead.