Engineering Problem Solving with C , Etter - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Engineering Problem Solving with C , Etter

Description:

... Second Edition, J. Ingber * Initializing Arrays Initializing ... These functions are defined in the header file: ... A string object can increase and decrease ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 30
Provided by: MarcI249
Learn more at: http://faculty.wiu.edu
Category:

less

Transcript and Presenter's Notes

Title: Engineering Problem Solving with C , Etter


1
Engineering Problem Solving with C, Etter
  • Chapter 6
  • One-Dimensional Arrays

2
One Dimensional Arrays
  • Arrays
  • Sorting Algorithms
  • Searching Algorithms
  • Character Strings
  • The string Class.

3
Arrays
  • Definition and Initialization
  • Computation and Output
  • Function Arguments

4
Definition
  • An array is a data structure for storing a
    contiguous block of data.
  • All data elements in an array must be of the
    same type.
  • Individual elements of the array are specified
    using the array name and an offset.
  • In C the offset of the first element is always
    0.

5
Definition and Initialization
  • Syntax
  • data_type identifiersize initialization
    list
  • Note size is an integer constant.
  • Example
  • double m8
  • m0 m1 m2 m3 m4 m5 m6
    m7

6
Initializing Arrays
  • Initializing array elements (initializationgtdecla
    ration)
  • char vowels5 'a', 'e', 'i', 'o', 'u'
  • bool ansKey true, true, false, true, false,
    false
  • char word "Hello"

vowels
ansKey
true
false
false
true
false
true
word
'H'
'e'
'l'
'l'
'o'
'\0'
7
Accessing Array Elements
  • Offsets are used to access individual elements of
    an array.
  • General format
  • array_identifieroffset
  • Example
  • for (int i0 ilt7 i)
  • mi double(i) 0.5
  • Integer expressions may be used as offsets.

8
Functions and arrays
  • An array identifier, without subscripts,
    references the starting address(first element) of
    the array.
  • In C, arrays are passed by reference. ie the
    starting address is passed, no size information.
  • Arrays in C to not know their size.
  • Generally we specify an additional parameter
    representing the number of elements in the array.

9
Example
  • include ltiostreamgt
  • using namespace std
  • const int MAXSIZE20
  • void ReadArr(double a, int count, istream
    in)
  • int FindMin(const double a, int count)
  • int main( )
  • double darrMAXSIZE
  • int cnt0, position0
  • ReadArr(darr, cnt, cin)
  • position FindMin(darr, cnt)
  • cout ltlt "The smallest value in the array is "
  • ltlt darrposition ltlt endl

10
  • // This function inputs values into an array
    until EOF or array
  • // limit reached
  • void ReadArray(double a, int count, istream
    in)
  • double temp
  • count 0
  • in gtgt temp
  • while ( (count lt MAXSIZE) !in.eof() )
  • acount temp
  • count
  • in gtgt temp

11
  • //This function returns the offset of the
    smallest
  • //value in an array
  • int FindMin(const double a, int size)
  • int offsetOfMin 0
  • for (int i1 iltsize i)
  • if (ai lt aoffsetOfMin )
  • offsetOfMin i
  • //end if
  • //end for
  • return offsetOfMin
  • //end FindMin

12
Sorting Algorithms
  • selection sort

13
Sorting Algorithms
  • Sorting algorithms arrange the data into either
    ascending or descending order, based on the
    values in the array.
  • Sorting algorithms to be discussed
  • Selection sort

14
Selection Sort Algorithm
  • Find minimum value, place it in the first
    position.
  • Find next minimum value, place it in the second
    position.
  • Continue doing this until you have placed the
    second to the largest value in the second to the
    last position.

15
Practice!
  • Fill in the following table to show how the array
    is sorted into ascending order using the
    selection sort.
  • arr0 arr1 arr2
    arr3 arr4

29 45 18 51 36
18 45 29 51 36



swap min and arr0
18 29 45 51 36
18 29 36 51 45
18 29 36 45 51
16
Search Algorithms
  • unordered lists
  • ordered lists

17
Searching Unordered Lists
  • Simple Sequential Search
  • Examine each element starting with the first one
    until
  • a match is found.
  • end of the list is reached.
  • Sequential search can be implemented as
  • a function which returns true if item in the
    list, false if item is not in the list.
  • a function which returns the location of the item
    if found, or 1 if not found.

18
Searching Ordered Lists
  • Modified Sequential Search
  • examine every element in the list until
  • item is found.
  • list element is larger/smaller than the item you
    are searching for (search fails).
  • Binary Search
  • Examine middle element
  • if element equals item, item found, return
    location.
  • if element is larger than item ignore bottom of
    list.
  • if element is smaller than item ignore top of
    list.
  • Repeat until
  • item is found.
  • top and bottom cross over (search failed).

19
Example of Binary Search for 48
70
59
56
43
37
28
22
14
11
5
arrtop
arrbot
arrmid
70
59
56
43
37
mid is 7
arrmid
arrbot
arrtop
mid is 5
arrtop
arrbot
43
mid is 6
arr6
43
arrtop
arrbot
20
Character Strings
  • C style strings
  • functions defined in cstring

21
C Style Character Strings
  • A C style strings is defined as a sequence of
    characters, terminated by the null character.
  • When declaring a character array to store a C
    style string, memory must be allocated for the
    null character ('\0').
  • Literal string constants are enclosed within
    double quote marks "a string".

22
C Style String Input
  • Recall that the input operator (gtgt) skips
    whitespace .
  • To input strings with embedded whitespace , the
    getline() function can be used as illustrated
  • char phraseSIZE
  • cin.getline(phrase, SIZE)
  • The getline() function reads up to SIZE-1
    characters from the input stream and will insert
    the null character.
  • getline() is a member function of what class?

23
C STYLE STRING FUNCTIONS
  • The Standard C library contains a set of
    predefined functions that operate on C style
    strings.
  • These functions are defined in the header
    file cstring
  • Commonly used string functions
  • strlen()
  • strcpy()
  • strcat()
  • strcmp()

24
Example C Style Strings
  • include ltiostreamgt
  • include ltcstringgt //strcmp(), strcpy(), strcat()
  • uses namespace std
  • int main()
  • char str130 "John", str230 "Johnson"
  • char phrase20 "'s shirt was green",
    sentence30
  • if (strcmp(str1,str2) lt 0)
  • strcpy (sentence, str1) // puts "John" into
    sentence
  • else
  • strcpy (sentence,str2) // puts "Johnson
    into sentence
  • strcat(sentence, phrase)
  • cout ltlt "Sentence is " ltlt sentence ltlt endl
  • return 0

25
The string class
  • functions defined in string

26
The string class
  • The string class implements the concept of a
    character string.
  • A string object can increase and decrease its
    size dynamically.
  • Numerous operators and functions are defined in
    the string class.

27
Common Functions Defined in string
  • size( )
  • empty( )
  • substr (int start, int len)
  • c_str()

28
Overloaded Operators Defined in string
  • relational operators
  • lt gt lt gt
  • concatenation
  • assignment

29
Example string class
  • include ltiostreamgt
  • include ltstringgt
  • uses namespace std
  • int main()
  • string str1 "John", str2 "Johnson"
  • string phrase "'s shirt was green", sentence
  • if (str1 lt str2)
  • sentence str1 // assign "John" to
    sentence
  • else
  • sentence str2 // assign "Johnson to
    sentence
  • sentence phrase // append phrase to sentence
  • cout ltlt "Sentence is " ltlt sentence ltlt endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com