C++ Template

Write a program for bubble sort using template programming.

Solution:
template<class T>
void bubbleSort(T a[], int n)
{
     int i, j;
     for(i = 0; i<n-1; i++)
     {
          for(j = i+1; j<n; j++)
          {
               if(a[i] > a[j])
               {
                    T element;
                    element = a[i];
                    a[i] = a[j];
                    a[j] = element;
               }
          }
     }
}
void main()
{
     intsize, n;
     cout<<"Enter the size of integer array"<<endl;
     cin>>size;
     int *arr = newint[size];
     cout<<"Enter the size of char array"<<endl;
     cin>>n;
     char *carr = newchar[n];
     cout<<"Enter the integer element"<<endl;
     for(int i = 0; i<size; i++)
     {
          cin>>arr[i];
     }
     cout<<"Enter the char element"<<endl;
     for(int i = 0; i<n; i++)
     {
          cin>>carr[i];
     }
     bubbleSort(arr, size);
     cout<<"\nSorted Order Integers: ";
     for(int i = 0; i<size; i++)
     cout<<arr[i]<<"\t";
     bubbleSort(carr, n);
     cout<<"\nSorted Order Characters: ";
     for(int j = 0; j<n; j++)
     cout<<carr[j]<<"\t";
     getch();
}

Write a program for sum of array using templates.

template<class T>
int sum(T arr[], int size)
{
     int i;
     int result = 0;
     for(i = 0; i < size; i++)
     {
          result += arr[i];
     }
     return result;
}
int main()
{
     intsize,n;
     cout<<"Enter the size of integer array"<<endl;
     cin>>size;
     int *arr = newint[size];
     cout<<"Enter the integer element"<<endl;
     for(int i = 0; i < size; i++)
     {
          cin>>arr[i];
     }
     cout<<"Sum of integer array =:"<< sum(arr, size)<<endl;
     return 0;
}

Write a program to find largest of three numbers using function template.

Solution:
template<class T>
T greatestNum(T x, T y, T z)
{
     if(x > y)
     {
          if(x > z)
          {
               return x;
          }
          else
          {
               return z;
          }
     }
     elseif(y > x)
     {
          if(y > z)
          {
               return y;
          }
          else
          {
               return z;
          }
     }
}
int main()
{
     int choice;
     int a,b,c;
     float x,y,z;
     double p,q,r;
     char option;
     do
     {
          cout<<"Largest of three numbers using function template"<<endl;
          cout<<"1. Among integer numbers"<<endl;
          cout<<"2. Among float numbers"<<endl;
          cout<<"3. Among double numbers"<<endl;
          cin>>choice;
          switch(choice)
          {
               case 1:
               cout<<"Enter three Integer numbers"<<endl;
               cin>>a>>b>>c;
               cout<<"The greatest integer is "<<greatestNum(a,b,c)<<endl;
               break;
               case 2:
               cout<<"Enter three floating numbers"<<endl;
               cin>>x>>y>>z;
               cout<<"The greatest float number is "<<greatestNum(x,y,z)<<endl;
               break;
               case 3:
               cout<<"Enter three double numbers"<<endl;
               cin>>p>>q>>r;
               cout<<"The greatest float number is "<<greatestNum(p,q,r)<<endl;
               break;
          }
     cout<<"Do you want to continue (y/n)"<<endl;
     cin>>option;
     }
     while(option=='y' || option=='Y');
     return 0;
}

You can also find the largest number among three numbers by using ternary operator.

Example:
T greatestNum(T x, T y, T z)
{
     T big;
     big = x > y ? (x > z ? x : z) : (y > z ? y : z) ;
     return big;
}