-
Notifications
You must be signed in to change notification settings - Fork 45
/
B5.cpp
84 lines (64 loc) · 1.72 KB
/
B5.cpp
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
/* OOPL – ASSIGNMENT – B3 */
#include<iostream>
using namespace std;
template < typename T >
void input(T * a, int size) {
cout << "\nEnter " << size << " elements in array: ";
for (int i = 0; i < size; i++)
cin >> a[i];
}
template < typename T >
void sorting(T * a, int size) {
int min;
for (int i = 0; i < size; i++) {
min = i;
for (int j = i + 1; j < size; j++)
if (a[j] < a[min])
min = j;
swap(a[i], a[min]);
}
cout << "\n";
}
template < typename T >
void display(T * a, int size) {
for (int i = 0; i < size; i++)
cout << a[i] << "\t";
}
int main() {
cout << "\nHow many elements you want to sort: ";
int size;
cin >> size;
if (size < 1) {
cout << "\nArray size can not be less than 0. Initializing size to 1";
size = 1;
}
int a[size];
input(a, size);
cout << "\nElements before sorting\n";
display(a, size);
cout << "\nElements after sorting\n";
sorting(a, size);
display(a, size);
float b[size];
input(b, size);
cout << "\nElements before sorting\n";
display(b, size);
cout << "\nElements after sorting\n";
sorting(b, size);
display(b, size);
return 0;
}
/*
----------------- OUTPUT ------------------
How many elements you want to sort: 5
Enter 5 elements in array: 5 8 1 2 12
Elements before sorting
5 8 1 2 12
Elements after sorting
1 2 5 8 12
Enter 5 elements in array: 1.2 5.4 1.9 4.3 9.5
Elements before sorting
1.2 5.4 1.9 4.3 9.5
Elements after sorting
1.2 1.9 4.3 5.4 9.5
*/