COS30008 Semester 1, 2022 Dr.
Markus Lumpe
Swinburne University of Technology
Faculty of Science, Engineering and Technology
ASSIGNMENT COVER SHEET
Subject Code: COS30008
Subject Title: Data Structures and Patterns
Assignment number and title: 2, Indexers, Method Overriding, and Lambdas
Due date: April 7, 2022, 14:30
Lecturer: Dr. Markus Lumpe
Your name: Tran Phuc Lam Your student id: 105505717
Check Mon Mon Tues Tues Tues Tues Tues Wed Wed Wed Wed
10:30 14:30 08:30 10:30 12:30 14:30 16:30 08:30 10:30 12:30 14:30
Tutorial
Marker's comments:
Problem Marks Obtained
1 48
2 30+10= 40
3 58
Total 146
Extension certification:
This assignment has been given an extension and is now due on
Signature of Convener:
1
[Link] 2/26/26, 9:19 AM
Problem 3/[Link]
1 #include "IntVector.h"
2 #include <stdexcept> // Library for discarding exceptions (exceptions) for scope
checking
3
4 // Constructor: Copy the array from the passed argument
5 IntVector::IntVector(const int aArrayOfIntergers[], size_t aNumberOfElements)
6 {
7 fNumberOfElements = aNumberOfElements; //
8 fElements = new int[fNumberOfElements]; // Dynamic memory allocation
9 for (size_t lI = 0; lI < fNumberOfElements; lI ++)
10 {
11 fElements[lI] = aArrayOfIntergers[lI]; // molecular copying
12
13 }
14 }
15 // Destructor: free allocated memory
16 IntVector::~IntVector(){
17 delete[] fElements;
18
19 }
20 // Return the numbers of elements
21 size_t IntVector::size() const {
22 return fNumberOfElements;
23 }
24
25 // Indexer operator: check statics errors
26 const int IntVector::operator[](size_t aIndex) const
27 {
28 if (aIndex >= fNumberOfElements)
29 {
30 throw std::out_of_range("Illegal vector index"); // Throw an error if the index
is out of range 0 -> n-1
31 }
32 return fElements[aIndex];
33 }
34
35 // get function: using operator[] through pointer (*this)
36 const int IntVector::get(size_t aIndex) const{
37 return (*this)[aIndex];
38 }
39
40 // swap function: swap position of 2 elements
41 void IntVector::swap(size_t aSourceIndex, size_t aTargetIndex)
42 {
43 // check the position of two elements
44 if (aSourceIndex < fNumberOfElements && aTargetIndex < fNumberOfElements)
45 {
46 int lTemp = fElements[aSourceIndex];
[Link] Page 1 of 2
[Link] 2/26/26, 9:19 AM
47 fElements[aSourceIndex] = fElements[aTargetIndex];
48 fElements[aTargetIndex] = lTemp;
49 }
50 else {
51 throw std::out_of_range("Illegal vector indices");
52 }
53 }
54
55
56
57
58
59
60
61
[Link] Page 2 of 2
[Link] 2/26/26, 9:19 AM
Problem 3/[Link]
1 #include "SortableIntVector.h"
2
3 // Constructor: call constructor parents of the class IntVector
4 SortableIntVector::SortableIntVector(const int aArrayOfIntegers[], size_t
aNumberOfElements):
5 IntVector(aArrayOfIntegers, aNumberOfElements)
6 {}
7
8 // algorithm bubble sort
9 void SortableIntVector::sort(Comparable aOrderFunction)
10 {
11 size_t lN = size(); // lN is the numbers taked from class parents
12 if (lN < 2) return;
13 for (size_t lI = 0; lI < lN - 1; lI++ ){
14 for (size_t lJ = 0; lJ < lN - lI - 1; lJ++)
15 {
16 // using aOrderFunction (Lambda) to compare
17 if (!aOrderFunction(get(lJ), get(lJ + 1))) // if the element that follows
has "priority" over the element that precedes it according to the rule
18 // so we swap them
19 {
20 this -> swap(lJ, lJ + 1); // Using swap of IntVector
21
22
23 }
24 }
25 }
26 }
27
28
29
30
31
32
33
34
35
[Link] Page 1 of 1
[Link] 2/26/26, 9:19 AM
Problem 3/[Link]
1 #include "ShakerSortableIntVector.h"
2
3 // constructor: Using class-chaining to call parent class SortableIntVector
4 ShakerSortableIntVector::ShakerSortableIntVector(const int aArrayOfIntegers[], size_t
aNumberOfElements) :
5 SortableIntVector(aArrayOfIntegers, aNumberOfElements) {}
6
7 // overwrite of formula sort
8 void ShakerSortableIntVector::sort(Comparable aOrderFunction )
9 {
10 size_t lBeginIndex = 0;
11 size_t lEndIndex = size() - 1;
12 while (lBeginIndex < lEndIndex){
13 for (size_t lI = lBeginIndex; lI < lEndIndex; lI++ ) // load from left to right
14 {
15 if (aOrderFunction(get(lI), get(lI + 1))){
16 swap (lI, lI + 1); // using swap function of class parent
17 }
18
19
20 }
21 lEndIndex--; // narrow the right
22
23
24 for (size_t lI = lEndIndex; lI > lBeginIndex; lI--) // load from right to left
push the biggest to the top
25 {
26 if (aOrderFunction(get(lI - 1), get(lI))){
27 swap (lI - 1, lI);
28 }
29 }
30 lBeginIndex++; // narrow the left
31 }
32 }
33
34
35
36
37
38
[Link] Page 1 of 1