Program:
Download Runnable Code
#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
int i,j;t tem;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]<a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
}
cout<<"\n\t--------After Sorting---------\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
}
int main()
{
int i,n,a[25],ch;float b[25];bool exit=false;
cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
cin>>n;
do{
cout<<"\n\t--------MENU---------\n\n\t1.Integer Sorting\n\t2.Floating point Sorting\n\t3.Exit\n\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\t------INT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
break;
case 2:
cout<<"\n\t------FLOAT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>b[i];
sort(b,n);
break;
case 3:
default:
cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
exit=true;
}
}while(!exit);
return 0;
}
Output:
nn@ubuntu:~$ ./a.out
------SORTING USING TEMPLATE-----------
Enter the number of elements: 5
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:1
------INT TYPE-----------
Enter elements:
5
4
3
2
1
--------After Sorting---------
5 4 3 2 1
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:2
------FLOAT TYPE-----------
Enter elements:
1.9
1.2
1.0
1.4
1.7
--------After Sorting---------
1.9 1.7 1.4 1.2 1
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:3
Exiting.....
...www.2k8618.blogspot.com
Download
Download Runnable Code
#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
int i,j;t tem;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]<a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
}
cout<<"\n\t--------After Sorting---------\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
}
int main()
{
int i,n,a[25],ch;float b[25];bool exit=false;
cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
cin>>n;
do{
cout<<"\n\t--------MENU---------\n\n\t1.Integer Sorting\n\t2.Floating point Sorting\n\t3.Exit\n\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\t------INT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
break;
case 2:
cout<<"\n\t------FLOAT TYPE-----------\n\n";
cout<<"\nEnter elements:\n";
for(i=0;i<n;i++)
cin>>b[i];
sort(b,n);
break;
case 3:
default:
cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
exit=true;
}
}while(!exit);
return 0;
}
Output:
nn@ubuntu:~$ ./a.out
------SORTING USING TEMPLATE-----------
Enter the number of elements: 5
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:1
------INT TYPE-----------
Enter elements:
5
4
3
2
1
--------After Sorting---------
5 4 3 2 1
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:2
------FLOAT TYPE-----------
Enter elements:
1.9
1.2
1.0
1.4
1.7
--------After Sorting---------
1.9 1.7 1.4 1.2 1
--------MENU---------
1.Integer Sorting
2.Floating point Sorting
3.Exit
Enter your choice:3
Exiting.....
...www.2k8618.blogspot.com
Download
Great and Useful Article.
ReplyDeleteJava Online Course
Java Online Training
Java EE training
Java Course in Chennai
Java Training in Chennai
Java Training Institutes in Chennai
Java Interview Questions
Java Interview Questions