1 / 28

Array Processing

7. Array Processing. Simple Program Design Third Edition A Step-by-Step Approach. 7. Objectives. To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To illustrate the manipulation of single and two dimensional arrays. 7.

Mercy
Télécharger la présentation

Array Processing

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. 7 Array Processing Simple Program Design Third Edition A Step-by-Step Approach

  2. 7 Objectives • To introduce arrays and the uses of arrays • To develop pseudocode algorithms for common operations on arrays • To illustrate the manipulation of single and two dimensional arrays

  3. 7 Array Processing • Arrays are one of the most powerful programming tools available • They provide the programmer with a way of organizing a collection of homogeneous data items (i.e. items that have the same type and same length) into a single data structure • An array is a data structure that is made up of a number of variables all of which have the same data type

  4. 7 Array Processing • The individual data items that make up the array are referred to as the elements of the array • Elements in the array are distinguished from one another by the use of an index or subscript, enclosed in parentheses, following the array name (such as ‘scores(3)’) • The subscript indicates the position of an element within the array • Arrays are an internal data structure, they are required only for the duration of the program in which they are defined

  5. 7 Operations on Arrays • Arrays can be used… • to load initial information into an array, which is later required for processing • to process the elements of the array • to store the results of processing into elements of an array • to store information, which is then written to a report

  6. 7 Operations on Arrays • The most typical operations performed on arrays are: • Loading a set of initial values into the elements of an array • Processing the elements of an array • Searching an array, using a linear or binary search, for a particular element • Writing out the contents of an array to a report • The elements of an array are processed in sequence, starting with the first element

  7. 7 Simple Algorithms That Manipulate Arrays • The following algorithms involve the simple manipulation of arrays • In each algorithm, the contents of the array and the number of elements have been established and the subscript is named index

  8. 7 Example 7.1 Find the Sum of the Elements of an Array • In this example, each element of the array is accumulated into a variable called sum • When all elements have been added, the variable sum is printed Find_sum_of_elements (Using DO loop) Set sum to zero DO index = 1 to number_of_elements sum = sum + array (index) ENDDO Print sum END

  9. 7 Example 7.1 Find the Sum of the Elements of an Array Find_sum_of_elements (Using DOWHILE loop) Set sum to zero Set index to 1 DOWHILE index <= number_of_elements sum = sum + array (index) index = index + 1 ENDDO Print sum END

  10. 7 Initializing the Elements of an Array • Because an array is an internal data structure, initial values must be placed into the array before any information can be retrieved from it

  11. 7 Loading Constant Values into an Array • This method should only be used when the data in the array is unlikely to be changed Initialize_month_table month_table(1) = ‘January’ month_table(2) = ‘February’ : : : month_table(12) = ‘December’ END

  12. 7 Loading Initial Values into an Array from an Input File • Defining array elements as constants in a program is not recommended if the values change frequently, as the program will need to be changed every time an array element changes • A general procedure is required to read values into the elements of an array from an input file • The reading of a series of values from a file into an array can be represented by a simple DOWHILE loop

  13. 7 Arrays of Variable Size • The number of entries in an array can vary • A sentinel value (e.g. 9999) is used to mark the last element of the array, both in the initializing file of data items and in the array itself • The sentinel record will indicate the end of input records during initial processing and the last element of the array during further processing • The processing will terminate when either the sentinel record has been read or the array is full

  14. 7 Paired Arrays • Many arrays in business applications are paired; that is, there are two arrays that have the same number of elements • The arrays are paired because the elements in the first array correspond to the elements in the same position in the second array • A sales number array can be paired with a sales name array

  15. 7 Searching an Array • The reasons for searching an array may be: • To edit an input value, to check that it is a valid element of an array • To retrieve information from an array • To retrieve information from a corresponding element in a paired array • When searching an array, it is an advantage to have the array sorted into ascending sequence

  16. 7 A Linear Search of an Array • A linear search involves looking at each of the elements of the array, one by one, starting with the first element • The pseudocode algorithm for a linear search of an array will require a program flag named element_found • This flag, initially set to false, will be set to true once the value being looked for is found

  17. 7 A Binary Search of an Array • When the number of elements in an array exceeds 25, and the elements are sorted into ascending sequence, a more efficient method of searching the array is a binary search • A binary search locates the middle element of the array first, and determines if the element being searched for is in the first half or second half of the table

  18. 7 A Binary Search of an Array • A search then points to the middle element of the relevant half table, and the comparison is repeated • The binary search will continue until the data item has been found or there can be no more halving operations (low_element is not less than high_element)

  19. 7 Writing Out the Contents of an Array • The elements of arrays are often used as a series of accumulators of data, to be written to a report • Writing out the contents of an array involves starting with the first element of the array and continuing until all elements have been written • This can be represented by a simple DO loop or a DOWHILE loop

  20. 7 Programming Examples Using Arrays • Example 7.6 Process_exam_scores • Design a program that will prompt for and receive 18 examination scores from a mathematics test, compute the class average, and display all the scores and the class average to the screen • A Defining diagram (see figure on page 92 of the textbook)

  21. 7 Programming Examples Using Arrays • B Control structures required • The programmer will need to consider the following requirements: • An array to store the exam scores (i.e. ‘scores’) • An index to identify each element in the array • One DO loop to accept the scores • Another DO loop to display the scores to the screen • C Solution algorithm • Refer to the code printed on page 92 of the textbook

  22. 7 Two-Dimensional Arrays • All algorithms in this chapter have manipulated one-dimensional arrays; that is, only one subscript is needed to locate an element in an array • In some business applications there is a need for multidimensional arrays, where two or more subscripts are required to locate an element in an array • The following freight charges array is an example of a two-dimensional array, and is an extension of Example 7.9

  23. 7 Two-Dimensional Arrays • The range of shipping weights, in grams, is provided in the same one-dimensional array as in Example 7.9 • The number of elements in a two-dimensional array is calculated as the product of the number of rows and the number of columns; in this case, 6 x 4 = 24

  24. 7 Two-Dimensional Arrays • An element of a two-dimensional array is specified using the name of the array followed by two subscripts, enclosed in parentheses, and separated by a comma • The row subscript is specified first, followed by the column subscript

  25. 7 Loading a Two-Dimensional Array • A two-dimensional array is loaded in columns within row order, that is all the columns for row one are loaded before moving to row two and loading the columns for that row, and so on • In the following pseudocode algorithm, values are read from an input file of freight charges and assigned to a two-dimensional freight_charges array • The reading of a series of values from a file into a two-dimensional array can be represented by a DO loop (or DOWHILE loop) within a DOWHILE loop, as shown on page 97

  26. 7 Searching a Two-Dimensional Array • Search Method 1 • In the following pseudocode algorithm, the freight charges for an article are to be calculated by searching a previously initialized two-dimensional array (refer to the example on page 98 of the textbook) • Search Method 2 • In the following algorithm, an input employee number is validated against a two-dimensional array of employee numbers, which has ten rows and five columns • The array is searched sequentially, as shown at the bottom of page 98 in the textbook

  27. 7 Writing Out the Contents of a Two-Dimensional Array • To write out the contents of a two-dimensional array, write out the elements in the columns within a row, before moving on to the next row • This is represented in pseudocode by a DO loop within another DO loop

  28. 7 Summary • This chapter defined an array as a data structure made up of a number of variables or data items that all have the same data type and are accessed by the same name • Algorithms were developed for the most common operations on arrays, namely: • Loading a set of initial values into the elements of an array • Processing the elements of an array • Searching an array, using a linear or binary search, for a particular element • Writing out the contents of an array to a report

More Related