// Selection Sort Function for Descending Order } } } Please rewrite the about f
ID: 3631264 • Letter: #
Question
// Selection Sort Function for Descending Order
}
} }
Please rewrite the about function to template function and put the values in ascending order (the code above is for descending order).Define the other template function to display the contents of the array.
Please write a program to define 3 10-element arrays – one for integer, one for float, and the last one for char. Ask user to input values into these three arrays. Call the selectionsort function to sort the array by using asending order and display your results by calling the display function you define.
Explanation / Answer
please rate - thanks
your code is ascending
hope it's okay, I did a 3rd function to fill the arrays
# include <iostream>
using namespace std;
template <class Type>
void display(Type a[],int n,char c)
{cout<<"array "<<c<<" after sorted ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
template <class Type>
void fill(Type a[],int n,string mess)
{cout<<"fill with "<<mess<<endl;
for(int i=0;i<n;i++)
{cout<<"enter element "<<i+1<<":";
cin>>a[i];
}
}
template <class Type>
void selectionSort(Type x[], int n)
{ for (int pass=0; pass<n-1; pass++) {
int potentialSmallest = pass; // assume this is smallest
//--- Look over remaining elements to find smallest.
for (int i=pass+1; i<n; i++) {
if (x[i] < x[potentialSmallest]) { //--- Remember index for latter swap.
potentialSmallest = i; }
}
//--- Swap smallest remaining element
Type temp = x[pass];
x[pass] = x[potentialSmallest];
x[potentialSmallest] = temp;
} }
int main ()
{int a[10],i;
char b[10];
float c[10];
fill(a,10,"integers");
fill(b,10,"characters");
fill(c,10,"floats");
selectionSort(a,10);
selectionSort(b,10);
selectionSort(c,10);
display(a,10,'a');
display(b,10,'b');
display(c,10,'c');
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.