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

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
C++ for Engineers and Scientists Third Edition
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
 2006 Pearson Education, Inc. All rights reserved Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Module 1: Array ITEI222 - Advance Programming Language.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ECE 264 Object-Oriented Software Development
7 Arrays.
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
4.9 Multiple-Subscripted Arrays
Engineering Problem Solving with C++, Etter
7 Arrays.
Presentation transcript:

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

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

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

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

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

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

Searching Arrays with Linear Search 7

ECE 264: Lecture 19 Searching Arrays with Linear Search 8

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

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

ECE 264: Lecture 19 Multidimensional Arrays 11

ECE 264: Lecture 19 Multidimensional Arrays 12

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

ECE 264: Lecture 19 Vector example 1 14

ECE 264: Lecture 19 Vector example 1 15

ECE 264: Lecture 19 Vector example 1 16

ECE 264: Lecture 19 Vector example 1 17

ECE 264: Lecture 19 Vector example 1 18

ECE 264: Lecture 19 Vector example 1 19

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

Vector Example 2 ECE 264: Lecture 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)" }

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

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

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

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. html ECE 264: Lecture 19 25