-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.h
138 lines (117 loc) · 3.15 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef ARRAY_H
#define ARRAY_H
#include "Exception.h"
#include <math.h>
class Array
{
private:
int array_capacity;
int *internal_array;
int array_size;
public:
//Member function declarations
Array(int = 10); //default array size
~Array();
//Accessors
int capacity() const;
bool empty() const;
bool full() const;
int size() const;
int at(int) const;
int sum() const;
double average() const;
double std_dev() const;
double variance() const;
//Mutators
bool append ( int );
void clear();
void swap (Array);
};
//Member function definitions
//The constructor
// -create an instance of the array by
/* 1. Initializing memory for an array of the given capacity, and
2. Allocating memory for an array of the given capacity, and
3 Initializing the array size to 0
*/
Array::Array(int n):
array_capacity( std::max(1,n) ),
internal_array( new int[array_capacity] ),
array_size( 0 ) {
if ( array_capacity <= 0) {
array_capacity = 1;
}
}
//The de-constructor
//DELETE EVERYTHING AND BE FREE
Array::~Array(){
delete [] internal_array;
}
//Return the number of entires in the array
int Array::size() const{
return array_size;
}
int Array::capacity() const{
return array_capacity;
}
//Append a new entry into the array
/* Note that
-when the array size is k, the next item should be appended in location k*
and the size is then k+1 */
bool Array::append( int obj ){
if( full()){
return false;
}
// currently, entries 0,...., arry_size - 1 are occupied
internal_array[array_size] = obj;
++array_size;
return true;
}
void Array::clear(){
array_size = 0;
}
bool Array::empty() const {
return (array_size == 0);
}
bool Array::full() const {
return array_size == array_capacity;
}
int Array::sum() const{
int sum = 0;
for (int i = 0; i < array_size; ++i){
sum = sum + internal_array[i];
}
return sum;
}
double Array::average() const{
if ( empty() ) {
throw division_by_zero();
}
return (sum()*1.0/array_size);
}
double Array::variance() const{
if ( size() <= 1 ) {
throw underflow();
}
double diff = 0;
double avg = average();
for (int i = 0; i < array_size; ++i){
diff += (internal_array[i] - avg)*(internal_array[i] - avg);
}
return (1.0/(array_size -1))*diff;
}
int Array::at( int n ) const {
if ( n < 0 || n >= size()){
throw out_of_range();
}
return internal_array[n];
}
double Array::std_dev() const {
return sqrt(variance());
}
void Array::swap(Array %other){
std::swap( array_capacity, other.array_capacity);
std::swap( internal_array, other.intern_array);
std::swap( array_size, other.array_size);
}
#endif