1 / 45

Chapter 6 Arrays

Chapter 6 Arrays. Passing Arrays to Functions. #include <iostream> 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; }

campj
Télécharger la présentation

Chapter 6 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Arrays

  2. Passing Arrays to Functions #include <iostream> 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] << " "; } } PassArrayDemo.cpp Just as you can pass single values to a function, you can also pass an entire array to a function.

  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. 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. #include <iostream> 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] } PassByReferenceDemo.cpp

  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 constkeyword 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. Compile error ConstArrayDemo.cpp

  7. const Parameters ConstArrayDemo.cpp Compile error

  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. 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. ReverseArray.cpp #include <iostream> 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. 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. animation Trace the reverse Function int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } } list 1 2 3 4 5 6 newList 0 0 0 0 0 0

  13. animation Trace the reverse Method, cont. i = 0 and j = 5 int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } } list 1 2 3 4 5 6 newList 0 0 0 0 0 0

  14. animation Trace the reverse Method, cont. i (= 0) is less than 6 int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } } list 1 2 3 4 5 6 newList 0 0 0 0 0 0

  15. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 0 0 0 0 0 1

  16. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 0 0 0 1

  17. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); i (=1) is less than 6 void reverse(constint list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) { newList[j] = list[i]; } } list 1 2 3 4 5 6 newList 0 0 0 0 0 1

  18. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 0 0 0 0 2 1

  19. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 0 0 2 1

  20. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 0 0 2 1

  21. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 0 0 0 3 2 1

  22. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 0 3 2 1

  23. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 0 3 2 1

  24. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 0 0 4 3 2 1

  25. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 4 3 2 1

  26. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 0 4 3 2 1

  27. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 0 5 4 3 2 1

  28. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 5 4 3 2 1

  29. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 0 5 4 3 2 1

  30. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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] list 1 2 3 4 5 6 newList 6 5 4 3 2 1

  31. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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 list 1 2 3 4 5 6 newList 6 5 4 3 2 1

  32. animation Trace the reverse Method, cont. int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(constint 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. list 1 2 3 4 5 6 newList 6 5 4 3 2 1

  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 searchand binary search.

  34. Linear Search • The linear search approach compares the key element, key, sequentially with each element in the array list. • 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. • If a match is made, the linear search returns the index of the element in the array that matches the key. • If no match is found, the search returns -1.

  35. animation Linear Search Animation Key List 3 3 3 3 3 3

  36. From Idea to Solution Trace the function 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

  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. Binary Search, cont. Consider the following three cases: • If the key is less than the middle element, you only need to search the key in the first half of the array. • If the key is equal to the middle element, the search ends with a match. • If the key is greater than the middle element, you only need to search the key in the second half of the array.

  39. animation Binary Search Key List 8 8 8

  40. Binary Search, cont.

  41. Sorting Arrays • Sorting, like searching, is also a common task in computer programming. • It would be used, for instance, if you wanted to display the grades from Listing 6.2, AssignGrade.cpp, in alphabetical order. • Many different algorithms have been developed for sorting. This section introduces two simple sorting algorithms: selection sort and insertion sort.

  42. Selection Sort • 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. • Figure 6.17 shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort.

  43. Selection Sort

  44. #include <iostream> using namespace std; void selectionSort(double [], int); void printArray(double list[], intarraySize); // 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[], intarraySize) { for (inti = 0; i < arraySize; i++) { cout << list[i] << " "; } } void selectionSort(double list[], intarraySize) { for (inti = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; intcurrentMaxIndex = 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. SelectionSort.cpp void selectionSort(double list[], intarraySize) { for (inti = arraySize - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; intcurrentMaxIndex = 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; } } }

More Related