To sort 10 number in asscending order using function template.
Program:-
#include <conio.h>
using namespace std;
template <class T>
void Sort(T a[],int n)
{
T hold;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
}
}
int main()
{
int i,j;
int x[10]={1,8,7,4,6,7,4,2,5,7};
float y[10]={0.2,0.5,1.1,2.1,0.33,5.6,9.7,1.59,8.8,0.001};
Sort(x,10);
Sort(y,10);
cout<<"------Sorted X array-----------"<<endl;
for(i=0;i<10;i++)
{
cout<<x[i]<<endl;
}
cout<<"------Sorted Y array-----------"<<endl;
for(j=0;j<10;j++)
{
cout<<y[j]<<endl;
}
cout<<"!!!!!!!!!!!Thank You!!!!!!!!!!!"<<endl;
getch();
return 0;
}
Output:-
0 comments:
Post a Comment