Repository navigation
Expand file tree
/
Copy pathb5.cpp
More file actions
91 lines (74 loc) · 2.28 KB
/
Copy pathb5.cpp
File metadata and controls
91 lines (74 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* 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; // Number of elements in each array
template <class T> // Declares function template with single template parameter T
void sort(T arr[10]) { // Definition of function template 'sort' which takes array 'arr' of type T and size 10
int min;
// Selection sort algorithm
for(int i = 0; i < n; i++) {
min = i;
for(int j = i+1; j < n; j++) {
if(arr[j] < arr[min]) {
min = j;
}
}
// After each iteration, minimum element is swapped with the unsorted array element i at the beginning
T temp = arr[i]; // temp is a variable of type T
arr[i] = arr[min];
arr[min] = temp;
}
// To display sorted array
cout << "\nSorted array: " << endl;
for(int k = 0; k < n; k++) {
cout << arr[k] << " ";
}
cout << endl;
}
int main() {
int A[10], a, ch;
float B[10];
cout << "\n\nMENU:\n" << endl;
cout << "1. Sort integer array" << endl;
cout << "2. Sort float array" << endl;
cout << "\nEND\n" << endl;
a = 1;
while(a == 1) {
cout << "\nEnter your choice: ";
cin >> ch;
switch(ch) {
case 1:
cout << "\nEnter number of elements in the array: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << "Enter an element: ";
cin >> A[i];
}
sort(A);
break;
case 2:
cout << "\nEnter number of elements in the array: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << "Enter an element: ";
cin >> B[i];
}
sort(B);
break;
default:
cout << "\nInvalid choice. Please try again.";
break;
}
string ans;
cout << "\nDo you want to choose again? (enter yes/no)" << endl;
cin >> ans;
if(ans == "yes") {
a = 1;
}
else {
a = 0;
cout << "Thank you!";
}
}
}