Generic Programming Assignments 4 points each Use the following shell code to solve these problems: 1- Code up a reduce routine that will produce a unique set of numbers in sorted order using the Standard Template Library, STL. 2- Code up a reduce template that will produce a unique set of things in sorted order using the Standard Template Library, STL. 3- Given this functor index, modify it so that you can sort an alternative index with natural occuring data like Days of Week or States by Population. // Reduce Assignment #include #include using namespace std; // definition of global varible const int MAX = 12; // declaration of global functions int reduce(long array[], int size); void show(const long array[], int size); // Main program for testing int main() { long nonUniqueArray[MAX] = {12, 12 ,5, 6, 7, 11, 5, 6, 77, 11, 12, 40}; // show non-unique array cout << "old array with non-unique elements: " << endl; show(nonUniqueArray, MAX); int newsize = reduce(nonUniqueArray, MAX); // now non-unique array becomes unique cout << "new array has only unique elements: " << endl; show(nonUniqueArray, newsize); cout << "size reduced to " << newsize << endl; return (0); } // reduce the non-unique array to unique array, return new size int reduce(long array[], int size) { // CODE UP A REDUCE ROUTINE HERE } // show the array element void show(const long array[], int size) { for (int i = 0; i < size; i++) { cout << array[i] << ' '; } cout << endl; } // Reduce Template Assignment #include #include using namespace std; // definition of global varible const int MAX = 10; // declaration of template functions template int reduce(T array[], int size); template void show(const T array[], int size); // Main program for testing int main() { // test using long instantiation long nonUniqueArray[MAX] = {12, 12 ,5, 6, 11, 5, 6, 77, 11, 12}; // show non-unique array cout << "old array with non-unique elements: " << endl; show(nonUniqueArray, MAX); int newsize = reduce(nonUniqueArray, MAX); // now non-unique array becomes unique cout << "new array has only unique elements: " << endl; show(nonUniqueArray, newsize); cout << "size reduced to " << newsize << endl; cout << endl; // test using string instantiation const char* strArray[MAX] = {"aa", "bb", "bc", "ca", "bc", "aa", "cc", "cd", "ca", "bb"}; // show non-unique array cout << "string array with non-unique elements: " << endl; show(strArray, MAX); newsize = reduce(strArray, MAX); // now non-unique array becomes unique cout << "string array has only unique elements: " << endl; show(strArray, newsize); cout << "size reduced to " << newsize << endl; return (0); } // reduce the non-unique array to unique array, return new size template int reduce(T array[], int size) { // CODE UP A REDUCE TEMPLATE HERE } // show the array element template void show(const T array[], int size) { for (int i = 0; i < size; i++) { cout << array[i] << ' '; } cout << endl; } // Index Functor Assignment // Functor.cpp : demonstrate the STL functor with vector index comparison #include #include #include #include #include "IndexCompare.h" using namespace std; template void iota(ForwardIterator first, ForwardIterator last, T value) { while (first != last) { *first++ = value++; } } const int MAX = 12; int main() { int numbers[] = {37, 33, 29, 36, 32, 35, 39, 34, 30, 38, 31, 40}; vector vecNum(numbers, numbers + MAX); // Display original number array. cout << "--- initial numbers array ---" << endl; vector::iterator iter = vecNum.begin(); for (; iter != vecNum.end(); iter++ ) { cout << *iter << " "; } cout << "\n"; vector indices( vecNum.size() ); // fill indices array cout << "\n--- invoke 'iota' on indices array ---"; iota( indices.begin(), indices.end(), 0 ); // Display original indices array. cout << "\n linear indices array: "; vector::iterator iterIdx = indices.begin(); for (; iterIdx != indices.end(); iterIdx++ ) { cout << *iterIdx << " "; } cout << "\n"; // sort indices array cout << "\n--- invoke 'Sort' on indices based on number array ---"; sort(indices.begin(), indices.end(), IndexCompare::iterator>(vecNum.begin(),vecNum.end())); // Display sorted indices array cout << "\n Sorted indices array: "; for (iterIdx = indices.begin(); iterIdx != indices.end(); iterIdx++ ) { cout << *iterIdx << " "; } cout << "\n"; cout << "\n--- Run check on number array indexed normally ---"; // Display original numbers array. cout << "\n replay numbers array: "; iter = vecNum.begin(); for (; iter != vecNum.end(); iter++ ) { cout << *iter << " "; } cout << "\n"; cout << "\n--- Run check on number array indexed with sorted indices ---"; // Print original nums array indirectly through indices. cout << "\n number array via indices: "; for (int index = 0; index < vecNum.size(); index++ ) cout << vecNum[indices[index]] << " "; cout << "\n"; getchar(); return 0; } // IndexCompare.h - interface for IndexCompare class template #ifndef _INDEXCOMPARE_H_ #define _INDEXCOMPARE_H_ #pragma once template class IndexCompare { public: IndexCompare(random_iterator begin, random_iterator end) : begin(begin), end(end) {} ~IndexCompare() {} bool operator() (unsigned int first, unsigned int second) { return (*(begin + first) < *(begin + second)); } private: random_iterator begin; random_iterator end; }; #endif