starting out with programming logic & design - chapter8_arrays

Upload: andrew-carts

Post on 06-Mar-2016

11 views

Category:

Documents


0 download

DESCRIPTION

Starting Out With Programming Logic & Design - Chapter8_Arrays

TRANSCRIPT

Chapter 8

Starting Out with Programming Logic & Design

Second Edition

by Tony GaddisChapter 8:ArraysCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley8-2Chapter Topics8.1 Array Basics8.2 Sequentially Searching an Array8.3 Processing the Contents of an Array8.4 Parallel Arrays8.5 Two-Dimensional Arrays8.6 Arrays of Three or More Dimension

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley28-38.1 Array BasicsAn array allows you to store a group of items of the same data type together in memoryWhy? Instead of creating multiple similar variables such as employee1, employee2, employee3 and so onIts more efficient to create just one variableDeclare String employees[50]Declare Real salesAmounts[7]The number in the [ ] is the size of the arrayCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley38-48.1 Array BasicsThe storage locations in an array are elementsEach element of the array has a unique number called a subscript that identifies it the subscript always starts at 0Figure 8-1 Array subscripts

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley48-58.1 Array BasicsAssigning values can be done individually using a subscriptSet numbers[0] = 20Set numbers[1] = 30Set numbers[2] = 40Set numbers[3] = 50Set numbers[4[ = 60But, it is much more efficient to use a Loop to step through the arrayCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley58-68.1 Array BasicsFigure 8-3 Contents of the hours array

Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley68-78.1 Array BasicsArrays can be initialized to 0 or specific valuesDeclare String days[7] = Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, SaturdayArray bounds checking should be performed to avoid use of an invalid subscriptDays[7] = Saturday is invalid because there is no 7 indexA common error is running a loop one time more than is necessary, exceeding the bound of the arrayOff-by-one ErrorCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley78.1 Array BasicsPartially Filled ArraySometimes an array is only partially filledTo avoid processing the unfilled elements, you must have an accompanying integer variable that holds the number of items stored in the array.When the array is empty, 0 is stored in this variableThe variable is incremented each time an item is added to the arrayThe variable's value is used as the array's size when stepping through the array.8-8Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley8Constant Integer SIZE = 100Declare Integer values[SIZE]Declare Integer count = 0Declare Integer numberDeclare Integer Index

Display "Enter a number, or -1 to quit."Input numberWhile (number != -1 AND count < SIZE) Set values[count] = number Set count = count + 1 Display "Enter a number, or -1 to quit." Input numberEnd While

Display "Here are the values you entered:"For index = 0 To count - 1 Display values[index]End For

8.1 Array Basics8-9Partially Filled Array ExampleThe count variable holds the number of items stored in the array.Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley98.1 Array BasicsOptional Topic: The For Each LoopSome languages provide a For Each loopIt works with an array, iterating once for each array elementDuring each iteration, the loop copies an element's value to a variable.8-10Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley108.1 Array Basics8-11Constant Integer SIZE = 5Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25Declare Integer num

For Each num In numbers Display numEnd For

For Each ExampleCopyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley118-128.2 Sequentially Searching an ArrayA sequential search algorithm is a simple technique for finding an item in a string or numeric arrayUses a loop to sequentially step through an arrayCompares each element with the value being searched for Stops when the value is found or the end of the array is hitSet found = FalseSet index = 0While found == False AND index