//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.
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.
//CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....
C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...
Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...
Selection Sort is a common algorithm to sort the elements of an array. First, it scans the elements of the array to find the smallest value and places it at index 0, thus creating a sorted “subarray” of size 1 that contains the smallest value. Then it scans the remaining unsorted values for the new smallest value and places it at index 1, creating a sorted subarray of size 2 that contains the 2 smallest values. It continues in this...
PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...
Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { int temp = numbers[i]; /* put numbers[i] somewhere to keep it safe */ numbers[i] = numbers[j]; /* put numbers[j] at index i */ numbers[j] = temp; /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...
IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template Classes – Chapter 12 Write an example of a template function that can swap 2 generic elements. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() { int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...