User Tools

Site Tools


c_-_c_templates:function_template_-_swap

C - C++ Templates - Function Template - Swap

#include<iostream.h>
#include<conio.h>
 
template<class T>
void swap(T &a,T &b)
{
  T temp;
  temp=a;
  a=b;
  b=a;
}
 
 
template<class T1>
void fun(T1 x[],int n) 
{
  int i,j;
 
  for(i=0;i<n-1;i++)
    for(j=n-1;j<i;j--)
      if(x[j]<x[j-1])
        swap(x[j],x[j-1]);
 
  cout<<"\nAscending Order:";
 
  for(i=0;i<n;i++)
  {
    cout<<x[i];
  }
 
  cout<<"\nDescending Order:";
 
  for(i=n-1;i>=0;i--)
  {
    cout<<x[i];
  }
}
 
 
void main()
{
  int k[]={3,4,5,2,1,0};
  float f[]={9.3,6.5,6.2,8.3,2.3,4.8};
  clrscr();
  fun(k,6);
  fun(f,6);
  getch();
}
c_-_c_templates/function_template_-_swap.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki