Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings."— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings

2 Lecture outline Announcements / reminders  Project groups posted on Wednesday  Project spec posted on Wednesday  Lab assignment 5 due today  Lab assignment 6 posted on Tuesday Today  Review: Arrays & vectors  More examples ECE 264: Lecture 19 2

3 Review: Arrays Constant size list of items of same type Can initialize using comma-separated list:  int n[] = {10, 20, 30, 40, 50}; Can access individual elements using []  cout << n[1]; would print 20 Can pass arrays to functions  void printArray(int arr[], int size); Pitfalls  Indexing past array boundaries  Array name is a pointer  passed by reference ECE 264: Lecture 19 3

4 Review: Vectors Vectors allow programmer to create “arrays” that:  Are dynamically resizable  Can be assigned to one another  Can be compared for equality  Contain easier generic boundary checking Can access vectors like arrays: v[0]  Can also use vector functions Examples: vector list; //empty vector vector wordList(n); //capacity:n strings //vector of 8 integers, each initialized to 0 vector intList(8,0); ECE 264: Lecture 19 4

5 Review: Vector methods Common member functions:  bool empty(): true if vector contains no values  void pop_back(): deletes last element in vector Does not actually return the element Gives an error if vector is empty  void push_back(element): add element to end of vector  void resize(int): changes the size of vector  size_t size(): returns the size of vector  at( ): allows you to insert element in vector, but also provides boundary checking : type of elements stored in the vector (e.g. int, double)  void clear(): removes all elements from vector ECE 264: Lecture 19 5

6 Searching Arrays with Linear Search Often it may be necessary to determine whether an array contains a value that matches a certain key value.  Called searching. The linear search compares each element of an array with a search key (line 36).  Because the array is not in any particular order, it’s just as likely that the value will be found in the first element as the last.  On average, therefore, the program must compare the search key with half the elements of the array. To determine that a value is not in the array, the program must compare the search key to every element of the array. ECE 264: Lecture 19 6

7 Searching Arrays with Linear Search 7

8 ECE 264: Lecture 19 Searching Arrays with Linear Search 8

9 Multidimensional Arrays Arrays with two dimensions (i.e., subscripts) often represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts.  By convention, the first identifies the element’s row and the second identifies the element’s column. Often called two-dimensional arrays or 2-D arrays. Arrays with two or more dimensions are known as multidimensional arrays. a two-dimensional array  The array contains three rows and four columns, so it’s said to be a 3-by-4 array.  In general, an array with m rows and n columns is called an m- by-n array. ECE 264: Lecture 19 9

10 Multidimensional Arrays  A multidimensional array can be initialized in its declaration much like a one-dimensional array.  The values are grouped by row in braces.  If there are not enough initializers for a given row, the remaining elements of that row are initialized to 0. 10

11 ECE 264: Lecture 19 Multidimensional Arrays 11

12 ECE 264: Lecture 19 Multidimensional Arrays 12

13 Class Template Vector Demonstrates capabilities provided by class template vector that are not available for C- style pointer-based arrays. Standard class template vector is defined in header and belongs to namespace std. ECE 264: Lecture 19 13

14 ECE 264: Lecture 19 Vector example 1 14

15 ECE 264: Lecture 19 Vector example 1 15

16 ECE 264: Lecture 19 Vector example 1 16

17 ECE 264: Lecture 19 Vector example 1 17

18 ECE 264: Lecture 19 Vector example 1 18

19 ECE 264: Lecture 19 Vector example 1 19

20 Vector Example 2 Prompt the user for the name and age of ten persons, and print them in the same order entered, printing name, age, and printing "(youngest)" and "(oldest)" next to the persons with lowest and highest age, respectively. ECE 264: Lecture 19 20

21 Vector Example 2 ECE 264: Lecture 19 21 Repeat 10 times: { prompt the user read name (store in vector) read age (store in vector) } find position (i.e., subscript) containing the lowest age find position (i.e., subscript) containing the highest age Repeat 10 times: { print name followed by " - " followed by age if (position == position of the lowest age) print "(youngest)" if (position == position of the highest age) print "(oldest)" }

22 Vector Example 2 #include #include #include using namespace std; int main() { const int NUM_PERSONS = 10; vector names(NUM_PERSONS); vector ages(NUM_PERSONS); for (int i = 0; i < names.size(); i++) { cout << "Enter name: "; getline (cin, names[i]); cout > ages[i]; cin.get(); // get rid of newline left in the input buffer // (otherwise the next getline would fail) } ECE 264: Lecture 19 22

23 Vector Example 2 /* star assuming that element 0 is both the lowest and the highest (after all, it is the only element checked so far, so it is both the lowest and the highest). Then, compare the rest, starting at element 1 */ int pos_youngest = 0, pos_oldest = 0; for (int i = 1; i < ages.size(); i++) { if (ages[i] < ages[pos_youngest]) { pos_youngest = i; } if (ages[i] > ages[pos_oldest]) { pos_oldest = i; } } ECE 264: Lecture 19 23

24 Vector Example 2 // Now print them cout << endl; for (int i = 0; i < names.size(); i++) { cout << names[i] << " - " << ages[i]; // no newline yet... if (i == pos_youngest) { cout << " (youngest)"; } if (i == pos_oldest) { cout << " (oldest)"; } cout << endl; } return 0; } ECE 264: Lecture 19 24

25 Final notes Next time  Project and strings Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. http://www.mochima.com/tutorials/cpp_examples. html ECE 264: Lecture 19 25


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings."

Similar presentations


Ads by Google