/*A C++ PROGRAM THAT USES FUNCTION TEMPLATES
TO SORT A LIST OF ELEMENTS IN ASCENDING ORDER*/
#include<iostream.h>
#include<conio.h>
template <class T>
void sort(T a[10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]<a[j])
{
T x;
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
cout<<"\nThe elements after sorting are :";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
void main()
{
int a[10],n;
float b[10];
clrscr();
cout<<"\nEnter no of elements :";
cin>>n;
cout<<"\nEnter "<<n<<" integer values :";
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,n);
cout<<"\n\nEnter no of float values :";
cin>>n;
cout<<"\nEnter "<<n<<" Float values :";
for(i=0;i<n;i++)
cin>>b[i];
sort(b,n);
getch();
}
/* OUTPUT
------
Enter no of elements :4
Enter 4 integer values :12 34 2 10
The elements after sorting are : 2 10 12 34
Enter no of float values :4
Enter 4 Float values :1.2 3.5 9.0 7.7
The elements after sorting are : 1.2 3.5 7.7 9
*/
No comments:
Post a Comment