Question

//CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

//CODE 16-02.cpp

//Demonstrates a template function that implements 
//a generic version of the selection sort algorithm.
#include <iostream>
using std::cout;
using std::endl;

template<class T>
void sort(T a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//The assignment and < operator work for values of type T.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <=... <= a[numberUsed - 1].

template<class T>
void swapValues(T& variable1, T& variable2);
//Interchanges the values of variable1 and variable2.
//The assignment operator must work correctly for thwe type T.

template<class T>
int indexOfSmallest(const T a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Array elements have values.
//The assignment and < operator work for values of type T.
//Returns the index i such that a[i] is the smallest of the values
//a[startIndex], a[startIndex + 1],..., a[numberUsed - 1].

#include "sort.cpp"

int main( )
{
    int i;
    int a[10] = {9, 8, 7, 6, 5, 1, 2, 3, 0, 4};
    cout << "Unsorted integers:\n";
        for (i = 0; i < 10; i++)
            cout << a[i] << " ";
    cout << endl;
    sort(a, 10);
    cout << "In sorted order the integers are:\n";
    for (i = 0; i < 10; i++)
        cout << a[i] << " ";
    cout << endl;
    double b[5] = {5.5, 4.4, 1.1, 3.3, 2.2};
    cout << "Unsorted doubles:\n";
    for (i = 0; i < 5; i++)
        cout << b[i] << " ";
    cout << endl;
    sort(b, 5);
    cout << "In sorted order the doubles are:\n";
    for (i = 0; i < 5; i++)
        cout << b[i] << " ";
    cout << endl;

    char c[7] = {'G', 'E', 'N', 'E', 'R', 'I', 'C'};
    cout << "Unsorted characters:\n";
    for (i = 0; i < 7; i++)
        cout << c[i] << " ";
    cout << endl;
    sort(c, 7);
    cout << "In sorted order the characters are:\n";
    for (i = 0; i < 7; i++)
        cout << c[i] << " ";
    cout << endl;

    return 0;
}

Modify code 16-2.cpp (which uses sort.cpp) to use a function template that displays the contents of an array.

This function should take two parameters: the array and the number of elements in the array.

(The function template is used to be able to handle an array of any data type).

Provide all source code files.

Also provide a screenshot with a sample run.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code:

//Demonstrates a template function that implements
//a generic version of the selection sort algorithm.
//Include headers
#include <iostream>
using std::cout;
using std::endl;
//template function prototype for sort
template<class T>
void sort(T a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//The assignment and < operator work for values of type T.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <=... <= a[numberUsed - 1].
//template function prototype for swapValues
template<class T>
void swapValues(T& variable1, T& variable2);
//Interchanges the values of variable1 and variable2.
//The assignment operator must work correctly for thwe type T.
//template function prototype for indexOfSmallest
template<class T>
int indexOfSmallest(const T a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Array elements have values.
//The assignment and < operator work for values of type T.
//Returns the index i such that a[i] is the smallest of the values
//a[startIndex], a[startIndex + 1],..., a[numberUsed - 1].

template<class T> void sort(T a[], int numberUsed){
int mini; //to store the min index
for(int i=0;i<numberUsed;i++){ //looping through the array
mini = indexOfSmallest(a, i, numberUsed); //getting the smallest index of the sub array
swapValues(a[mini], a[i]); //swapping values
}
}
template<class T> void swapValues(T& variable1, T& variable2){
T temp; //temp variable
//swapping two variables with the use of a third temp variable
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
template<class T> int indexOfSmallest(const T a[], int startIndex, int numberUsed){
int min_idx;
min_idx = startIndex; //set the min index as start index
for (int j = startIndex; j < numberUsed; j++) //loop through the sub array
if (a[j] < a[min_idx]) //if a smaller element is found change the min index
min_idx = j;
return min_idx; //return min index
}

#include "sort.cpp"
//main method
int main( )
{
int i;//for loop index
int a[10] = {9, 8, 7, 6, 5, 1, 2, 3, 0, 4};//unsorted array
cout << "Unsorted integers:\n";
for (i = 0; i < 10; i++) //displaying unsorted array
cout << a[i] << " ";
cout << endl;
sort(a, 10);//calling the sorting function
cout << "In sorted order the integers are:\n";
for (i = 0; i < 10; i++) //displaying sorted array
cout << a[i] << " ";
cout << endl;
double b[5] = {5.5, 4.4, 1.1, 3.3, 2.2}; //unsorted array
cout << "Unsorted doubles:\n";
for (i = 0; i < 5; i++) //displaying unsorted array
cout << b[i] << " ";
cout << endl;
sort(b, 5); //calling the sorting function
cout << "In sorted order the doubles are:\n";
for (i = 0; i < 5; i++) //displaying sorted array
cout << b[i] << " ";
cout << endl;

char c[7] = {'G', 'E', 'N', 'E', 'R', 'I', 'C'}; //unsorted array
cout << "Unsorted characters:\n";
for (i = 0; i < 7; i++) //displaying unsorted array
cout << c[i] << " ";
cout << endl;
sort(c, 7); //calling the sorting function
cout << "In sorted order the characters are:\n";
for (i = 0; i < 7; i++) //displaying sorted array
cout << c[i] << " ";
cout << endl;

return 0;
}

Sample Output:

Unsorted integers: 9 8 7 6 5 1 2 3 04 In sorted order the integers are: 0 1 2 3 4 5 6 7 8 9 Unsorted doubles: 5.5 4.4 1.1 3.3 2.2 In sorted order the doubles are: 1.1 2.2 3.3 4.4 5.5 Unsorted characters: GENERIC In sorted order the characters are: CE EGINR execution time : 0.305 Si Process returned o (0x0) Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
//CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT