-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStats.h
More file actions
58 lines (54 loc) · 1.2 KB
/
ArrayStats.h
File metadata and controls
58 lines (54 loc) · 1.2 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
#ifndef ARRAYSTATS_H
#define ARRAYSTATS_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
using namespace std;
template<typename T>
class ArrayStats {
private:
T* array;
int size;
public:
ArrayStats(int size, T value) : size(size) {
if (size <= 0) {
throw invalid_argument("Size must be greater than 0");
}
array = new T[size];
for (int i = 0; i < size; ++i) {
array[i] = value;
}
}
T findMax() {
T max = array[0];
for (int i = 1; i < size; ++i) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
T findMin() {
T min = array[0];
for (int i = 1; i < size; ++i) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
void randomizeValues() {
srand(time(0));
for (int i = 0; i < size; ++i) {
array[i] = static_cast<T>(rand() % 100);
}
}
T* getArray() const {
return array;
}
~ArrayStats() {
delete[] array;
}
};
#endif