Question
Homework 5: The objective of this homework is to give you experience using function templates with C++. You will use the provided code in file:
Homework 5: The objective of this homework is to give you experience using function templates with C++. You will use the provided code in file: fig08_13.cpp as a starting point. The file is located in the Source Code folder on Learn. For this assignment, you will write a function template selectionSort based on the code in figure 8.13. Write a driver program that inputs, sorts, and outputs an int array and a float array. Execute your test program and copy the outputs to a text file to demonstrate proper execution.
What to submit: Please submit a copy of your source code file and a text file(s) that includes execution output that demonstrates proper operation.
I am using NetBeans
SOURCE CODE:
// Fig. 8.13: fig08_13.cpp // Selection sort with pass-by-reference. This program puts values into an // array, sorts them into ascending order and prints the resulting array. #include#include using namespace std; void selectionSort( int * const, const int ); // prototype void swap( int * const, int * const ); // prototype int main() { const int arraySize = 10; int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Data items in original order "; for ( int i = 0; i < arraySize; ++i ) cout << setw( 4 ) << a[ i ]; selectionSort( a, arraySize ); // sort the array cout << " Data items in ascending order "; for ( int j = 0; j < arraySize; ++j ) cout << setw( 4 ) << a[ j ]; cout << endl; } // end main // function to sort an array void selectionSort( int * const array, const int size ) { int smallest; // index of smallest element // loop over size - 1 elements for ( int i = 0; i < size - 1; ++i ) { smallest = i; // first index of remaining array // loop to find index of smallest element for ( int index = i + 1; index < size; ++index ) if ( array[ index ] < array[ smallest ] ) smallest = index; swap( &array[ i ], &array[ smallest ] ); } // end if } // end function selectionSort // swap values at memory locations to which // element1Ptr and element2Ptr point void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } // end function swap
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started