This is an individual assignment. You must complete this lab on your own, although you may discuss lab concepts with other students. Please keep the Academic Integrity Policy in mind---do not show your code to anyone outside of the professors and ninjas, and do not look at anyone else's code for this lab. If you need help, please come to a tutoring session or contact the professor.
The goal of this lab is to further practice specific concepts in object oriented programming:
First, download the .zip file that contains the starter code for the third programming assignment. It contains the code we worked on last week with our running example of the SafeIntArray. Your assignment will be to write a few more methods and overload a few more operators.
The starter code already contains the name of all the new methods you should write.
I invite you to read the comments that precede the declaration of each method in safeIntArray.h
to learn about the input and output behavior or each method.
You should implement the following methods:
to_string
: takes no input and returns a string that represents the content of the safe array. Each element of the array should be separated by a comma (and nothing else) and the whole string should start and end with a curly brace. For example, if the array contains the numbers 1, 7, 2, and 9 the output of the method should beslice
: takes three integers as input, a start index, an end index and inc, the increment. This should act the same way as the slice operator in Python (remember when you did something like myList[0:n:3]
in Python?). The method should return a new SafeIntArray that contains the elements of the SafeIntArray from which the method was called starting at index start (inclusive), ending at index end (exclusive) with an increment of inc. For example, if a SafeIntArray contains integers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, the taking a slice with inputs 2, 13, 3 should return a SafeIntArray that contains integers 2, 5, 8, 11. The method should throw an exception if the start or end indices are out of bounds, or if the increment is non-positive.SafeIntArray.h
, the default value is already given in the method declaration. operator*
: takes an integer rep as a parameter, and is the same as multiplication for lists in Python. This is called the repetition operator. The method should return a new SafeIntArray that is rep times larger than the original, and whose elements consists of rep consecutive copies of the SafeIntArray from which the method was called. For example, if a SafeIntArray containing integers 1, 2, 3 is "multiplied" by 3, the returned SafeIntArray should contain integers 1, 2, 3, 1, 2, 3, 1, 2, 3 in that order. The method should throw an exception if the number of repetitions is negative.operator==
: takes another SafeIntArray as a parameter, and returns true
if the input SafeIntArray and the SafeIntArray from which the method was called have the same size, and all the elements of the two arrays are the same, and in the same order.count
: takes an integer as input and returns the number of occurences of that element in the SafeIntArray. reverse
: takes no input and returns a new SafeIntArray whose content is the same as the SafeIntArray from which the method was called, except the order of the elements has been reversed.
Make sure that you thoroughly test each method in main.cpp
. When grading your assignment, I will replace your main
function by my own and test every possible bizarre case
// good int *array = new int[size]; // bad int *a = new int[size];
//good if(condition) { cout << "Test" << endl; } //bad if(condition) { cout << "Test" << endl; }*if your text editor's default indenting is four spaces instead of two, don't stress about it. Just be consistent when indenting.
//good if(condition) { cout << "Something" << endl; } //legal but bad if(condition) cout << "Something" << endl;
/** * Name: Martin Gagne * Project: PrgAsst3 * File: SafeIntArray.cpp * Content: implementation of the methods for the class SafeIntArray declared in SafeIntArray.h */
// a comment here is nice and easy to read // it also aligns nicely with the code that it precedes if (complex.condition()) { write.comment().toExplain(); } if (otherComplex.condition()) { // a comment written here gets rather unwieldy if the comment long and the line of code already takes a bit of space moreComplex.stuff(); // and it's annoying to have comment that don't align together, and nobody like to have to scroll to the right to read the whole comment }
void insertAtHead(int value) { // make sure that there is room in the array for the additional element if (this->size == this->capacity) { this->expandCapacity(); } // shift all the current element one position to the right for (int i=size-1; i>=0; i--) { this->theArray[i+1] = this->theArray[i]; } //place the new element this->theArray[0] = value; }
/** * Performs the main quick sort algorithm to sort the provided array. * @param array The array to sort. * @param startIndex The leftmost index (inclusive) of the part of the array * to sort. * @param endIndex The rightmost index (inclusive) of the part of the array * to sort. */ void quickSort(int* array, int startIndex, int endIndex);This type of comment should precede any function or method declared in a .h file. If you use a helper function in a .cpp file that was not declared in a .h file, the comment should precede the definition of the function in the .cpp file.
Submit a .zip file containing the entire project folder for the programming assignment using the submission link on onCourse.