1 / 18

CSS 342

CSS 342. Data Structures, Algorithms, and Discrete Mathematics I Lecture 4. 141006. Announcements. Syllabus Review HW due date Rational Example Posted Grader: Pooja S, O ffice H ours: Tu : 4:30 - 5:30, UW1 3 rd floor atrium. Look for Pooja . Today:

kyra-rogers
Download Presentation

CSS 342

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 4. 141006.

  2. Announcements • Syllabus Review • HW due date • Rational Example Posted • Grader: Pooja S, Office Hours: Tu: 4:30 - 5:30, UW1 3rd floor atrium. Look for Pooja. • Today: • Templates: functions and class, • examples: vector, sawp, bubble sort, Insertion Sort • new:

  3. Vectors #include <vector> Using namespace std; int main () { vector<int> first; // empty vector of ints vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second vector<int> fourth (third); // a copy of third intmyints[] = {16,2,77,29}; vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); for (inti=0; i < second.size(); i++) { cout << second.at(i); cout << second[i]; } second.push_back(56); second.pop_back();

  4. Vectors • Excellent data structure choice • Fast, safe-access, good memory characteristics • Built on dynamic array • Templatized so that vector<myFooClass> vFoo(4, foos); vector<Bird> birds(3, eagle);

  5. #include"Bird.h" #include<iostream> #include<string> #include<vector> intmain() { Birdb1("eagle"); Bird b2 = b1; vector<Bird> birds; vector<Bird> birds2(4, b2); vector<Bird> birds3(birds2); birds2.push_back(Bird("penguin")); for (inti = 0; i < birds2.size(); i++) { cout<< birds2[i].getName() << endl; } return 0; }

  6. Swap (w/doubles) voidswap(double&a, double &b) { int temp; temp = a; a = b; b = temp; return; } int main() { double a = 3; double b = 7; cout << "a = " << a << " b = " << b << endl; swap(a, b); cout << "a = " << a << " b = " << b << endl; cin >> a; return 0; }

  7. Templates • Polymorphism • Allows for multiple types to be passed to routine • Works on Function or Class level • Syntax: template <class ItemType> • ItemType is the type utilized throughout code • Code must be able to handle the types utilized

  8. Why Object Oriented Programming (OOP)? • Abstraction • Encapsulation • Hierarchy • Polymorphism

  9. Swap w/Templates template <classItemType> voidswapAll(ItemType &, ItemType&); int main() { doubleaDouble = 3, bDouble = 7; intaInt= 5, bInt = 13; stringaString("First"), bString("Second"); cout << "aDouble = " << aDouble << " bDouble = " << bDouble << std::endl; swapAll(aDouble, bDouble); cout << "aDouble = " << aDouble << " bDouble = " << bDouble << std::endl; cout << "aInt = " << aInt << " bInt = " << bInt << endl; swapAll(aInt, bInt); cout << "aInt = " << aInt << " bInt = " << bInt << endl; return } cout << "aString = " << aString << " bString = " << bString << endl; swapAll(aString, bString); cout << "aString = " << aString << " bString = " << bString << endl; cin >> aInt; return 0; } template <classItemType> voidswapAll(ItemType &a, ItemType &b) { ItemType temp; temp = a; a = b; b = temp; return; }

  10. Computer Scientist of the week Grace Hopper • Invented the first compiler • United States Navy Rear Admiral • Promoted the idea of machine independent languages • Key inventor of COBOL • Popularized the term debugging • Advocated for Navy to move from centralized computers to distributed computers in the 1970s

  11. Bubblesort or Sinking Sort • Used to sort an array of items by traversing (n-1) times and bubbling largest current item to the bottom • Graphical Representation : http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort-example-300px.gif • Bad memory and speed characteristics. • Let’s implement as a function template • “the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems“, Donald Knuth

  12. SortedList: Class Template Example • Example to show templates on a class which maintains sorted list • Example will also show also Insertion sort algorithm (if time permits) • Use a vector as the data structure • Functions to • insert an item, • concatenate two Sorted Lists (if time permits) • sort

  13. #ifndef SORTED_LIST_H #define SORTED_LIST_H #include <iostream> #include <vector> using namespace std; template <class ItemType> class SortedList { public: SortedList(); ~SortedList(); void Insert(constItemType &item); void Print() const; private: vector<ItemType> thelist; }; #include "SortedList.cpp" #endif #include"SortedList.h" #ifndefSORTED_LIST_H template<classItemType> SortedList<ItemType>::SortedList(){ } template<classItemType> voidSortedList<ItemType>::Insert(constItemType &item) { thelist.push_back(item); return; } template<classItemType> voidSortedList<ItemType>::Print() const { for (int i = 0; i < thelist.size(); i++) { cout << thelist[i] << endl; } } #include “SortedList.cpp” #endif

  14. Insertion Sort Sorted Unsorted 29 10 14 37 13 Copy 10 Shift 29 29 29 14 37 13 10 29 14 37 13 Insert 10, copy 14 unsortedTop 10 29 29 37 13 Shift 29 10 14 29 37 13 Insert 14; copy 37 10 14 29 37 13 Shift nothing 10 14 29 37 13 Insert 37; Copy 13 10 14 14 29 37 Shift 37, 29 and 14. 10 13 14 29 37 Insert 13 Or Carranno 311-313, Or: http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort-example-300px.gif

  15. Function for Insertion Sort template<classItemType> voidSortedList<ItemType>::Sort() { for (int place = 1; place < thelist.size(); place++) { ItemType temp = thelist[place]; inti = place; while ((i > 0) && (thelist[i-1] > temp)) { thelist[i] = thelist[i-1]; i--; } thelist[i] = temp; } }

  16. Review Assignment 2

  17. ? p p p p ? q q q q Dynamic Allocation • Pointer declaration int *p, *q; • Dynamic allocation p = new int; q = new int; • Deallocation delete p; p = NULL; • Memory leak q = new int; 4 1 2 ? 3 NULL NULL Leak! ? ? ? ?

  18. Dynamic Allocation • Works with all Object types Bird *pBigBird pBigBird = Bird(“chicken”); string s = pBigBird->species;

More Related