Presentation is loading. Please wait.

Presentation is loading. Please wait.

Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.

Similar presentations


Presentation on theme: "Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project."— Presentation transcript:

1 Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project and Evaluations –Final Exam Reviews Final Exam Times: –Mon 10:00 Class: TBD –Mon 11:25 Class: TBD –Wed 6:25 Class: TBD –Changes: Send email to hanrath@iit.edu NO LATE OR MAKEUP EXAMS GIVEN!!!

2 Survey Time Survey available at: survey.iit.edu

3 Vectors A Vector Is a Resizable Array Must Include File Declaration of Vector Syntax: vector variableName; vector variableName( size ); vector variableName( size, initialValue );

4 #include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); return(0); }

5 Using Vectors May Access Data the Same as Arrays Use Square Brackets ( [ ] )

6 #include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); vals2[0] = 6; cout << vals3[2]; return(0); }

7 Vector Public Functions Remember: ofstream Type Has Public Functions open() and close() Vector Has Public Functions as Well: –resize(int) to Change the Size of the Vector (usually larger) –size() to Return the Current Number of Members in the vector –push_back(element_type elem) a copy of elem is placed at the end of the vector.

8 #include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); vals2[0] = 6; cout << vals3[2] << endl; vals1.resize(50); cout << vals1.size() << endl; vals1.push_back(150); cout << vals1.size() << endl; return(0); }

9 Try This Declare a vector of 5 integers Assign the value 0 to each member of the vector 5 minutes go!

10 #include using namespace std; const int MAXVEC = 5; int main() { vector vals(MAXVEC); for (int i = 0; i < MAXVEC; i++) vals[i] = 0; return(0); }

11 Pass by Reference Pass by Reference Means Passing a Location of a Variable into a Function Modifying Variable in Function Will Modify Value in Original Variable In C++, Function Asks for Pass by Reference by using the Ampersand (&) before Parameter Name E.g. int func(int &num)

12 Passing Vectors to Functions Pass by Value by Default Not the Same as Passing Arrays Can Pass by Reference for Efficiency

13 Passing Vectors to Functions #include using namespace std; void func(vector &); int main() { vector intVector(5); int i; for(i = 0; i < 5; i++) intVector[i] = i; cout << intVector[0]; func(intVector); cout << intVector[0]; return(0); } void func(vector &passedVector) { passedVector[0] = 1000; }


Download ppt "Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project."

Similar presentations


Ads by Google