Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pealse make sure before you answer this question: you have to make in your answe

ID: 3575050 • Letter: P

Question

pealse make sure before you answer this question:
you have to make in your answer ((FunctionTemplates ,Class Templates))

question :

Implement a program that sort numbers or characters.

1. The program should sort the input from the user. the input can be numbers(integer or float type) or characters (char type).

2. in main function, you only get the input value. make additional sort function and print function for the program.

3. the order of same number or character is not considered.

4. this is the example of program results.

Data items in original order

> 6 4 8 10 12 89 68 45 37

Data items in ascending order

> 4 6 8 10 12 37 45 68 89

Data items in original order

> 5.2 36.5 3.4 12.8 1.9 6.8 24.3

Data items in ascending order

> 1.9 3.4 5.2 6.8 12.8 24.3 36.5

Data items in original order

> I n h a U n i v .

Explanation / Answer

Using function template to sort different data types.

-----------------------------------------------------------------------------

#include<iostream>
using namespace std;

template<class bubble>
void bubble_sort(bubble 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])
           {
               bubble element;
               element = a[i];
               a[i] = a[j];
               a[j] = element;
           }
       }
   }
}

int main()
{
   //declare and assign three arrays of different data type to test for tempate function
   int IntArray[9] = { 6, 4, 8, 10, 12, 89, 68, 45, 37 };
   float FloatArray[7] = { 5.2, 36.5, 3.4, 12.8, 1.9, 6.8, 24.3 };
   char CharArray[8] = { 'I', 'n', 'h', 'a', 'U', 'n', 'i', 'v' };
   int i;

   //call sorting for int array and print array before and after sorting
   printf("Data items in original order >");
   for (i = 0; i < 9; i++)
       printf("%d ", IntArray[i]);
   bubble_sort(IntArray, 9);
   printf(" Data items in ascending order >");
   for (i = 0; i < 9; i++)
       printf("%d ", IntArray[i]);

   //call sorting for float array and print array before and after sorting
   printf(" Data items in original order >");
   for (i = 0; i < 7; i++)
       printf("%.1f ", FloatArray[i]);
   bubble_sort(FloatArray, 7);
   printf(" Data items in ascending order >");
   for (i = 0; i < 7; i++)
       printf("%.1f ", FloatArray[i]);
   //call sorting for char array and print array before and after sorting
   printf(" Data items in original order >");
   for (i = 0; i < 8; i++)
       printf("%c ", CharArray[i]);
   bubble_sort(CharArray, 8);
   printf(" Data items in original order >");
   for (i = 0; i < 8; i++)
       printf("%c ", CharArray[i]);
  
}

--------------------------------------------------------------------------

output

Data items in original order
>6 4 8 10 12 89 68 45 37
Data items in ascending order
>4 6 8 10 12 37 45 68 89
Data items in original order
>5.2 36.5 3.4 12.8 1.9 6.8 24.3
Data items in ascending order
>1.9 3.4 5.2 6.8 12.8 24.3 36.5
Data items in original order
>I n h a U n i v
Data items in original order
>I U a h i n n v