//selection using function templates
#include <iostream>
using namespace std;
template <class T>
void sort()
{
int i, j;
T temp;
T n[5];
cout<<"\n Enter five numbers : ";
for(i=0;i<5;i++)
{
cin>>n[i];
}
for(i=0;i<4;i++)
{
for(j=i;j<5;j++)
{
if(n[i]>n[j])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"\n The array in the sorted order is : "<<endl;
for(i=0;i<5;i++)
{
cout<<"\t"<<n[i];
}
}
int main()
{
int choice;
char ans;
do
{
cout<<"\n 1. Integer sort. \n 2. Float sort.";
cout<<"\n Enter the input you want to sort : ";
cin>>choice;
switch(choice)
{
case 1 : sort<int>();
break;
case 2 : sort<float>();
break;
case 3 : cout<<"\n Invalid choice.";
break;
}
cout<<"\n Do u wish to continue (Y/N)?";
cin>>ans;
}while(ans=='Y' || ans=='y');
return 0;
}
/*Output:
[student@localhost ~]$ g++ B2selectionsort.cpp
[student@localhost ~]$ ./a.out
1. Integer sort.
2. Float sort.
Enter the input you want to sort : 1
Enter five numbers : 70
90
1
5
60
The array in the sorted order is :
1 5 60 70 90
Do u wish to continue (Y/N)?y
1. Integer sort.
2. Float sort.
Enter the input you want to sort : 2
Enter five numbers : 2.6
5.9
2.1
4.7
5.3
The array in the sorted order is :
2.1 2.6 4.7 5.3 5.9
Do u wish to continue (Y/N)?n
[student@localhost ~]$ */
#include <iostream>
using namespace std;
template <class T>
void sort()
{
int i, j;
T temp;
T n[5];
cout<<"\n Enter five numbers : ";
for(i=0;i<5;i++)
{
cin>>n[i];
}
for(i=0;i<4;i++)
{
for(j=i;j<5;j++)
{
if(n[i]>n[j])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"\n The array in the sorted order is : "<<endl;
for(i=0;i<5;i++)
{
cout<<"\t"<<n[i];
}
}
int main()
{
int choice;
char ans;
do
{
cout<<"\n 1. Integer sort. \n 2. Float sort.";
cout<<"\n Enter the input you want to sort : ";
cin>>choice;
switch(choice)
{
case 1 : sort<int>();
break;
case 2 : sort<float>();
break;
case 3 : cout<<"\n Invalid choice.";
break;
}
cout<<"\n Do u wish to continue (Y/N)?";
cin>>ans;
}while(ans=='Y' || ans=='y');
return 0;
}
/*Output:
[student@localhost ~]$ g++ B2selectionsort.cpp
[student@localhost ~]$ ./a.out
1. Integer sort.
2. Float sort.
Enter the input you want to sort : 1
Enter five numbers : 70
90
1
5
60
The array in the sorted order is :
1 5 60 70 90
Do u wish to continue (Y/N)?y
1. Integer sort.
2. Float sort.
Enter the input you want to sort : 2
Enter five numbers : 2.6
5.9
2.1
4.7
5.3
The array in the sorted order is :
2.1 2.6 4.7 5.3 5.9
Do u wish to continue (Y/N)?n
[student@localhost ~]$ */
No comments:
Post a Comment