Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 Arrays.

Similar presentations


Presentation on theme: "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 Arrays."— Presentation transcript:

1 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 Arrays

2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2 Passing Arrays to Functions Just as you can pass single values to a function, you can also pass an entire array to a function. PassArrayDemo.cpp #include using namespace std; void printArray(int list[], int arraySize); // function prototype int main() { int numbers[5] = {1, 4, 3, 6, 8}; printArray(numbers, 5); system("PAUSE"); return 0; } void printArray(int list[], int arraySize) { for (int i = 0; i < arraySize; i++) { cout << list[i] << " "; }

3 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3 Passing Size along with Array Normally when you pass an array to a function, you should also pass its size in another argument. So the function knows how many elements are in the array.

4 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4 Pass-by-Reference Passing an array variable means that the starting address of the array is passed to the formal parameter. The parameter inside the function references to the same array that is passed to the function. No new arrays are created. This is pass- by-reference. PassByReferenceDemo.cpp

5 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5 PassByReferenceDemo.cpp #include using namespace std; void m(int, int []); int main() { int x = 1; // x represents an int value int y[10]; // y represents an array of int values y[0] = 1; // Initialize y[0] m(x, y); // Invoke m with arguments x and y cout << "x is " << x << endl; cout << "y[0] is " << y[0] << endl; system("PAUSE"); return 0; } void m(int number, int numbers[]) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] }

6 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6 const Parameters Passing arrays by reference makes sense for performance reasons. If an array is passed by value, all its elements must be copied into a new array. For large arrays, it could take some time and additional memory space. However, passing arrays by reference could lead to errors if your function changes the array accidentally. To prevent it from happening, you can put the const keyword before the array parameter to tell the compiler that the array cannot be changed. The compiler will report errors if the code in the function attempts to modify the array. ConstArrayDemo.cpp Compile error

7 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7 const Parameters ConstArrayDemo.cpp Compile error

8 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8 Returning an Array from a Function Can you return an array from a function using a similar syntax? For example, you may attempt to declare a function that returns a new array that is a reversal of an array as follows: // Return the reversal of list int[] reverse(const int list[], int size) This is not allowed in C++.

9 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9 Modifying Arrays in Functions, cont. However, you can avoid this restriction by passing two array arguments in the function, as follows: // newList is the reversal of list void reverse(const int list[], list newList[], int size) ReverseArray.cpp

10 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10 ReverseArray.cpp #include using namespace std; // newList is the reversal of list void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } void printArray(const int list[], int size) { for (int i = 0; i < size; i++) cout << list[i] << " "; }

11 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11 ReverseArray.cpp int main() { int size = 6; int list[] = {1, 2, 3, 4, 5, 6}; int newList[6]; reverse(list, newList, size); cout << "The original array: "; printArray(list, 6); cout << endl; cout << "The reversed array: "; printArray(newList, 6); cout << endl; return 0; }

12 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12 Trace the reverse Function void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000000 animation

13 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000000 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 0 and j = 5

14 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000000 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (= 0) is less than 6

15 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000001 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 0 and j = 5 Assign list[0] to result[5]

16 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000001 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 1 and j becomes 4

17 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000001 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=1) is less than 6

18 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000021 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 1 and j = 4 Assign list[1] to result[4]

19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000021 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 2 and j becomes 3

20 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000021 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=2) is still less than 6

21 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 2 and j = 3 Assign list[i] to result[j]

22 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 3 and j becomes 2

23 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 000321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=3) is still less than 6

24 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 004321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 3 and j = 2 Assign list[i] to result[j]

25 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 004321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 4 and j becomes 1

26 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 26 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 004321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=4) is still less than 6

27 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 27 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 054321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 4 and j = 1 Assign list[i] to result[j]

28 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 28 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 054321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 5 and j becomes 0

29 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 29 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 054321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=5) is still less than 6

30 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 30 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 654321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i = 5 and j = 0 Assign list[i] to result[j]

31 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 31 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 654321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } After this, i becomes 6 and j becomes -1

32 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 32 Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); list newList 123456 654321 animation void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } i (=6) < 6 is false. So exit the loop.

33 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 33 Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching is a common task in computer programming. There are many algorithms devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

34 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 34 Linear Search F The linear search approach compares the key element, key, sequentially with each element in the array list. F The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. F If a match is made, the linear search returns the index of the element in the array that matches the key. F If no match is found, the search returns -1.

35 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 35 Linear Search Animation 64197328 64197328 64197328 64197328 64197328 64197328 3 3 3 3 3 3 animation KeyList

36 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 36 From Idea to Solution int[] list = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(list, 4); // returns 1 int j = linearSearch(list, -4); // returns -1 int k = linearSearch(list, -3); // returns 5 Trace the function

37 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 37 Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79 The binary search first compares the key with the element in the middle of the array.

38 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 38 Binary Search, cont. F If the key is less than the middle element, you only need to search the key in the first half of the array. F If the key is equal to the middle element, the search ends with a match. F If the key is greater than the middle element, you only need to search the key in the second half of the array. Consider the following three cases:

39 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 39 Binary Search 12346789 12346789 12346789 8 8 8 KeyList animation

40 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 40 Binary Search, cont.

41 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 41 Sorting Arrays F Sorting, like searching, is also a common task in computer programming. F It would be used, for instance, if you wanted to display the grades from Listing 6.2, AssignGrade.cpp, in alphabetical order. F Many different algorithms have been developed for sorting. This section introduces two simple sorting algorithms: selection sort and insertion sort.

42 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 42 F Selection sort finds the largest number in the list and places it last. It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number. F Figure 6.17 shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort. Selection Sort

43 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 43 Selection Sort

44 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 44 #include using namespace std; void selectionSort(double [], int); void printArray(double list[], int arraySize); // function prototype int main() { double list[] = {2, 9, 5, 4, 8, 1, 6}; selectionSort(list, 7); printArray(list, 4); system("PAUSE"); return 0; } void printArray(double list[], int arraySize) { for (int i = 0; i < arraySize; i++) { cout << list[i] << " "; } void selectionSort(double list[], int arraySize) { for (int i = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; int currentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } // Swap list[i] with list[currentMaxIndex] if necessary; if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; list[i] = currentMax; } SelectionSort.cpp

45 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 45 void selectionSort(double list[], int arraySize) { for (int i = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; int currentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } // Swap list[i] with list[currentMaxIndex] if necessary; (list[i] is in the right place) if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; // list[i] is in the right place list[i] = currentMax; } SelectionSort.cpp


Download ppt "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 6 Arrays."

Similar presentations


Ads by Google