Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OOP Assignment 5
- #include <iostream>
- using namespace std;
- template <class T>
- class Sort {
- private:
- int size;
- T *arr;
- public:
- Sort(int size) {
- this->size = size;
- arr = new T[size];
- }
- ~Sort() {
- delete[] arr;
- }
- void populate(){
- for (int i = 0; i < size; i++) {
- cin >> arr[i];
- }
- }
- void selectSort(){
- T temp;
- int min;
- for (int i = 0; i < size; i++){
- min = i;
- for (int j = i + 1; j < size; j++) {
- if (arr[min] > arr[j]) {
- min = j;
- }
- }
- temp = arr[min];
- arr[min] = arr[i];
- arr[i] = temp;
- }
- }
- void display() {
- // cout << "\nSorted Array: " << endl;
- cout << "[ ";
- for (int i = 0; i < size; i++) {
- if (i != size - 1) {
- cout << arr[i] << ", ";
- } else if (i == size - 1) {
- cout << arr[i] << " ]";
- }
- }
- cout << endl;
- }
- };
- int main() {
- int choice;
- while (true) {
- cout << "\n---------- MENU ----------" << endl;
- cout << "1. Sort Integer Values" << endl;
- cout << "2. Sort Float Values" << endl;
- cout << "3. Exit" << endl;
- cout << "---------------------------" << endl;
- cout << "Enter your choice: "; cin >> choice;
- cout << endl;
- if (choice == 1) {
- int size;
- cout << "Enter the number of elements: "; cin >> size;
- Sort<int> zuck(size);
- zuck.populate();
- cout << "\nThe Entered Array is: " << endl;
- zuck.display();
- zuck.selectSort();
- cout << "\nThe Sorted Array is: " << endl;
- zuck.display();
- }
- else if (choice == 2) {
- int size;
- cout << "Enter the number of elements: "; cin >> size;
- Sort<float> zuck(size);
- zuck.populate();
- cout << "\nThe Entered Array is: " << endl;;
- zuck.display();
- zuck.selectSort();
- cout << "\nThe Sorted Array is: " << endl;
- zuck.display();
- }
- else if (choice == 3) {
- cout << endl;
- cout << "Terminating program..." << endl;
- cout << "Byee" << endl;
- cout << endl;
- break;
- }
- else {
- cout << endl;
- cout << "ERROR! Please Enter a Valid Choice." << endl;
- cout << endl;
- }
- }
- return 0;
- }
- /* OUTPUT:
- ---------- MENU ----------
- 1. Sort Integer Values
- 2. Sort Float Values
- 3. Exit
- ---------------------------
- Enter your choice: 1
- Enter the number of elements: 5
- 16
- 34
- 78
- 59
- 45
- The Entered Array is:
- [ 16, 34, 78, 59, 45 ]
- The Sorted Array is:
- [ 16, 34, 45, 59, 78 ]
- ---------- MENU ----------
- 1. Sort Integer Values
- 2. Sort Float Values
- 3. Exit
- ---------------------------
- Enter your choice: 2
- Enter the number of elements: 3
- 12.45
- 57.34
- 37.42
- The Entered Array is:
- [ 12.45, 57.34, 37.42 ]
- The Sorted Array is:
- [ 12.45, 37.42, 57.34 ]
- ---------- MENU ----------
- 1. Sort Integer Values
- 2. Sort Float Values
- 3. Exit
- ---------------------------
- Enter your choice: 3
- Terminating program...
- Byee
- */
Advertisement
Add Comment
Please, Sign In to add comment