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;
}

Dani AI

Generated

A short, practical note: for real code the standard algorithm is the simplest and fastest (as pointed out). If the goal is to practise templates, a clean approach is to write generic functions that deduce the array length at compile time. 's template is correct; the version below removes the separate size parameter by taking the array by reference and works for built-in arrays of any element type.

// requires <iostream> and <utility> (or <algorithm> for std::swap)

template <class T, std::size_t N>
void printArray(const T (&arr)[N]) {
    for (std::size_t i = 0; i < N; ++i)
        std::cout << arr[i] << ' ';
    std::cout << '\n';
}

template <class T, std::size_t N>
void selectionSort(T (&arr)[N]) {
    for (std::size_t i = 0; i + 1 < N; ++i) {
        std::size_t minIdx = i;
        for (std::size_t j = i + 1; j < N; ++j)
            if (arr[j] < arr[minIdx]) minIdx = j;
        if (minIdx != i) std::swap(arr[i], arr[minIdx]);
    }
}

With the arrays declared in the original post, calls like selectionSort(a); selectionSort(Days); selectionSort(Vowels); followed by printArray(...) will compile and work because the compiler deduces N. If data is dynamic (pointers or heap arrays) prefer std::vector or pass an explicit size; templates that take T* cannot deduce length. For production use prefer std::sort with std::begin/std::end for better performance and maintainability.

Notes and caveats: printing a char[] element-wise prints single characters (the template above does that). A C-string literal includes a terminating null byte, so its deduced N may be one larger than the visible characters. Also avoid system("pause") for portable code. This keeps the exercise focused on templates while pointing to the standard library when appropriate.

Recommended Answers

All 3 Replies

Member Avatar for Member #248612

Maybe you are looking for something like this:

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

Ya but I need to use template <class T> function

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.