programming in c++ dale/weems/headington chapter 12 applied arrays: lists and strings

40
1 Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays: Lists and Strings

Upload: jakeem-mooney

Post on 03-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays: Lists and Strings. 1. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’. message [0] [1] [2] [3] [4] [5] [6] [7]. String in C++. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

1

Programming in C++

Dale/Weems/HeadingtonChapter 12

Applied Arrays: Lists and Strings

Page 2: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

2

String in C++

A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end.

A string can be initialized in its declaration in two equivalent ways.

char message [ 8 ] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ };

char message [ 8 ] = “Hello” ;

message [0] [1] [2] [3] [4] [5] [6] [7]

‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

Page 3: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

3

char vs. string

‘A’ has data type charand is stored in 1 byte

“A” is a string of 2 characters

and is stored in 2 bytes

5000

‘A’

6000

‘A’

6001

‘\0’

Page 4: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

4

Recall that . . .

char message[8]; // this declaration allocates memory

To the compiler, the value of the identifier message alone is the base address of the array. We say message is a pointer (because its value is an address). It “points” to a memory location.

message [0] [1] [2] [3] [4] [5] [6] [7]

‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

6000

Page 5: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

5

Aggregate String I/O in C++

I/O of an entire string is possible using the array identifier with no subscripts and no looping.

EXAMPLE

char message [ 8 ] ; cin >> message ; cout << message ;

HOWEVER . . .

Page 6: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

6

Extraction operator >>

When using the extraction operator ( >> ) to read input characters into a string variable,

the >> operator skips any leading whitespace characters such as blanks and newlines.

It then reads successive characters into the array, and stops at the first trailing whitespace character (which is not consumed, but remains waiting in the input stream).

The >> operator adds the null character to the end of the string.

Page 7: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

7

Example using >>

char name [ 5 ] ;

cin >> name ;

Suppose input stream looks like this: J o e

name [0] name [1] name [2] name [3] name [4]

7000

total number of elements in the array

null character is added

‘J’ ‘o’ ‘e’ ‘\0’

Page 8: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

8

Function get( ) Because the extraction operator stops reading at the

first trailing whitespace, >> cannot be used to input a string with blanks in it.

If your string’s declared size is not large enough to hold the input characters and add the ‘\0’, the extraction operator stores characters into memory beyond the end of the array.

Use get function with 2 parameters to overcome these obstacles.

EXAMPLE

char message [ 8 ] ;

cin.get ( message, 8 ) ; // inputs at most 7 characters plus ‘\0’

Page 9: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

9

inFileStream.get ( str, count + 1)

get does not skip leading whitespace characters such as blanks and newlines.

get reads successive characters (including blanks) into the array, and stops when it either has read count characters, or it reaches the newline character ‘\n’, whichever comes first.

get appends the null character to str.

If it is reached, newline is not consumed by get, but remains waiting in the input stream.

Page 10: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

10

Function ignore( )

can be used to consume any remaining characters up to and including the newline ‘\n’ left in the input stream by get

EXAMPLE

cin.get ( string1, 81 ) ; // inputs at most 80 characters

cin.ignore ( 30, ‘\n’ ) ; // skips at most 30 characters // but stops if ‘\n’ is

read

cin.get ( string2, 81 ) ;

Page 11: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

11

Another example using get( )char ch ;

char fullName [ 31 ] ;

char address [ 31 ] ;

cout << “Enter your full name: “ ;

cin.get ( fullName, 31 ) ;

cin.get (ch) ; // to consume the newline

cout << “Enter your address: “ ;

cin.get ( address, 31 ) ;

fullName [0]

‘N’ ‘e’ ‘l’ ‘l’ ‘ ’ ‘D’ ‘a’ ‘l’ ‘e’ ‘\0’ . . .

address [0]

‘A’ ‘u’ ‘s’ ‘t’ ‘i‘ ‘n’ ‘ ’ ‘T’ ‘X’ ‘\0’ . . .

Page 12: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

12

String function prototypes in< string.h >

int strlen (char str [ ] ); // FCTNVAL == integer length of string str ( not including ‘\0’ )

int strcmp ( char str1 [ ], char str2 [ ] ); // FCTNVAL == negative, if str1 precedes str2 lexicographically

// == positive, if str1 follows str2 lexicographically

// == 0, if str1 and str2 characters same through ‘\0’

char * strcpy ( char toStr [ ], char fromStr [ ] );// FCTNVAL == base address of toStr ( usually ignored )

// POSTCONDITION : characters in string fromStr are copied to

// string toStr, up to and including ‘\0’,

// overwriting contents of string toStr

Page 13: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

# include <string.h> . . .

char author [ 21 ] ;

int length ;

cin.get ( author , 21 ) ;

length = strlen ( author ) ; // What is the value of length ?

5000

author [0]

‘C’ ‘h’ ‘i’ ‘p’ ‘ ’ ‘W’ ‘e’ ‘e’ ‘m’ ‘s’ ‘\0’ . . . .

Page 14: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

char myName [ 21 ] = “Huang” ; // WHAT IS OUTPUT?

char yourName [ 21 ] ;

cout << “Enter your last name : “ ;

cin.get ( yourName, 21 ) ;

if ( strcmp ( myName, yourName ) == 0 )

cout << “We have the same name! “ ;

else if ( strcmp ( myName, yourName ) < 0 )

cout << myName << “ comes before “ << yourName ;

else if ( strcmp ( myName, yourName ) > 0 )

cout << yourName << “comes before “ << myName ;

myName [0]

‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . .

yourName [0]

‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . .

Page 15: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

char myName [ 21 ] = “Huang” ;

char yourName [ 21 ] ;

if ( myName == yourName ) // compares addresses only!

{ // That is, 4000 and 6000 here.

. // DOES NOT COMPARE CONTENTS! . .

}

myName [0]

‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . .

yourName [0]

‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . .

4000

6000

Page 16: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

char myName [ 21 ] = “Huang” ;

char yourName [ 21 ] ;

cin.get ( yourName, 21 ) ;

yourName = myName; // DOES NOT COMPILE!

// What is the value of myName ?

myName [0]

‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . .

yourName [0]

4000

6000

‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . .

Page 17: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

char myName [ 21 ] = “Huang” ;

char yourName [ 21 ] ;

cin.get ( yourName, 21 ) ;

strcpy ( yourName, myName ) ; // changes string yourName

// OVERWRITES CONTENTS!

myName [0]

‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . .

yourName [0]

‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . .

4000

6000 ‘u’ ‘n’ ‘g’ ‘\0’

Page 18: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

18

Using typedef with arrays

typedef int Boolean ; // names Boolean as a data type

typedef char String20 [ 21 ] ; // names String20 as an array type

String20 myName ; // these declarations

String20 yourName ; // allocate memory for 3 variables

Boolean isSeniorCitizen ;

5000

7000

6000

Page 19: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

19

Write a program that will...

Read the ID numbers, hourly wages, and names, for up to 50 persons from a data file.

Then display the ID number and hourly wage for any person in the file whose name is entered at the keyboard, or indicate that the person was not located, if that is the case.

Page 20: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

20

Assume file has this form with data for no more than 50 persons

4562 19.68 Dale Nell 1235 15.75 Weems Chip 6278 12.71 Headington Mark . . . . . . . . .

8754 17.96 Cooper Sonia2460 14.97 Huang Jeff

Page 21: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

21

Parallel arrays hold related data

const int MAX_PERSONS = 50;

typedef char String20[ 21] ; // define data type

.

.

.

// declare 3 parallel arrays

int idNums[ MAX_PERSONS ] ;

float wages[ MAX_PERSONS ] ;

String20 names[ MAX_PERSONS ] ;

// holds up to 50 strings each with

// up to 20 characters plus null character ‘\0’

Page 22: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

22

idNums[ 0 ] 4562 wages[ 0 ] 19.68 names[ 0 ] “Dale Nell”

idNums[ 1 ] 1235 wages[ 1 ] 15.75 names[ 1 ] “Weems Chip”

idNums[ 2 ] 6278 wages[ 2 ] 12.71 names[ 2 ] “Headington Mark”

. . . . . . . . . . . . . . . . . .

idNums[ 48] 8754 wages[ 48] 17.96 names[ 48] “Cooper Sonia”

idNums[ 49] 2460 wages[ 49] 14.97 names[ 49] “Huang Jeff”

int idNums [ MAX_PERSONS ] ; // parallel arrays

float wages [ MAX_PERSONS ] ;

String20 names [ MAX_PERSONS ] ;

Page 23: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

23

#include < iomanip.h >#include < iostream.h >#include < fstream.h >#include < ctype.h >#include < string.h >#include “bool.h”

typedef char String20 [ 21 ] ;const int MAX_PERSONS = 50 ;

void GetData ( int [ ], float [ ], String20 [ ], int & ) ; // prototypes

void HandleRequests ( int [ ], float [ ], String20 [ ], int ) ;

void LookUp ( String20 [ ], String20, int, Boolean & , int & ) ;

Using array of strings

Page 24: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

24

Main Program

int main (void){ int idNums [MAX_PERSONS] ; // holds up to 50 IDs float wages [MAX_PERSONS] ; // holds up to 50 wages String20 names [MAX_PERSONS] ; // holds up to 50 names int numPersons; // number of persons’ information in file

GetData ( idNums, wages, names, numPersons ) ;

HandleRequests ( idNums, wages, names, numPersons ) ;

cout << “End of Program.\n”;

return 0 ;}

Page 25: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

25

Module Structure Chart

Main

GetData

LookUp

HandleRequests

names oneNamenumPersons

idNums wages names numPersons

idNums wages names numPersons

foundindex

Page 26: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

void GetData ( /* out */ int ids[ ] , /* out*/ float wages[ ] , /* out */ String20 names[ ] , /* out */ int & howMany )

{ ifstream myInfile ; // Reads data from data fileint k = 0 ;

char ch ;

myInfile.open (“A:\\my.dat”) ; if ( ! myInfile ) { cout << “File opening error. Program terminated! “ << endl ; exit ( 1 ) ; } myInfile >> ids[ k ] >> wages [k] ; // get information for first person myInfile.get(ch) ; // read blank myInfile.get (names[ k ] , 21) ; myInfile.ignore(30, ‘\n’) ; // consume newline

while (myInfile) // while the last read was successful { k++ ;

myInfile >> ids[ k ] >> wages [k] ; myInfile.get(ch) ; // read blank myInfile.get (names[ k ] , 21) ;

myInfile.ignore(30, ‘\n’) ; // consume newline } howMany = k;}

Page 27: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

void HandleRequests( const /* in */ int idNums[ ], const /* in */ float wages[ ] , const /* in */ String20 names[ ], /* in */ int numPersons )

{ String20 oneName ; // string to hold name of one person int index ; // will hold an array index value char response; // user’s response whether to continue Boolean found; // has oneName been located in array names

do { cout << “Enter name of person to find: ” ; cin.get (oneName, 21) ;

cin.ignore (100, ‘\n’); // consume newline

LookUp (names, oneName, numPersons, found, index );

if ( found ) cout << oneName << “ has ID # “ << idNums [index]

<< “ and hourly wage $ “ << wages [index] << endl;else cout << oneName << “ was not located. “ << endl;

cout << “Want to find another (Y/N)? “;cin >> response ;response = toupper ( response );

} while ( response == ‘Y’ );}

Page 28: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

void LookUp ( const /* in */ String20 names [ ], const /* in */ String20 oneName, /* in */ int numPersons, /* out */ Boolean & found , /* out */ int &

index)

// Sequential search of unordered array. // POSTCONDITION:// IF oneName is in names array// found == true && names[index] == oneName// ELSE// found == false && index == numPersons{

index = 0; found = false; // initialize flag while ( ( ! found ) && ( index < numPersons ) ) // more to search

{if ( strcmp ( oneName, names[index] ) == 0 ) // match here

found = true ; // change flagelse

index ++ ; }

}

Page 29: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

29

Some Questions

Assume that numPersons has value 50.

How many actual names in the array namesmust be examined before determining that oneName was not located?

Now suppose MAX_PERSONS is later changed to 1000.

How many actual names in the array names would need to be examined to determine that oneName cannot be located?

What are some ways to “speed up” this process?

Page 30: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

30

Ways to improve efficiency of searching process

If the array names were sorted, the sequential search for oneName could be aborted as soon as a single name with greater lexicographic (dictionary) order is examined.

If the array names were sorted, a faster type of search, called a binary search, could be used instead of the slower sequential search.

Page 31: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

31

Sorting

means arranging the list elements into some order (for instance, strings into alphabetical order, or numbers into ascending or descending order).

sorting

Dale NellWeems ChipHeadington MarkCooper SoniaHuang Jeff

Cooper Sonia Dale NellHeadington MarkHuang Jeff Weems Chip

Page 32: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

32

Selection Sort Process

examines the entire list to select the smallest element. Then places that element where it belongs (with array subscript 0).

examines the remaining list to select the smallest element from it. Then places that element where it belongs (with array subscript 1).

. . .

examines the last 2 remaining list elements to select the smallest one. Then places that element where it belongs in the array.

Page 33: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

33

Selection Sort Algorithm

FOR pass going from 0 through length - 2

Find minimum value in list [ pass . . length-1 ]

Swap minimum value with list [ pass ]

length = 5

names [ 0 ] Dale Nell Cooper Sonia names [ 1 ] Weems Chip Weems Chip names [ 2 ] Headington Mark Headington Mark names [ 3 ] Cooper Sonia Dale Nell names [ 4 ] Huang Jeff Huang Jeff

pass = 0

Page 34: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

void SelSort ( /* inout */ String20 names [ ] , /* in */ int length )

// Selection sorts names into alphabetic order// Preconditions: length <= MAX_PERSONS// && names [0 . . length -1 ] are assigned// Postcondition: names [ 0 . . length -1 ] are rearranged into order

{ int pass; int place; int minIndex; String20 temp; for ( pass = 0 ; pass < length - 1 ; pass++ )

{ minIndex = pass; for ( place = pass + 1 ; place < length ; place ++ ) if ( strcmp ( names [ place ] , names [ minIndex ] ) < 0 )

minIndex = place; //swap names[pass] with names[minIndex]

strcpy ( temp , names [ minIndex ] ) ; strcpy ( names [ minIndex ] , names [ pass] ) ; strcpy ( names [ pass ] , temp ) ; }}

Page 35: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

35

Binary Search in an Ordered List

Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array.

Repeat the process in the half of the list that should be examined next.

Stop when item is found, or when there is nowhere else to look and it has not been located.

Page 36: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

36

void BinSearch( /* in */ const ItemType list [ ] , /* in */ ItemType item, /* in */ int length, /* out */ int& index,

/* out */ Boolean & found )

// Searches list for item, returning index of item if found

// Precondition:// list [0 . . length - 1 ] are in ascending order // && length is assigned && item is assigned//// Postcondition:// IF item is in list// found == true && list[index] == item// ELSE// found == false && index is undefined

Heading for BinSearch function

Page 37: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

37

{ int first = 0 ; // lower bound on list int last = length - 1 ; // upper bound on list int middle ; // middle index

found = false ; while ( ( last >= first ) && (! found ) )

{ middle = ( first + last ) / 2 ;

if ( item < list [ middle ] )last = middle - 1 ; // look in first half next

else if ( item > list [ middle ] )first = middle + 1; // look in second half next

else found = true ; } index = middle ; }

Body for BinSearch function

Page 38: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

38

Trace of BinSearch function

list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

first middle last

list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first middle last

item < list [ middle ] last = middle - 1

item > list [ middle ] first = middle + 1

Page 39: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

39

Trace continued

list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

first, middle, last

list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first, last middle

item > list [ middle ] first = middle + 1

item < list [ middle ] last = middle - 1

Page 40: Programming in C++ Dale/Weems/Headington Chapter 12 Applied Arrays:  Lists and Strings

40

Trace concludes

list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

last first

last < first found = false