0% found this document useful (0 votes)
17 views

Assignment No.5

The document provides a template for a selection sort function that can sort both integer and float arrays. The function template uses generics to accept either data type. The code is tested by sorting sample integer and float arrays.

Uploaded by

dekuled5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment No.5

The document provides a template for a selection sort function that can sort both integer and float arrays. The function template uses generics to accept either data type. The code is tested by sorting sample integer and float arrays.

Uploaded by

dekuled5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

Write a function template for selection sort that inputs, sorts and outputs an integer array
and
a float array.
*/

#include<iostream>
using namespace std;
int n;
#define size 10
template<class T>
void sel(T A[size])
{
int i,j,min;
T temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
cout<<"\nSorted array:";
for(i=0;i<n;i++)
{
cout<<" "<<A[i];
}
}

int main()
{
int A[size];
float B[size];
int i;
int ch;
do
{
cout<<"\n* * * * * SELECTION SORT SYSTEM * * * * *";
cout<<"\n--------------------MENU-----------------------";
cout<<"\n1. Integer Values";
cout<<"\n2. Float Values";
cout<<"\n3. Exit";
cout<<"\n\nEnter your choice : ";
cin>>ch;

switch(ch)
{
case 1:
cout<<"\nEnter total no of int elements:";
cin>>n;
cout<<"\nEnter int elements:";
for(i=0;i<n;i++)
{
cin>>A[i];
}
sel(A);
break;
case 2:
cout<<"\nEnter total no of float elements:";
cin>>n;
cout<<"\nEnter float elements:";
for(i=0;i<n;i++)
{
cin>>B[i];
}
sel(B);
break;
case 3:
exit(0);
}
}while(ch!=3);
return 0;
}
OUTPUT:-
* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------
1. Integer Values

2. Float Values

3. Exit

Enter your choice : 1

Enter total no of int elements:3

Enter int elements:22

12

Sorted array: 6 12 22

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

Enter your choice : 2

Enter total no of float elements:3

Enter float elements:11

Sorted array: 5 9 11

* * * * * SELECTION SORT SYSTEM * * * * *


--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

Enter your choice :

You might also like