1 / 40

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

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

Download Presentation

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

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. Programming in C++Dale/Weems/HeadingtonChapter 12Applied Arrays: Lists and Strings 1

  2. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ message [0] [1] [2] [3] [4] [5] [6] [7] 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” ; 2

  3. 5000 ‘A’ 6000 6001 ‘A’ ‘\0’ char vs. string ‘A’ has data type char and is stored in 1 byte “A” is a string of 2 characters and is stored in 2 bytes 3

  4. 6000 ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ message [0] [1] [2] [3] [4] [5] [6] [7] Recall that . . . char message[8]; // this declaration allocates memory To the compiler, the value of the identifier message aloneis the base address of the array. We say message is a pointer (because its value is an address).It “points” to a memory location. 4

  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 . . . 5

  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. 6

  7. Example using >> char name [ 5 ] ; cin >> name ; Suppose input stream looks like this: J o e total number of elements in the array 7000 ‘J’ ‘o’ ‘e’ ‘\0’ name [0] name [1] name [2] name [3] name [4] 7 null character is added

  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’ 8

  9. inFileStream.get ( str, count + 1) • get does notskip 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. 9

  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 ) ; 10

  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 ) ; ‘N’ ‘e’ ‘l’ ‘l’ ‘ ’ ‘D’ ‘a’ ‘l’ ‘e’ ‘\0’ . . . fullName [0] ‘A’ ‘u’ ‘s’ ‘t’ ‘i‘ ‘n’ ‘ ’ ‘T’ ‘X’ ‘\0’ . . . 11 address [0]

  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 12

  13. 5000 ‘C’ ‘h’ ‘i’ ‘p’ ‘ ’ ‘W’ ‘e’ ‘e’ ‘m’ ‘s’ ‘\0’ . . . . author [0] # include <string.h> . . . char author [ 21 ] ; int length ; cin.get ( author , 21 ) ; length = strlen ( author ) ; // What is the value of length ?

  14. 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 ; ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . . myName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . . yourName [0]

  15. char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; if ( myName == yourName ) // compares addresses only! {// That is, 4000 and 6000 here. . // DOES NOT COMPARE CONTENTS! . . } 4000 ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . . myName [0] 6000 ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . . yourName [0]

  16. char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; cin.get ( yourName, 21 ) ; yourName = myName; // DOES NOT COMPILE! // What is the value of myName ? 4000 ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . . myName [0] 6000 ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . . yourName [0]

  17. char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; cin.get ( yourName, 21 ) ; strcpy ( yourName, myName ) ; // changes string yourName // OVERWRITES CONTENTS! 4000 ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’ . . . myName [0] 6000 ‘u’ ‘n’ ‘g’ ‘\0’ ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’ . . . yourName [0]

  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 6000 7000 18

  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. 19

  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 Sonia 2460 14.97 Huang Jeff 20

  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’ 21

  22. int idNums [ MAX_PERSONS ] ; // parallel arraysfloat wages [ MAX_PERSONS ] ; String20 names [ MAX_PERSONS ] ; • 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” 22

  23. Using array of strings #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 & ) ; 23

  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 ; } 24

  25. Module Structure Chart Main idNums wages names numPersons idNums wages names numPersons GetData HandleRequests names oneName numPersons found index LookUp 25

  26. void GetData ( /* out */ int ids[ ] , /* out*/ float wages[ ] , /* out */ String20 names[ ] , /* out */ int & howMany ) { ifstream myInfile ; // Reads data from data file int 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; }

  27. 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’ ); }

  28. 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 flag else index ++ ; } }

  29. Some Questions Assume that numPersons has value 50. How many actual names in the array names must 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? 29

  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. 30

  31. Sorting • means arranging the list elements into some order (for instance, strings into alphabetical order, or numbers into ascending or descending order). Dale Nell Weems Chip Headington Mark Cooper Sonia Huang Jeff Cooper Sonia Dale Nell Headington Mark Huang Jeff Weems Chip sorting 31

  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. 32

  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 33

  34. 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 ) ; } }

  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. 35

  36. Heading for BinSearch function 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 36

  37. Body for BinSearch function { 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 ; } 37

  38. 15 26 38 57 62 78 84 91 108 119 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item < list [ middle ] last = middle - 1 item > list [ middle ] first = middle + 1 Trace of BinSearch function item = 45 first middle last 15 26 38 57 62 78 84 91 108 119 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first middle last 38

  39. 15 26 38 57 62 78 84 91 108 119 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last middle item > list [ middle ] first = middle + 1 15 26 38 57 62 78 84 91 108 119 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item < list [ middle ] last = middle - 1 Trace continued item = 45 first, middle, last 39

  40. 15 26 38 57 62 78 84 91 108 119 list[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] last < first found = false Trace concludes item = 45 last first 40

More Related