Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent.

Similar presentations


Presentation on theme: "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent."— Presentation transcript:

1 Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent out. Assignment 1 sample solution posted. Today: –Gravatar –Assn 1 Algorithms. –Operator Overloading, Cont. (We have already covered what you need for assn 2.)

2 Presentation Schedule – Week 6 Tuesday, Oct. 20 – Basswood, Beech, Cherry, Walnut. Thursday, Oct. 22, Lecture Time – Hickory, Maple, Oak, Birch. Thursday, Oct. 22, Tutorial Time (room TBA) – Poplar, BalsamFir, Spruce, Cedar. Friday, Oct. 23 – Hemlock, JackPine, Tamarack, WhitePine. Fall 2015CISC/CMPE320 - Prof. McLeod2

3 Changing Your Icon If you wish to change your Redmine icon and your SlackChat icon: Go to https://gravatar.com/site/signup to create your “globally recognized avatar”. It will prompt you to create a wordpress account with your email and a photo. You can supply more info, but don’t have to. Use the email address (queensu with netID) used with Redmine. Fall 2015CISC/CMPE320 - Prof. McLeod3

4 Assignment 1 Algorithm Choices The “Good, Bad & Ugly” ways to do this assignment. Focus on generating and sorting unique insults. Ugly, for example: –Append a new insult and re-sort entire vector every time. –Add enough insults, sort, then eliminate duplicates, then generate missing insults, and repeat until entire set is complete and unique. –… Fall 2015CISC/CMPE320 - Prof. McLeod4

5 Assignment 1 Algorithm Choices, Cont. You can make “non-ugly” choices, which can still be improved upon. Benchmark tests to generate 10,000 unique insults in order: Going from Bad to Good in milliseconds: 7349: Strings only (vector ). Sequential search followed by single coded insertion sort. 2154: Strings only. Sequential search followed by single algorithm::sort. Fall 2015CISC/CMPE320 - Prof. McLeod5

6 Assignment 1 Algorithm Choices, Cont. You know that comparing strings is slow compared to integers, right? Since you have three columns of phrases of 50 terms each, and they are in order (or would be easy to sort since of only size 50), then generate a “hash” code to represent an insult: hash = rand(50) + 100 * rand(50) + 10000 * rand(50) Then sort and search integers instead. Fall 2015CISC/CMPE320 - Prof. McLeod6

7 Assignment 1 Algorithm Choices, Cont. Benchmarks in milliseconds, continued: 1165: Integer "hash". Inserting ints, keeping vector in order. Used algorithm::binary_search each pass. 852: Integer "hash". Sequential search followed by single algorithm::sort using vector. Generated insult strings with single pass. Fall 2015CISC/CMPE320 - Prof. McLeod7

8 Assignment 1 Algorithm Choices, Cont. 29 milliseconds!: Use a bool[50][50][50] array. Initialize all entries to false and then initialize 10,000 random positions to true. Rebuild the vector of insults in a triply nested loop, which will be in order since the phrases are already in order. No sorting or searching! Could also use a bitset object, which would be more memory efficient. Runs about the same time. Fall 2015CISC/CMPE320 - Prof. McLeod8

9 bool boolArray[sourceSize][sourceSize][sourceSize]; for (num1 = 0; num1 < sourceSize; num1++) for (num2 = 0; num2 < sourceSize; num2++) for (num3 = 0; num3 < sourceSize; num3++) boolArray[num1][num2][num3] = false; while (count < numInsults) { num1 = randInt(sourceSize); num2 = randInt(sourceSize); num3 = randInt(sourceSize); if (!boolArray[num1][num2][num3]) { boolArray[num1][num2][num3] = true; count++; } // end if } // end while Fall 2015CISC/CMPE320 - Prof. McLeod9

10 count = 0; for (num1 = 0; num1 < sourceSize && count < numInsults; num1++) for (num2 = 0; num2 < sourceSize && count < numInsults; num2++) for (num3 = 0; num3 < sourceSize && count < numInsults; num3++) if (boolArray[num1][num2][num3]) { insult = "Thou " + part1.at(num1) + " " + part2.at(num2) + " " + part3.at(num3) + "!"; insults.push_back(insult); count++; } // end if Fall 2015CISC/CMPE320 - Prof. McLeod10

11 Fall 2015CISC/CMPE320 - Prof. McLeod11 Overloading Conversion Operators You don’t need to write these for assignment 2. Consider that your Fraction class has top for the numerator and bottom for the denominator. To overload a cast to type double : Fraction::operator double() const { return static_cast (top) / bottom; } If test is a Fraction object, this allows stuff like: double aNum = static_cast (test);

12 Fall 2015CISC/CMPE320 - Prof. McLeod12 Overloading Conversion Operators, Cont. Similarly for int. Note the lack of return type. But if you have a conversion operator and an applicable conversion constructor this leads to an ambiguity: Which operator should be used in a mixed type expression? See MyComplexAmbiguous

13 Fall 2015CISC/CMPE320 - Prof. McLeod13 Overloading Conversion Operators, Cont. With just the constructor you can do things like: int aVal = 10; Fraction test = static_cast (aVal); Same as: Fraction test = Fraction(aVal); But you can’t do: aVal = static_cast (test);

14 Fall 2015CISC/CMPE320 - Prof. McLeod14 Overloading Conversion Operators, Cont. So, what’s to do? If you ditch the one number constructor, then you give yourself a lot more work when you need to overload operators for mixed expressions. Might be easiest to keep the constructor and compromise. Write member functions called intVal() and doubleVal() instead? Or declare the constructor explicit ?

15 Fall 2015CISC/CMPE320 - Prof. McLeod15 The explicit Keyword If you want a constructor to just be a constructor and not for use as a rule for implicit type conversions, declare it with explicit. For example: class Fraction { // etc. explicit Fraction(int); // etc. };

16 The explicit Keyword, Cont. If you use explicit : From the MyComplexAmbiguous demo, what is the result of a mixed type expression with a number on one side and a MyComplex instance on the other? Fall 2015CISC/CMPE320 - Prof. McLeod16

17 Fall 2015CISC/CMPE320 - Prof. McLeod17 Overloading the Subscript Operator: [ ] Appropriate for: –A data structure requiring access using an index. –A data structure that wants to make sure index values are legal. See the SafeArray class. –Note the two versions of operator[] used. –Note the use of a friend non-member function. –Note the use of a destructor (getting ahead of myself…)

18 Fall 2015CISC/CMPE320 - Prof. McLeod18 Overloading the Function Call Operator: ( ) Allows an object to behave as if it is a function. Called a function object. The only operator where the number of arguments is not fixed or limited to one or two. See the RandomInt class. Used in the STL.

19 Fall 2015CISC/CMPE320 - Prof. McLeod19 Aside - inline Functions Used in the RandomInt class. Used when the function body is short: –≤ 3 assignment statements. –one conditional. –one return. An inline function is not placed on the activation record stack, but is expanded in place and so is executed faster. Must be in declaration file. The downside is that a copy is made every time the function is invoked – so it should be short.


Download ppt "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent."

Similar presentations


Ads by Google