-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgeneric_max.cpp
More file actions
44 lines (41 loc) · 1 KB
/
generic_max.cpp
File metadata and controls
44 lines (41 loc) · 1 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
/* generic_max.cpp */
#include <iostream>
#include <random>
#define N 4
#define RMAX 10
using namespace std;
// defines a generic class
template <class X> class myArray{
public:
X data[N];
X max();
};
// defines a generic function
template <class X> X myArray<X>::max(){
X largest = data[0];
for(int i = 1; i < sizeof(data)/sizeof(data[0]); i++){
if(largest < data[i]){
largest = data[i];
}
}
return largest;
}
int main(){
random_device rnd;
mt19937 mt(rnd());
uniform_real_distribution<> rnd_real(0, 1);
myArray<int> forInt;
myArray<double> forDouble;
cout << endl << "forInt.data = ";
for (int i = 0; i < N; ++i) {
forInt.data[i] = rnd() % RMAX;
cout << forInt.data[i] << " ";
}
cout << endl << "forDouble.data = ";
for (int i = 0; i < N; ++i) {
forDouble.data[i] = rnd_real(mt);
cout << forDouble.data[i] << " ";
}
cout << endl << endl << "forInt.max() = " << forInt.max() << endl;
cout << "forDouble.max() = " << forDouble.max() << endl << endl;
}