-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySorter.cpp
More file actions
94 lines (75 loc) · 2.09 KB
/
ArraySorter.cpp
File metadata and controls
94 lines (75 loc) · 2.09 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
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
#include "ArraySorter.h"
using namespace std;
ArraySorter::ArraySorter( const int aArrayOfNumbers[], unsigned int aArraySize )
{
// copy array into sorter
fArrayOfNumbers = new int[aArraySize];
for ( unsigned int i = 0; i < aArraySize; i++ )
{
fArrayOfNumbers[i] = aArrayOfNumbers[i];
}
fArraySize = aArraySize;
}
ArraySorter::~ArraySorter()
{
// delete memory associated with array
delete [] fArrayOfNumbers;
}
//called at the end of the sorting algorithm to show array state
void ArraySorter::stepCompleted(std::ostream& aOStream)
{
aOStream << "State: [";
// prints out every element of the array
for (int i = 0; i < fArraySize; i++)
{
aOStream << fArrayOfNumbers[i];
if (i < (fArraySize - 1))
{
aOStream << ", ";
}
};
aOStream << "]";
}
//exchanges the underlying array elements specified
void ArraySorter::swapElements(unsigned int aSourcIndex, unsigned int aTargetIndex)
{
//uses "at" method to ensure indices are within bounds
unsigned int lTemporary = at(aTargetIndex);
fArrayOfNumbers[aTargetIndex] = at(aSourcIndex);
fArrayOfNumbers[aSourcIndex] = lTemporary;
}
//processes the index into a range check for validity before returning the value from index
const unsigned int ArraySorter::at(unsigned int aIndex) const
{
if (aIndex > fArraySize)
{
throw range_error("Invalid as index is out of range");
}
return fArrayOfNumbers[aIndex];
}
//returns the range of the array
const unsigned int ArraySorter::getRange() const
{
return fArraySize;
}
//invokes stepCompleted by default. Expected to be called from overriden method(s)
void ArraySorter::sort(std::ostream& aOStream)
{
stepCompleted(aOStream);
}
//outputs textual representation of the underlying array without emitting newlines at the end
std::ostream& operator<<(std::ostream& aOStream, const ArraySorter& aObject)
{
aOStream << "[";
// loops through the underlying array and prints out each element
for (int i = 0; i < aObject.getRange(); i++)
{
aOStream << aObject.at(i);
if (i < (aObject.getRange() - 1))
{
aOStream << ", ";
}
};
aOStream << "]";
return aOStream;
}