-
Notifications
You must be signed in to change notification settings - Fork 2
/
Array.h
executable file
·120 lines (110 loc) · 2.65 KB
/
Array.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
||
|| @file Array.h
|| @version 1.0
|| @author Alexander Brevig
|| @contact [email protected]
||
|| @description
|| | Provide an easy way of managing raw c++ arrays
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#ifndef ARRAY_H
#define ARRAY_H
#include <Arduino.h>
template<typename type>
class Array {
public:
Array( type* newArray, int newSize ): array(newArray), arraySize(newSize) {/**/}
int size() const{
return arraySize;
}
type getMax(){
type max = array[0];
for (int i=1; i<arraySize; i++){
if (max < array[i]){
max = array[i];
}
}
return max;
}
type getMaxIndex(){
type max = array[0];
type maxIndex = array[0];
for (int i=0; i<arraySize; i++){
if (max <= array[i]){
max = array[i];
maxIndex = i;
}
}
return maxIndex;
}
type getMin(){
type min = array[0];
for (int i=1; i<arraySize; i++){
if (min > array[i]){
min = array[i];
}
}
return min;
}
type getMinIndex(){
type min = array[0];
type minIndex = array[0];
for (int i=0; i<arraySize; i++){
if (min >= array[i]){
min = array[i];
minIndex = i;
}
}
return minIndex;
}
type getAverage(){
type sum = 0;
for (int i=0; i<arraySize; i++){
sum += array[i];
}
return (sum/arraySize);
}
// subscript operator for non-const objects returns modifiable lvalue
type &operator[]( int index ) {
// simple error handling
if (index>=arraySize){
return array[arraySize-1];
}else if (index<0){
return array[0];
}else{
return array[index];
}
}
// subscript operator for const objects returns rvalue
type operator[]( int index ) const {
// simple error handling
if (index>=arraySize || index<0){
return 0;
}else{
return array[index];
}
}
private:
type* array;
int arraySize;
};
#endif