Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.

Similar presentations


Presentation on theme: "An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays."— Presentation transcript:

1 An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays

2 An Object-Oriented Approach to Programming Logic and Design 2 Objectives Understand arrays and how they occupy computer memory Manipulate an array to replace nested decisions Declare and initialize an array Understand the difference between initialized and uninitialized arrays Load array values from a file

3 An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Search an array for an exact match Use a constant to store an array size Search an array for a range match

4 An Object-Oriented Approach to Programming Logic and Design 4 Understanding Arrays and How They Occupy Memory Array: a series or list of variables in memory that have the same name and type but different subscripts (or indexes) Subscript (or index): a number that indicates the position of an item within the array Example: mailboxes in an apartment building – all have the same street address (name), but each has a different apartment number (subscript)

5 An Object-Oriented Approach to Programming Logic and Design 5 How Arrays Occupy Computer Memory Element: one item in the array; has a common name and a unique subscript, and stores a value Size of the array: the number of elements in the array Subscripts start at 0 An array’s elements are stored contiguously in memory

6 An Object-Oriented Approach to Programming Logic and Design 6 How Arrays Occupy Computer Memory (continued)

7 An Object-Oriented Approach to Programming Logic and Design 7 Manipulating an Array to Replace Nested Decisions

8 An Object-Oriented Approach to Programming Logic and Design 8 Manipulating an Array to Replace Nested Decisions (continued)

9 An Object-Oriented Approach to Programming Logic and Design 9 Manipulating an Array to Replace Nested Decisions (continued) Using an array can greatly reduce the amount of code required Subscript can be represented by a constant or a variable Using a variable for the subscript allows the use of a loop to cycle through all elements in an array

10 An Object-Oriented Approach to Programming Logic and Design 10 Manipulating an Array to Replace Nested Decisions (continued)

11 An Object-Oriented Approach to Programming Logic and Design 11 Manipulating an Array to Replace Nested Decisions (continued)

12 An Object-Oriented Approach to Programming Logic and Design 12 Manipulating an Array to Replace Nested Decisions (continued) Because the action taken was the same for each element of the array, the code can be significantly shortened Using a variable for the subscript replaces the need for decision structures in this example

13 An Object-Oriented Approach to Programming Logic and Design 13 Manipulating an Array to Replace Nested Decisions (continued)

14 An Object-Oriented Approach to Programming Logic and Design 14 Manipulating an Array to Replace Nested Decisions (continued)

15 An Object-Oriented Approach to Programming Logic and Design 15 Manipulating an Array to Replace Nested Decisions (continued) Subscript –Can have any legal name –Must be numeric with no decimal places –Must be initialized to 0 –Must be incremented by 1 each time the logic passes through a loop Arrays make code easier to read

16 An Object-Oriented Approach to Programming Logic and Design 16 Array Declaration and Initialization Declaring an array requires the type, the name, and the number of elements Example: numeric count[20] Individual array elements must be initialized If elements will contain different values, initialize each element separately Some languages allow declaration and initialization in a single statement Example: numeric count[0] = 5

17 An Object-Oriented Approach to Programming Logic and Design 17 Array Declaration and Initialization (continued ) If all elements in an array will have the same initial value, use a loop Providing values for an array is called populating the array

18 An Object-Oriented Approach to Programming Logic and Design 18 Initialized and Uninitialized Arrays Uninitialized array: does not receive values for the elements until run time Initialized array: values for the elements are “hard-coded” by the programmer An array can be thought of as a table

19 An Object-Oriented Approach to Programming Logic and Design 19 Initialized and Uninitialized Arrays (continued)

20 An Object-Oriented Approach to Programming Logic and Design 20 Loading an Array from a File Hard-coded values are inconvenient – only the programmer can change them Initializing an array by reading values from a file makes the program more flexible

21 An Object-Oriented Approach to Programming Logic and Design 21 Loading an Array from a File (continued)

22 An Object-Oriented Approach to Programming Logic and Design 22 Searching an Array for an Exact Match Use a loop to search through an array, element by element The loop control variable can be used as the array subscript variable Flag: a variable used to indicate if an event has occurred Start with flag = “No”; set it to “Yes” if you find the desired value in an array element

23 An Object-Oriented Approach to Programming Logic and Design 23 Searching an Array for an Exact Match (continued)

24 An Object-Oriented Approach to Programming Logic and Design 24 Using Parallel Arrays Parallel arrays: two arrays in which each element in one array is associated with the element in the same relative position in the other array Example: one array holds product numbers, the other holds the prices for each product

25 An Object-Oriented Approach to Programming Logic and Design 25 Using Parallel Arrays (continued)

26 An Object-Oriented Approach to Programming Logic and Design 26 Improving Search Efficiency Using an Early Exit When using a loop to search an array, exit the loop if the item is found in an array element Early Exit: leaving a loop when a match is found

27 An Object-Oriented Approach to Programming Logic and Design 27 Improving Search Efficiency Using an Early Exit (continued)

28 An Object-Oriented Approach to Programming Logic and Design 28 Using a Constant to Store an Array Size Instead of hard-coding the array size, use a named constant or variable Advantage: only the variable or constant needs to be changed to change the array size Named constant identifiers should be written in all caps Example: NUM_PRODUCTS

29 An Object-Oriented Approach to Programming Logic and Design 29 Using a Constant to Store an Array Size (continued)

30 An Object-Oriented Approach to Programming Logic and Design 30 Searching an Array for a Range Match Range match: finding a value within a contiguous range of values Example: range is 0 through 8

31 An Object-Oriented Approach to Programming Logic and Design 31 Searching an Array for a Range Match (continued)

32 An Object-Oriented Approach to Programming Logic and Design 32 Searching an Array for a Range Match (continued)

33 An Object-Oriented Approach to Programming Logic and Design 33 Summary Array: a series of variables in memory which all have the same name but different subscripts Each array element has a unique subscript to identify it from other array elements Subscript (or index): a number indicating the position of the element within the array Use a variable for the subscript to allow more programmatic control of the array

34 An Object-Oriented Approach to Programming Logic and Design 34 Summary (continued) Array element values can be hard-coded, or assigned at run time Array element values can be loaded from a file Searching an array involves examining each element –Loop through all elements –Use a flag to indicate when the matching value is found Flag: a variable that indicates if an event has occurred

35 An Object-Oriented Approach to Programming Logic and Design 35 Summary (continued) Parallel arrays are two arrays in which each element of one array is related to the element in the same subscript position in the other array When comparing a range of values, use either the low-end or high-end values of each range


Download ppt "An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays."

Similar presentations


Ads by Google