Presentation is loading. Please wait.

Presentation is loading. Please wait.

ㅎㅎ Fourth step for Learning C++ Programming Call by value

Similar presentations


Presentation on theme: "ㅎㅎ Fourth step for Learning C++ Programming Call by value"— Presentation transcript:

1 ㅎㅎ Fourth step for Learning C++ Programming Call by value
Call by reference Array 2-D Array Array Searching Bubble-Sort Binary-Search

2 Call-by-value vs Call-by-reference
So far we looked at functions that get a copy of what the caller passed in. - This is call-by-value, as the value is what gets passed in (the value of a variable). We can also define functions that are passed a reference to a variable. - This is call-by-reference, the function can change a callers variables directly.

3 Call-by-value vs Call-by-reference
A reference variable is an alternative name for a variable. A shortcut. A reference variable must be initialized to reference another variable. Once the reference is initialized you can treat it just like any other variable.

4 [ Practice 1 - defining and using a reference ]
[ex 1]

5 [ Practice 1 - defining and using a reference ]
[ex 1] & is not the address operator as part of the type identifier & is the address operator &rodents representing the address of the variable to which rodents refers

6 [ Practice 2 - defining and using a reference ]
[ex 2]

7 [ Practice 3 – Swap ] [ex 3]

8 An array is a sequence of consecutive memory elements.
C++ Arrays An array is a sequence of consecutive memory elements. The contents of all elements are of the same type. Could be an array of int, double, char, … We can refer to individual elements by giving the position number (index) of the element in the array. 10/2014

9 int foo[6]; Each int is 4 bytes foo[0] foo[1] foo[5] 4 bytes
Memory and Arrays 4 bytes Each int is 4 bytes foo[0] foo[1] int foo[6]; foo[5] 10/2014

10 foo[17], foo[i+3], foo[a+b+c]
C++ Arrays The first element is the 0th element! If you declare an array of n elements, the last one is number n-1. If you try to access element number n it is an error! The element numbers are called subscripts: foo[i] A subscript can be any integer expression: These are all valid subscripts: foo[17], foo[i+3], foo[a+b+c] 10/2014

11 Initialization Rules for Arrays
1. int cards[4] = {3, 6, 8, 10}; //valid 2. int hand[4] = {}; 3. hand[4]; //invalid 4. float hotelTips[5] = {5.0, 2.5}; //valid - hotelTips[0] = 5.0, - hotelTips[1] = 2.5 5. long totals[500] = {0}; 6. short things[] = {1, 5, 3, 8}; Ο Ο X O Ο Ο

12 [ Practice 4 - Array ]

13 [ Practice 4 - Array ] index→ index →

14 [ Practice 5 - Array 2 ]

15 [ Practice 5 - Array 2 ]

16 2-D Array: int A[3][4] Col 0 Col 1 Col 2 Col 3 Row 0 A[0][0] A[0][1]
10/2014

17 2-D Array: char A[4][3] Memory Organization
{ A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] A[0] A[1] A[2] A[3] char A[4][3]; { A is an array of size 4. Each element of A is an array of 3 chars { { 10/2014

18 [ Practice D Array ]

19 [ Practice D Array ]

20 [ Practice 7 - Array Searching ]

21 Bubble-Sort 10/2014

22 [ Practice 8 - Bubble Sort ]

23 Binary Search

24 Exercise

25 Result

26


Download ppt "ㅎㅎ Fourth step for Learning C++ Programming Call by value"

Similar presentations


Ads by Google