Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.

Similar presentations


Presentation on theme: "1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm."— Presentation transcript:

1 1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm

2 2 Exam 2 Lecture Sort% 6597 6394 6292. 5 6089. 6 5988. 1 5886. 6 5886. 6 5683. 6 4973. 1 4770. 1 4567. 2 4465. 7 4161. 2 3958. 2 3856. 7 3856. 7 3247. 8 Exam 2 (Lecture) 17 Scores Avg 50.2/67 (or 75/100)Std dev 10.5

3 3 Exam 2 Questions NbrStudy Question 1 Similar to Ch 5 ex 12 (exam used / instead of %) 2Ch 3 ex 3 3Ch 5 ex 16 4Ch 3 ex 5 5Similar to Ch 6 ex 2 6Ch 10 ex 2

4 4 Project Posted on http://www.cs.iit.edu/~cs105/wed_eve Due date ← Discuss! Fri Apr 29 (2400 hrs)? Or Wed May 4 at noon? Hand in to assignment folder in Blackboard Don't send to TA, don't email to me. Include.cpp (source code), Study the skeleton and think before coding. No working together. May ask you to come in to discuss program.

5 5 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)

6 6 Function Pass by Reference #include using namespace std; void func1(int &num); void func2(int num); main() { int x = 10; func1(x); cout << x << endl;// Prints 0, not 10 // Can't call: func1(10) x = 10; func2(x); cout << x << endl;// Prints 10, not 0 } void func1(int &num) { num = 0; cout << num << endl;// Prints 0 } void func2(int num) { num = 0; cout << num << endl;// Prints 0 }

7 7 Passing Arrays as Parameters Caller has an array E.g., int intArray[100]; Caller specifies name of array and length separately E.g., func(intArray, 100); Callee header specifies an array with size passed as separate parameter: E.g., int func(int array[], int len) Changes to array by callee does update caller's array.

8 8 Passing Arrays Example void setZero(int passedArray[], int len); main() { int intArray[5]; func(intArray, 5); cout << intArray[0]; } // setZero(passedArray, len) sets // passedArray[0], passedArray[1],..., // passedArray[len-1] all to 0. // void setZero(int passedArray[], int len) { int i; for (i = 0; i < len ; i++) passedArray[i] = 0; }

9 9 Two-Dimensional Arrays char letters[10][20]; int values[100][50]; letters[0][0] = 'a'; letters[0][1] = 'b'; letters[9][19] = 'x'; values[0][0] = 99; values[1][5] = 135; values[99][49] = 0;


Download ppt "1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm."

Similar presentations


Ads by Google