Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development"— Presentation transcript:

1 ECE 264 Object-Oriented Software Development
Instructor: Dr. Honggang Wang Fall 2012 Lecture 18: Arrays and vectors

2 Lecture outline Announcements / reminders Today Lab 5 due next Monday
Project groups: Dr. Wang by Fri., 10/19 Groups of 3 or 4 students Those who don’t choose a group will be randomly assigned Can me with “sub-group”; I’ll fill rest of group Today Arrays Vectors 5/30/2018 ECE 264: Lecture 11

3 Arrays Contain related items of same type Array size is constant
Declaring array allocates space in memory e.g., int c[12]  Array with n elements has indices between 0 and n-1 5/30/2018 ECE 264: Lecture 11

4 Using arrays Can initialize using comma-separated list:
int n[] = {10, 20, 30, 40, 50}; Array size is optional if initialized at declaration Compiler will determine array size Can access individual elements using [] cout << n[1] would print 20 Can pass arrays to functions Array size is ignored Must at least indicate that parameter is array void printArray(int arr[], int size); 5/30/2018 ECE 264: Lecture 11

5 Example 1: Array use Output: Array a1: 1 3 5 7 9
#include <iostream> using std::cout; using std::endl; void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } int main() { int a1[] = {1, 3, 5, 7, 9}; int a2[8]; for (int i = 0; i < 8; i++) a2[i] = i * 4; cout << "Array a1: "; printArray(a1, 5); cout << "Array a2: "; printArray(a2, 8); return 0; Output: Array a1: Array a2: 5/30/2018 ECE 264: Lecture 11

6 Example 2: Common pitfalls
// Assume printArray, includes from previous slide int sumArray(int arr[], int size) { for (int i = 0; i < size; i++) arr[i+1] = arr[i] + arr[i+1]; return arr[size]; } int main() { int a1[] = {1, 3, 5, 7, 9}; cout << "Array a1: "; printArray(a1, 5); cout << "Sum of elements in a1: " << sumArray(a1, 5) << endl; cout << "Reprinting a1: "; return 0; 5/30/2018 ECE 264: Lecture 11

7 Example 2 (cont.) Program outputs (before crashing):
Array a1: Sum of elements in a1: Reprinting a1: What’s wrong with the sum? sumArray function returns arr[size], not arr[size-1] Body of function will actually try to access arr[size] when summing elements Why are values in a1 different? Array names are pointers  always passed by reference 5/30/2018 ECE 264: Lecture 11

8 Vectors Array shortcomings
Fixed size Boundary checking difficult Can’t check array equality Can’t simply copy using assignment operators C++ class template vector addresses all of these issues (must #include <vector> Examples: vector <double> list; //empty vector vector<string> wordList(n); //capacity:n strings //vector of 8 integers, each initialized to 0 vector<int> intList(8,0); 5/30/2018 ECE 264: Lecture 11

9 Operators The square bracket operator ( [ ] ) is defined for vector objects. The assignment operator ( = )is defined for vectors of the same type. Example: vector<int> list(10); vector<int>newList; list[0]=10; newList = list; cout << newList.size() << endl; //method 5/30/2018 ECE 264: Lecture 12

10 Vector methods Common member functions:
empty()returns true if vector contains no values clear() removes all elements from the vector pop_back()deletes last element in vector push_back(element) add element to end of vector resize(int) changes the size of vector If new size > old size, elements are initialized to default value size() returns the size of vector at(int)allows you to insert element in vector, but also provides boundary checking 5/30/2018 ECE 264: Lecture 12

11 Example 3: Vector functions
#include <iostream> #include <vector> using std::cout; using std::endl; using std::vector; int main() { vector <int> example; //Vector to store integers example.push_back(3); //Add 3 onto the vector example.push_back(10); //Add 10 to the end example.push_back(33); //Add 33 to the end for (int x = 0; x < example.size(); x++) cout << example[x] << " "; cout << endl; Output: 5/30/2018 ECE 264: Lecture 11

12 Example 3 (cont.) if (!example.empty()) //Checks if empty example.clear(); //Clears vector vector <int> another_vector; another_vector.push_back(10); //10->end of vector example.push_back(10); //Same if (example==another_vector) //To show testing equality example.push_back(20); for (int y=0; y<example.size(); y++) cout<<example[y]<<" "; cout << endl; Output (cont.): 10 20 5/30/2018 ECE 264: Lecture 11

13 Example 3 (cont.) vector <int> one_more_vector(10); // Another vector one_more_vector.push_back(1); one_more_vector.push_back(2); one_more_vector.push_back(3); one_more_vector.pop_back(); // Remove last element one_more_vector.pop_back(); one_more_vector.resize(9); cout << "Vector size = " << one_more_vector.size() << endl; cout << one_more_vector[0] << " " << one_more_vector[1] << endl; return 0; } Output (cont.): Vector size = 9 0 0 5/30/2018 ECE 264: Lecture 11

14 Final notes Next time In-class vector programming example Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8th ed. Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. 5/30/2018 ECE 264: Lecture 11


Download ppt "ECE 264 Object-Oriented Software Development"

Similar presentations


Ads by Google