Chapter 9 Data Structures Arrays Problem Solving Abstraction

Chapter 9: Data Structures: Arrays Problem Solving, Abstraction, and Design using C++ 6 e by Frank L. Friedman and Elliot B. Koffman Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

9. 1 The Array Data Type • Array elements have a common name – The array as a whole is referenced through the common name • Array elements are of the same type — the base type • Individual elements of the array are referenced by sub-scripting the group name – element’s relative position used, beginning with 0 • Array stored in consecutive memory locations Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2
![Additional Array Details • Subscripts are denoted as expressions within brackets: [ ] • Additional Array Details • Subscripts are denoted as expressions within brackets: [ ] •](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-3.jpg)
Additional Array Details • Subscripts are denoted as expressions within brackets: [ ] • Base type can be any fundamental, librarydefined, or programmer -defined type • The index type is integer and the index range must be 0. . . n-1, for array of size n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3
![Array Declaration element-type array-name [array-size]; Type of all the values in the array Name Array Declaration element-type array-name [array-size]; Type of all the values in the array Name](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-4.jpg)
Array Declaration element-type array-name [array-size]; Type of all the values in the array Name of the entire collection of values Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Integer expression indicating number of elements in the array 4
![Example 1 element-type array-name [array-size] = {initialization-list}; float x[8] = {16. 0, 12. 0, Example 1 element-type array-name [array-size] = {initialization-list}; float x[8] = {16. 0, 12. 0,](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-5.jpg)
Example 1 element-type array-name [array-size] = {initialization-list}; float x[8] = {16. 0, 12. 0, 6. 0, 8. 0, 2. 5, 12. 0, 14. 0, -54. 5}; 16. 0 12. 0 6. 0 8. 0 2. 5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12. 0 14. 0 -54. 5 5
![Example 1 (con’t) cout << x[0]; x[3] = 25. 0; sum = x[0] + Example 1 (con’t) cout << x[0]; x[3] = 25. 0; sum = x[0] +](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-6.jpg)
Example 1 (con’t) cout << x[0]; x[3] = 25. 0; sum = x[0] + x[1]; sum += x[2]; x[3] += 1. 0; x[2] = x[0] + x[1]; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6
![Example 2 const int NUM_EMP = 10; bool on. Vacation[NUM_EMP]; int vacation. Days[NUM_EMP]; enum Example 2 const int NUM_EMP = 10; bool on. Vacation[NUM_EMP]; int vacation. Days[NUM_EMP]; enum](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-7.jpg)
Example 2 const int NUM_EMP = 10; bool on. Vacation[NUM_EMP]; int vacation. Days[NUM_EMP]; enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; day. Off[NUM_EMP]; float plant. Hours[7]; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7

Figure 9. 3 Arrays on. Vacation, vacation. Days, and day. Off Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8

Array Initialization • List of initial values enclosed in braces ({ }) following assignment operator (=) • Values from initialization list are assigned in order to array elements • Length of initialization list cannot exceed size of the array • If too few values, value assigned is system dependent • Size of array can be automatically set to number of initializing values using empty brackets ([ ]) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9
![Array Subscripts • Enclosed in brackets ([ ]) • Indicates which element is referenced Array Subscripts • Enclosed in brackets ([ ]) • Indicates which element is referenced](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-10.jpg)
Array Subscripts • Enclosed in brackets ([ ]) • Indicates which element is referenced by position • Array subscript value is different than array element value • Subscript can be an expression of any integral type • To be valid, subscript must be a value between 0 and one less than the array size Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10

9. 2 Sequential Access to Array Elements • Random Access – Access elements is any order • Sequential Access – Process elements in sequential order starting with the first Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11
![Example of Sequential Access int cube[10]; for (int i = 0; i < 10; Example of Sequential Access int cube[10]; for (int i = 0; i < 10;](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-12.jpg)
Example of Sequential Access int cube[10]; for (int i = 0; i < 10; i++) cube[i] = i * i; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12

Strings and Arrays of Characters • string object uses an array of char • Can reference individual character of a string object in different ways – name[ i ] – name. at( i ) • Other member functions of string class – message. length( i ) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13

9. 3 Array Arguments • Use <, ==, >, +, -, etc. to test and modify array elements individually • Can pass array elements to functions exchange (s[3], s[5]); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14

Listing 9. 3 Function to exchange the contents of two floating-point memory locations Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15

Passing an Array Argument • Arrays are always passed by reference • Pass entire array to a function by writing just its name (no subscripts or brackets) in the argument list of the function call • In function definition and prototype, user empty brackets ([ ]) to identify array • Use keyword const to indicate that array argument cannot be changed by function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16
![Example 1 const int MAX_SIZE = 5; float x[MAX_SIZE ]; float y[MAX_SIZE ]; . Example 1 const int MAX_SIZE = 5; float x[MAX_SIZE ]; float y[MAX_SIZE ]; .](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-17.jpg)
Example 1 const int MAX_SIZE = 5; float x[MAX_SIZE ]; float y[MAX_SIZE ]; . . . if (same. Array(x, y, MAX_SIZE)) cout << “Arrays are identical. ” << endl; else cout << “Arrays are different. ” << endl; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17

Listing 9. 4 Function same. Array Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18
![Example 2 const int MAX_SIZE = 5; float x[MAX_SIZE ] = {1. 8, 2. Example 2 const int MAX_SIZE = 5; float x[MAX_SIZE ] = {1. 8, 2.](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-19.jpg)
Example 2 const int MAX_SIZE = 5; float x[MAX_SIZE ] = {1. 8, 2. 2, 3. 4, 5. 1, 6. 7}; float y[MAX_SIZE ] = {2. 0, 4. 5, 1. 3, 4. 0, 5. 5}; float z[MAX_SIZE]; . . . add. Array(MAX_SIZE, x, y, z); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19

Listing 9. 5 Function add. Array // File: add. Array. cpp // Stores the sum of a[i] and b[i] in c[i] // Sums pairs of array elements with subscripts ranging from 0 // to size – 1 // Pre: a[i] and b[i] are defined (0 <= i <= size-1) // Post: c[i] = a[i] + b[i] (0 <= i <= size-1) void add. Array (int size, // IN: the size of the arrays const float a[], // IN: the first array const float b[], // IN: the second array float c[]) // OUT: result array { // Add corresponding elements of a and b and store in c for (int i = 0; i < size; i++) c[i] = a[i] + c[i]; } Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20

9. 4 Reading Part of an Array • Sometimes it is difficult to know how many elements will be in an array – 150 students in one section – 200 students in another section • Always allocate enough space for largest possible amount needed • Remember to start reading with index [0] • Must keep track of how many elements used Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21

Listing 9. 7 Function read. Scores. File Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22

Listing 9. 7 Function read. Scores. File (continued) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23

9. 5 Searching and Sorting Arrays • Two common array processing problems – Searching – Sorting • E. g. – look for a particular score, highest score, etc. – rearrange an array of scores in increasing order Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24

Finding the Smallest Value 1. Assume the first element is smallest so far and save its subscript 2. For each array element after the first one 2. 1 If the current element < the smallest so far 2. 1. 1 Save the subscript of current element Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25

Listing 9. 8 Function find. Index. Of. Min Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26

Listing 9. 8 Function find. Index. Of. Min (continued) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27
![Array Search - Interface • Input arguments – int items[ ] – int size Array Search - Interface • Input arguments – int items[ ] – int size](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-28.jpg)
Array Search - Interface • Input arguments – int items[ ] – int size – int target // array to search // number of items in array // item to find • Output arguments – none • Returns – if found, subscript of first location in array – if not found, -1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28

Array Search - Algorithm 1. For each array element 1. 1 If the current element contains the target 1. 2 Return the subscript of the current element 2. Return -1. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29

Listing 9. 9 The function lin. Search Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 30

Sorting an Array in Ascending Order • Many programs execute more efficiently if data is in order before processing starts • Possible to order in either ascending or descending arrangement • Selection sort just one of many ways to do this – reuses previous components of search and swap operations Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 31
![Selection Sort • Input arguments float items[ ] int n // array to sort Selection Sort • Input arguments float items[ ] int n // array to sort](http://slidetodoc.com/presentation_image_h2/b0ca1a7bbf5033aee0dedfc24a13626b/image-32.jpg)
Selection Sort • Input arguments float items[ ] int n // array to sort // number of items to sort • Output arguments int items [ ] // original array sorted • Local variables int i int min. Sub // subscript of first element // subscript of smallest item Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32

Selection Sort - Algorithm 1. Starting with the first item in the array (subscript 0) and ending with the next-to-last-item: 1. 1 Set i equal to the subscript of the first item in the subarray to be processed in the next steps 1. 2 Find the subscript (min. Sub) of the smallest item in the subarray with subscripts ranging from i through n-1 1. 3 Exchange the smallest item found in step 1. 2 with item i Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33

Listing 9. 10 Function sel. Sort Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 34

9. 13 Common Programming Errors • Arrays – – Out-of-range subscript references Unsubscripted array references Subscripted references to nonarray variables Mixing types in passing arrays to functions Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35
- Slides: 35