Arrays provide the programmer with a way of organizing a collection of homogeneous data items into a single data structure. - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Arrays provide the programmer with a way of organizing a collection of homogeneous data items into a single data structure.

Description:

... using a linear or binary search, ... The pseudocode algorithm for a linear search of an array will require a boolean flag named elementFound. – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 9
Provided by: NATE205
Category:

less

Transcript and Presenter's Notes

Title: Arrays provide the programmer with a way of organizing a collection of homogeneous data items into a single data structure.


1
Arrays
  • Arrays provide the programmer with a way of
    organizing a collection of homogeneous data items
    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 and name.
  • 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
    e.g., scores (3)

2
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
  • Usually, the elements of an array are processed
    in sequence, from first to last.

3
Writing Algorithms that Manipulate Arrays
  • Array algorithms are written using a DO or
    DOWHILE loop.
  • In each algorithm, the contents of the array and
    the number of elements have been established and
    the subscript is named index.
  • The number of elements in the array is stored in
    the variable numElements.

4
Initializing an Array
  • One common procedure is to read a fixed number of
    input values into the elements of an array from
    an input file.
  • Another procedure loads a variable number of
    values into an array. It must include a sentinel
    and a check to ensure that no attempt is made to
    load more entries into the array than the array
    can hold.

5
  • Initialize Fixed Array
  • Read numElements
  • DO index 1 TO numElements
  • READ inputValue
  • SET array(index) inputValue
  • ENDDO
  • END

6
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.
  • Continue the search until either you find the
    element being looked for or you reach the end of
    the array.
  • A linear search is often used to validate data
    items.
  • The pseudocode algorithm for a linear search of
    an array will require a boolean flag named
    elementFound. (See next slide for example.)

7
Example Calculate Freight Charge
  • Design an algorithm that will read an input
    weight for an item to be shipped, search an array
    of shipping weights, and retrieve a corresponding
    freight charge.
  • An array, shipping_weights, contains a range of
    shipping weights in ounces, and an array,
    freight_charges, contains a corresponding array
    of freight charges in dollars

Shipping Weight
Freight Charge
3.00
5.00
7.50
12.00
16.00
35.00
100 oz.
500 oz.
1000 oz.
3000 oz.
5000 oz.
9999 oz.
8
  • Calculate Freight Charges
  • Set numElements 6
  • SET index 1
  • SET elementFound false
  • PROMPT entryWeight
  • GET entryWeight
  • DOWHILE (NOT elementFound AND index lt
    numElements)
  • IF (shipping(index) lt entryWeight) THEN
  • SET index index 1
  • ELSE
  • SET elementFound true
  • ENDIF
  • ENDDO
  • IF (elementFound) THEN
  • charge freight(index)
  • DISPLAY charge
  • ELSE
  • DISPLAY Invalid Weight
  • ENDIF
Write a Comment
User Comments (0)
About PowerShow.com