Download presentation
Presentation is loading. Please wait.
Published byLee Gilbert Modified over 8 years ago
2
Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content derived/taken from: http://www.stroustrup.com/Programming/
3
Points of Note Assignments 1, 2, 3, 4, and 5 are now graded Assignment 6 is posted Next class, there is a Test More on this shortly
4
Previously Algorithms Analysis of Algorithms Big Oh and the “Take Home / Online” Quiz began
5
Today’s Plan Quickly go over the homework Review for Test 1 Format Material to Expect
6
A01: MoneyChanger
7
A02: BestGuess
8
A03: Movies
10
A04: Pointers Arrays and Pointers part
11
A04: Pointers StudentCheck
12
A04: Pointers StudentClub.H
13
A04: Pointers StudentClub.cpp
14
A05: DynMemInherit DynSort
15
A05: DynMemInherit vlistTest
16
A05: DynMemInherit Car Truck
17
End Assignment Review Any Questions? Next Quick Big-Oh Review Prep for Test
18
Big Oh – Again Because you can never get enough We will briefly go over some things again Review some rules first
19
1 st Thing to Remember – Order of Classes of Functions
20
2 nd Thing to Remember – Rules of Big-Oh If f(n) is a polynomial of degree d, then f(n) is O(n d ), 1.Drop lower-order terms 2.Drop constant factors The idea is to: Use the smallest possible class of functions So “2n is O(n)” instead of “2n is O(n 2 )” Use the simplest expression of the class So “3n 5 is O(n)” instead of “3n 5 is O(3n)”
21
3 rd Thing - Analyze the Code Correctly We have seen a lot of implementations Let’s make a more generic algorithm to do it
22
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Many will be a constant number of operations Assign these a 1 Assuming NOT in a loop then they execute only once int sum = 0; if (count < 20) { cout << value area = (1.0 / 2.0) * base * height; total = total + current; O(1)
23
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Some will require n operations Assign these n This includes constant stuff inside a loop iterating n times O(1) O(n) for (int j = 1; j <= n; j++) n { cout << “this prints n times” << endl; n ( 1 thing n times) }
24
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Some will require n 2 operations Assign these n 2 Typically these are nested loops and constants in nested loops O(1) O(n 2 ) for (int j = 1; j <= n; j++) still just n { for (int k = 1; k <= n; k++) n 2 (n things n times) { cout << “this prints n^2 times” << endl; n 2 } } O(n)
25
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Some get tricky and require math skills Like n 1/2 (or sqrt(n) ) recall working with positive integers O(1) O(n 1/2 ) for (int j = 1; j*j <= n; j++) n 1/2 { cout << “this prints sqrt(n) times” << endl; n 1/2 } O(n) O(n 2 ) j <= sqrt(n) Tables may help to verify this OR inspire the thought to apply math to make the problem easier
26
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Others require understanding of math stuff like logs log 2 n … thinking in terms of integers gives the number of times n can be divided by 2 O(1) O(lg n) for (int j = n; j > 0; j /= 2) lg n { cout << “this prints lg n times” << endl; lg n } O(n) O(n 2 ) O(n 1/2 ) Again… Tables may help to verify this OR inspire the thought to apply math to make the problem easier
27
Given a C++ function For each statement in the function Assign the worst case number of operations it will use i.e. maximum number of times it executes Sum the operations for each statement Reduce per the Big-Oh rules Drop lower order terms Drop constants i.e. Effectively pick the line that executes the greatest number of operations (in the worst case) This gives us the “worst case” for the entire function And we arrive at our Big-Oh for the function for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { cout << “this prints n^2 times” << endl; } n + n 2 = n + n 2 + n 2 = n + 2 n 2 O(n 2 )
28
Why Summations Before? All of the previous slides are relatively straight forward cases. More difficult cases require more sophisticated tools. As it turns out Math again comes to the rescue
29
Summations and Loops For the “easy” case of 1 loop for (int j = 1; j <= n; j++) Human brain thinks: 1 + 1 +... + 1 there are n additions that means n things
30
Summations and Loops Nested Loops for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { … }
31
And Math Wins For cases like for (int j = 1; j <= n; j++) { for (int k = 1; k <= j ; k++) { … } Human brain thinks: 1 + 2 +... + n uh...
32
And Math Wins For cases like for (int j = 1; j <= n; j++) { for (int k = 1; k <= j ; k++) { … }
33
And Math Wins For cases like for (int j = 1; j <= n; j++) { for (int k = 1; k <= j ; k++) { … } O(n 2 )
34
Enough Big-Oh Get to the test already. =)
35
Game Plan for Test 1 Closed Book No notes of any kind Closed Computer No electronic devices (pace makers or other life sustaining devices maybe) Nothing but a pencil, eraser, brain and one functioning hand needed / required this time Any attempts to cheat will result in beheading umm… maximal punishment allowed per University regulations Do NOT give me any reason to believe you are or even might be cheating
36
Layout for Test 1 Some Short answer / coding questions Circa 10 Some true false Circa 10 Some Multiple choice Circa 10 to 15 There will be THREE (or more) versions of the test Similar but not identical in content
37
Topics for Test 1 General C++ C++ Classes UML Abstract Data Types (ADTs) Standard Template Library (STL) Inheritance Templates Searching / Sorting Looping and Recursion Linked Lists Big Oh Math Stuff
38
General C++ What is the g++ command to compile a file? a program? Can you write functions in C++ global functions and class member functions Things to know about Return values / types Pointers dereferencing pointers address operator things like char * Pass by ??? Constant functions Built in types char, int, double, etc… Enumerated types Operator Overloading new, delete, delete [ ], allocation on the stack vs. on the free store (heap)
39
C++ Classes C++ Class Declaration / Implementation Can you write a C++ header file (.h) In general and from a UML specification Can you write a C++ implementation file (.cpp) In general and from a UML specification How do classes relate to objects How do classes relate to Abstract Data Types File Usage What does a header file do? What does a cpp file do?
40
UML UML stands for ______ How does UML relate to ADTs and classes? Given an ADT description can you relate it to a UML diagram to a C++ class
41
ADTs ADT stands for what How do ADTs relate to UML and C++ classes
42
Standard Template Library (STL) using namespace std std::string some of its functions be able to read string s; s.length() returns what? std::vector what does it allow you to do? It’s like an array, but better vector v; is a vector of what ? integers. std::cout How do you use this function? std::cin And how does this work?
43
Inheritance How does this relate to UML? What does it have to do with C++ classes Can you write the code to derive one class from another How does inheritance affect access to member variables what data can a child see that is declared in it’s parent’s class what data can it access what “types” of data access are there in C++
44
Templates Have some examples handy Can you template a class Can you template a function What is the purpose of templates
45
Searching / Sorting Linear search Binary search Insertion sort Selection sort Describe Walk through Maybe implement at least partial
46
Looping Understand looping particularly with regard to Big-Oh for-loops while-do loops do-while loops any others break
47
Linked Lists How are they implemented in C++ Node and pointer Mostly Singly Linked For extra may want to know about doubly linked lists Be able to read code Use pointers Allocate and De-allocate nodes
48
Big-Oh See all of earlier slides and homework and quizzes
49
Math Stuff There will not be any rigorous proofs but likely a “show or explain” Summation Stuff Properties of logs Concept of Asymptotic (limits, etc) Implicitly likely more (and perhaps explicitly)
50
What to do to Prepare Be sure to sleep sometime before the test NOT during Review the homework Review the quizzes Think about the topics list just provided What questions would you ask about those topics? Read the book Chapters 3 and 4, but glance at 1 and 2 too Write some C++ code
51
What about the Lectures Read/review them There were a lot of examples If you didn’t understand some of them work them out run the code change the code run it again
52
And the Homework All good to look at
53
Questions Any questions that have not yet been asked and or answered
54
The End It says, The End But there is likely time left over Could take the online quiz…
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.