Object Oriented Programming
Group B -
Practical : 5
Problem Statement : Write a function template for selection sort that inputs, sorts and outputs an integer array and
a float array.
Check Out Code Here ๐
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
int n; | |
template < class T > | |
class mysort { | |
private: | |
T a[20]; | |
public: | |
void read(); | |
void display(); | |
void sort(); | |
}; | |
template < class T > | |
void mysort < T > ::read() { | |
cout << "\nEnter the number of the elements you want to insert : "; | |
cin >> n; | |
cout << "\nEnter the numbers : " << endl; | |
for (int i = 0; i < n; i++) { | |
cin >> a[i]; | |
} | |
} | |
template < class T > | |
void mysort < T > ::display() { | |
for (int i = 0; i < n; i++) { | |
cout << "\n" << a[i]; | |
} | |
cout << endl; | |
} | |
template < class T > | |
void mysort < T > ::sort() { | |
T temp; | |
for (int i = 0; i < (n - 1); i++) { | |
for (int j = (i + 1); j < n; j++) { | |
if (a[j] < a[i]) { | |
temp = a[i]; | |
a[i] = a[j]; | |
a[j] = temp; | |
} | |
} | |
for (int k = 0; k < n; k++) //To display Passes | |
{ | |
cout << a[k] << " "; | |
} | |
cout << endl; | |
} | |
} | |
int main() { | |
int ch; | |
do { | |
cout << "\n Enter your choice "; | |
cout << "\n 1. For integer array sorting "; | |
cout << "\n 2. For float array sorting "; | |
cout << "\n 3. Exit" << endl; | |
cout << " ---> "; | |
cin >> ch; | |
switch (ch) { | |
case 1: | |
mysort < int > s1; //for sorting integer numbers | |
s1.read(); | |
cout << "\nThe Elements in the UNSORTED order are as follows:"; | |
s1.display(); | |
cout << endl << "Sorting...\n"; | |
s1.sort(); | |
cout << "\nThe Elements in the SORTED order are as follows:"; | |
s1.display(); | |
break; | |
case 2: | |
mysort < float > s2; //for sorting float numbers | |
s2.read(); | |
cout << "\nThe Elements in the UNSORTED order are as follows:"; | |
s2.display(); | |
cout << "Sorting...\n"; | |
s2.sort(); | |
cout << "\nThe Elements in the SORTED order are as follows:"; | |
s2.display(); | |
break; | |
case 3: | |
cout << " End!!"; | |
break; | |
default: | |
cout << "\n Please, Enter the valid option " << endl; | |
} | |
} while (ch != 3); | |
return 0; | |
} |
Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.๐
Comments
Post a Comment