Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.

Similar presentations


Presentation on theme: "© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings."— Presentation transcript:

1 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings

2 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 2 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.

3 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 3 Solution AnalyzeNumbers Run with prepared input

4 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 4 Objectives F To describe why an array is necessary in programming (§7.1). F To declare arrays (§7.2.1). F To access array elements using indexes (§7.2.2). F To initialize the values in an array (§7.2.3). F To program common array operations (displaying arrays, summing all elements, finding min and max elements, random shuffling, shifting elements) (§7.2.4). F To apply arrays in application development (LottoNumbers, DeckOfCards) (§§7.3–7.4). F To define and invoke functions with array arguments (§§7.5–7.7). F To define functions involving array parameters (CountLettersInArray) (§7.8). F To search elements using the linear (§7.9.1) or binary search algorithm (§7.9.2). F To sort an array using the selection sort (§7.10). F To represent strings using C-strings (§7.11).

5 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 5 Introducing Arrays Array is a data structure that represents a collection of the same types of data.

6 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 6 Declaring Array Variables datatype arrayRefVar[arraySize]; Example: double myList[10]; C++ requires that the array size used to declare an array must be a constant expression. For example, the following code is illegal: int size = 4; double myList[size]; // Wrong But it would be OK, if size is a constant as follow: const int size = 4; double myList[size]; // Correct

7 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 7 Arbitrary Initial Values When an array is created, its elements are assigned with arbitrary values.

8 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 8 Indexed Variables The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arraySize-1. In the example in Figure 7.1, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayName[index]; For example, myList[9] represents the last element in the array myList.

9 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 9 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];

10 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 10 No Bound Checking C++ does not check array’s boundary. So, accessing array elements using subscripts beyond the boundary (e.g., myList[-1] and myList[11]) does not does cause syntax errors, but the operating system might report a memory access violation.

11 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 11 Array Initializers Declaring, creating, initializing in one step: dataType arrayName[arraySize] = {value0, value1,..., valuek}; double myList[4] = {1.9, 2.9, 3.4, 3.5};

12 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 12 Declaring, creating, initializing Using the Shorthand Notation double myList[4] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

13 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 13 CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double myList[4]; myList = {1.9, 2.9, 3.4, 3.5};

14 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 14 I mplicit Size C++ allows you to omit the array size when declaring and creating an array using an initilizer. For example, the following declaration is fine: double myList[] = {1.9, 2.9, 3.4, 3.5}; C++ automatically figures out how many elements are in the array.

15 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 15 Partial Initialization C++ allows you to initialize a part of the array. For example, the following statement assigns values 1.9, 2.9 to the first two elements of the array. The other two elements will be set to zero. Note that if an array is declared, but not initialized, all its elements will contain “garbage”, like all other local variables. double myList[4] = {1.9, 2.9};

16 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 16 Initializing arrays with random values The following loop initializes the array myList with random values between 0 and 99: for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = rand() % 100; }

17 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 17 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } Declare array variable values, create an array, and assign its reference to values animation

18 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 18 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i becomes 1 animation

19 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 19 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i (=1) is less than 5 animation

20 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 20 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this line is executed, value[1] is 1 animation

21 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 21 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After i++, i becomes 2 animation

22 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 22 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i (= 2) is less than 5 animation

23 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 23 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this line is executed, values[2] is 3 (2 + 1) animation

24 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 24 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this, i becomes 3. animation

25 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 25 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i (=3) is still less than 5. animation

26 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 26 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this line, values[3] becomes 6 (3 + 3) animation

27 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 27 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this, i becomes 4 animation

28 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 28 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i (=4) is still less than 5 animation

29 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 29 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this, values[4] becomes 10 (4 + 6) animation

30 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 30 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After i++, i becomes 5 animation

31 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 31 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } i ( =5) < 5 is false. Exit the loop animation

32 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 32 Trace Program with Arrays int main() { int values[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } After this line, values[0] is 11 (1 + 10) animation

33 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 33 Printing arrays To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < ARRAY_SIZE; i++) { cout << myList[i] << " "; }

34 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 34 Copying Arrays Can you copy array using a syntax like this? list = myList; This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < ARRAY_SIZE; i++) { list[i] = myList[i]; }

35 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 35 Summing All Elements Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: double total = 0; for (int i = 0; i < ARRAY_SIZE; i++) { total += myList[i]; }

36 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 36 Finding the Largest Element Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element in myList with max, update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; }

37 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 37 Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; }

38 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 38 Random Shuffling srand(time(0)); for (int i = 0; i < ARRAY_SIZE; i++) { // Generate an index randomly int index = rand() % ARRAY_SIZE; double temp = myList[i]; myList[i] = myList[index]; myList[index] = temp; }

39 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 39 Shifting Elements double temp = myList[0]; // Retain the first element // Shift elements left for (int i = 1; i < myList.length; i++) { myList[i - 1] = myList[i]; } // Move the first element to fill in the last position myList[myList.length - 1] = temp;

40 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 40 Problem: Lotto Numbers Your grandma likes to play the Pick-10 lotto. Each ticket has 10 unique numbers ranging from 1 to 99. Every time she buys a lot of tickets. She likes to have her tickets to cover all numbers from 1 to 99. Write a program that reads the ticket numbers from a file and checks whether all numbers are covered. Assume the last number in the file is 0. LottoNumbers Run

41 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 41 Problem: Deck of Cards The problem is to write a program that picks four cards randomly from a deck of 52 cards. All the cards can be represented using an array named deck, filled with initial values 0 to 52, as follows: int deck[52]; // Initialize cards for (int i = 0; i < NUMBER_OF_CARDS; i++) deck[i] = i;

42 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 42 Problem: Deck of Cards, cont. DeckOfCards Run

43 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 43 Passing Arrays to Functions Just as you can pass single values to a function, you can also pass an entire array to a function. Listing 7.3 gives an example to demonstrate how to declare and invoke this type of functions. PassArrayDemo Run

44 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 44 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. Otherwise, you will have to hard code this into the function or declare it in a global variable. Neither is flexible or robust.

45 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 45 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. EffectOfPassArrayDemo Run

46 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 46 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 Compile error

47 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 47 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++.

48 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 48 Modifying Arrays in Functions, cont. However, you can circumvent this restriction by passing two array arguments in the function, as follows: // newList is the reversal of list void reverse(const int list[], int newList[], int size) ReverseArray Run

49 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 49 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

50 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 50 Trace the reverse function, 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

51 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 51 Trace the reverse function, 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

52 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 52 Trace the reverse function, 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]

53 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 53 Trace the reverse function, 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

54 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 54 Trace the reverse function, 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

55 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 55 Trace the reverse function, 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]

56 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 56 Trace the reverse function, 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

57 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 57 Trace the reverse function, 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

58 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 58 Trace the reverse function, 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]

59 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 59 Trace the reverse function, 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

60 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 60 Trace the reverse function, 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

61 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 61 Trace the reverse function, 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]

62 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 62 Trace the reverse function, 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

63 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 63 Trace the reverse Function, 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

64 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 64 Trace the reverse Function, 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]

65 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 65 Trace the reverse Function, 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

66 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 66 Trace the reverse Function, 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

67 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 67 Trace the reverse Function, 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]

68 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 68 Trace the reverse Function, 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

69 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 69 Trace the reverse function, 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.

70 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 70 Problem: Counting Occurrence of Each Letter F Generate 100 lowercase letters randomly and assign to an array of characters. F Count the occurrence of each letter in the array. CountLettersInArrayRun

71 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 71 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 and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

72 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 72 Linear Search The linear search approach compares the key element, key, sequentially with each element in the array list. The Function 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.

73 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 73 Linear Search Animation 64197328 64197328 64197328 64197328 64197328 64197328 3 3 3 3 3 3 animation KeyList

74 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 74 http://www.cs.armstrong.edu/liang/animation/LinearSearc hAnimation.html Linear Search Animation animation

75 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 75 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

76 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 76 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.

77 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 77 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:

78 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 78 Binary Search 12346789 12346789 12346789 8 8 8 KeyList animation

79 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 79 http://www.cs.armstrong.edu/liang/animation/BinarySearc hAnimation.html Binary Search Animation animation

80 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 80 Binary Search, cont.

81 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 81 Binary Search, cont.

82 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 82 Binary Search, cont. The binarySearch Function returns the index of the search key if it is contained in the list. Otherwise, it returns –insertion point - 1. The insertion point is the point at which the key would be inserted into the list.

83 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 83 From Idea to Solution int binarySearch(const int list[], int key, int arraySize) { int low = 0; int high = arraySize - 1; while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return –low - 1; }

84 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 84 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 7.2, AssignGrade.cpp, in alphabetical order. Many different algorithms have been developed for sorting. This section introduces two simple, intuitive sorting algorithms: selection sort and insertion sort.

85 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 85 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. Selection Sort

86 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 86 http://www.cs.armstrong.edu/liang/animation/SelectionSo rtAnimation.html Selection Sort Animation animation

87 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 87 From Idea to Solution for (int i = 0; i < listSize; i++) { select the smallest element in list[i..listSize-1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration apply on list[i..listSize-1] } list[0] list[1] list[2] list[3]... list[10]... list[0] list[1] list[2] list[3]... list[10]

88 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 88 Expand for (int i = 0; i < listSize; i++) { select the smallest element in list[i..listSize-1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration apply on list[i..listSize-1] } double currentMin = list[i]; int currentMinIndex = i; for (int j = i; j < listSize; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; }

89 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 89 Expand for (int i = 0; i < listSize; i++) { select the smallest element in list[i..listSize-1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration apply on list[i..listSize-1] } double currentMin = list[i]; int currentMinIndex = i; for (int j = i; j < listSize; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; }

90 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 90 Expand for (int i = 0; i < listSize; i++) { select the smallest element in list[i..listSize-1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration apply on list[i..listSize-1] } if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; }

91 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 91 How to Insert? The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. Optional

92 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 92 Initializing Character Arrays char city[] = {'D', 'a', 'l', 'l', 'a', 's'}; char city[] = "Dallas"; This statement is equivalent to the preceding statement, except that C++ adds the character '\0', called the null terminator, to indicate the end of the string. Recall that a character that begins with the back slash symbol (\) is an escape character.

93 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 93 Reading C-Strings You can read a string from the keyboard using the cin object. For example, see the following code: char city[10]; cout << "Enter a city: "; cin >> city; // read to array city cout << "You entered " << city << endl;

94 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 94 Printing Character Array For a character array, it can be printed using one print statement. For example, the following code displays Dallas: char city[] = "Dallas"; cout << city;

95 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 95 Reading C-Strings Using getline C++ provides the cin.getline function in the iostream header file, which reads a string into an array. The syntax of the function is: cin.getline(char array[], int size, char delimitChar) The function stops reading characters when the delimiter character is encountered or when the size - 1 number of characters are read. The last character in the array is reserved for the null terminator ('\0'). If the delimiter is encountered, it is read, but not stored in the array. The third argument delimitChar has a default value ('\n').

96 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 96 C-String Functions

97 © Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 97 String Examples CopyString Run CombineString Run CompareStringRun StringConversion Run


Download ppt "© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings."

Similar presentations


Ads by Google