Chapter 9 One-Dimensional Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 9 One-Dimensional Arrays

Description:

... Library, or STL, contains a class called vector that can be used to build and ... Returns, but does not remove, the last element of the vector. bool empty ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 28
Provided by: andrewst3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9 One-Dimensional Arrays


1
Chapter 9One-Dimensional Arrays
2
Chapter 9
  • An array is an indexed data structure that is
    used to store data elements of the same data
    type.

3
Chapter 9
  • A one-dimensional array, or list, is a sequential
    list of storage locations that contain data
    elements that are located via indices.

4
Chapter 9
  • One-Dimensional Array Definition Format
  • element data type array nameSIZE

5
Chapter 9
  • Inserting an Array Element Using Direct
    Assignment
  • array name index element value

6
Chapter 9
  • Inserting Array Elements Using a for Loop
  • for(int index0indexltarray size index)
  • assign or read to arrayindex

7
Chapter 9
  • Copying an Array Element Using Direct Assignment
  • variable array nameindex

8
Chapter 9
  • Displaying Array Elements Down the Screen Using
    a for Loop
  • for(int index0indexltarray size index)
  • cout ltlt arrayindex ltlt endl

9
Chapter 9
  • C-stings are stored in character arrays where the
    last element of the string is always the \0
    null terminator character.

10
Chapter 9
  • C-String Definition with a Specified Size
  • char variable identifiermaximum size of
    string 1 \0

11
Chapter 9
  • C-String Definition without a Specified Size
  • char variable identifier "string value"

12
Chapter 9
  • Reading C-Strings Using getline()
  • cin.getline(array ident., array size,
  • 'delimiting character')

13
Chapter 9
  • C-String Functions
  • strcat() string.h Appends one string to
    another.
  • strcmp() string.h Compares two strings.
  • strlen() string.h Returns length of a
    string.
  • strcpy() string.h Copies a string.

14
Chapter 9
  • Sequential SearchSet found false.
  • Set index first array index.
  • While (element is not found) AND (index lt last
    array index)
  • If (arrayindex element) Then
  • Set found true.
  • Else
  • Increment index.
  • If (found true)
  • Return index.
  • Else
  • Return 1.
  • .

15
Chapter 9
  • Insertion Sort
  • Set i second array index.
  • While (i lt last array index)
  • Set j i.
  • While ( (j gt first array index) AND
    (arrayj lt arrayj 1) )
  • Exchange arrayj and arrayj 1.
  • Decrement j.
  • Increment i.

16
Chapter 9
  • teleSearch()
  • BEGIN
  • If (the telephone book only contains one page)
  • Look for the name on the page.
  • Else
  • Open the book to the middle.
  • If (the name is in the first half)
  • teleSearch(first half of the book for the
    name).
  • Else
  • teleSearch(second half of the book for the
    name).
  • END.

17
Chapter 9
  • Recursive Binary Search (initial algorithm)
  • binSearch()
  • If (the array has only one element)
  • Determine if this the element.
  • Else
  • Find the midpoint of the array.
  • If (the element is in the first half)
  • binSearch(first half).
  • Else
  • binSearch(second half).

18
Chapter 9
  • Recursive Binary Search (final algorithm)If
    (first gt last)
  • Return 1.
  • Else
  • Set mid (first last) / 2.
  • If (arraymid element)
  • Return mid.
  • Else
  • If (the element is in the first half)
  • binSearch(array, element, first, mid 1).
  • Else
  • binSearch(array, element, mid 1, last).

19
Chapter 9
  • Method binSearch()Searches a sorted array of
    integers for a given value.
  • Accepts An array of integers, an element for
    which to search, the first index of the array
    being searched, and the last index of the array
    being searched.
  • Returns The array index of the element, if
    found, or the value 1 if the element is not
    found.
  • int binSearch(int array, int element, int
    first, int last)

20
Chapter 9
21
Chapter 9
  • int binSearch(int array , int element, int
    first, int last)
  • int mid //ARRAY MIDPOINT
  • if (first gt last) //IF ELEMENT NOT IN ARRAY
  • return -1 //RETURN -1, ELSE CONTINUE
  • else
  • mid (first last) / 2 //FIND
    MIDPOINT OF ARRAY
  • if (element arraymid) //IF ELEMENT IS
    IN ARRAYMID
  • return mid //RETURN MID
  • else //ELSE SEARCH APPROPRIATE HALF
  • if (element lt arraymid)
  • return binSearch(array, element, first, mid
    - 1)
  • else
  • return binSearch(array, element,
    mid 1, last)
  • //END OUTER ELSE
  • //END binSearch()

22
Chapter 9
  • The C Standard Template Library, or STL,
    contains a class called vector that can be used
    to build and manipulate one-dimensional arrays.

23
Chapter 9
  • Defining a Vector Example//INTEGER VECTOR, SIZE
    10, //INITIALIZED TO 0s
  • vectorltintgt v1(10,0)

24
Chapter 9
  • STL Vector Class Functions
  • void begin()
  • Places iterator at beginning of the vector.
  • vector data type back()
  • Returns, but does not remove, the last element
    of the vector.
  • bool empty()
  • Returns true if vector is empty.
  • void end()
  • Places iterator at end of the vector.
  • vector data type front()
  • Returns, but does not remove, the first element
    of the vector.
  • void pop_back()
  • Removes, but does not return, the last element
    of the vector.

25
Chapter 9
  • STL Vector Class Functions
  • void begin()
  • Places iterator at beginning of the vector.
  • vector data type back()
  • Returns, but does not remove, the last element
    of the vector.
  • bool empty()
  • Returns true if vector is empty.

26
Chapter 9
  • STL Vector Class Functions (continued)
  • void end()
  • Places iterator at end of the vector.
  • vector data type front()
  • Returns, but does not remove, the first element
    of the vector.
  • void pop_back()
  • Removes, but does not return, the last element
    of the vector.

27
Chapter 9
  • STL Vector Class Functions (continued)
  • void push_back(element)
  • Places element at the end of the vector.
  • int size()
  • Returns the number of elements in the vector.
Write a Comment
User Comments (0)
About PowerShow.com