4 Write a template class to swap two integer two double and

4. Write a template class to swap two integer, two double and two string.

5. Write a template class to do the bubble sorting.

6. Write a template class for creating a stack

Solution

4.

5.

#include <iostream>
using namespace std;
template <class X> void bubble_sort(X *items, int size);
template <class X> void show_items(X *items, int size);

int main(void)
{
   int iarray[7] =      {7, 5, 4, 3, 9, 8, 6};
   double darray[5] =   {4.2, 2.5, -0.9, 1.2, 3.0};

   cout << \"Here is unsorted integer array: \" << endl;
   show_items(iarray, 7);
   cout << \"Here is unsorted double array: \" << endl;
   show_items(darray, 5);
   bubble_sort(iarray, 7);
   bubble_sort(darray, 5);
   cout << \"Here is sorted integer array: \" << endl;
   show_items(iarray, 7);
   cout << \"Here is sorted double array: \" << endl;
   show_items(darray, 5);
}

template <class X> void bubble_sort(X *items, int size)
{
   register int i, j;
   X temp;

   for (i = 1; i < size; i++)
    for (j = size-1; j >= i; j--)
      if (items[j-1] > items[j])
        {
          temp = items[j-1];
          items[j-1] = items[j];
          items[j] = temp;
        }
}

template <class X> void show_items(X *items, int size)
{
   int i;

   for(i=0; i < size; i++)
      cout << items[i] << \", \";
   cout << endl;
}
  

6.


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site