Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Lab 16Lab 16 Filling in an arrayFilling in an array Searching for a.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Lab 16Lab 16 Filling in an arrayFilling in an array Searching for a."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Lab 16Lab 16 Filling in an arrayFilling in an array Searching for a matchSearching for a match Methods to buildMethods to build

2 March 2005 2R. Smith - University of St Thomas - Minnesota Lab 16 A “phone number lookup” procedureA “phone number lookup” procedure –Take a file of names and phone numbers “phones.txt” –Read it into a too-large array, at least 20 element Mark the end with a sentinel valueMark the end with a sentinel value –Go into a loop looking up names/numbers First, give the user instructions on how to match and how to stop the programFirst, give the user instructions on how to match and how to stop the program Then run the loopThen run the loop –Prompt for a name to look up –Collect the name –Call a search procedure –Display the found result or say it wasn’t found –Continue until the user says to stop

3 March 2005 3R. Smith - University of St Thomas - Minnesota The “phones.txt” file Example contents: John Jones 623-6304 Sally Smith 344-435-3429 Molly McGuire 230-4034 Mary Robins 442-230-4023 First the name, then the number We try to match the start of the string

4 March 2005 4R. Smith - University of St Thomas - Minnesota Searching A “while” loopA “while” loop –Two conditions 1. Check for the ‘sentinel’1. Check for the ‘sentinel’ 2. Check for a match with the array entry2. Check for a match with the array entry If we’re looping, increment the loop indexIf we’re looping, increment the loop index –When done, return the correct result If a match, return the matched entryIf a match, return the matched entry If NOT a match, return an empty string “”If NOT a match, return an empty string “” Ignore caseIgnore case –Use toLowerCase() or toUpperCase() Only match the start of the stringOnly match the start of the string –Use the startsWith(str) method

5 March 2005 5R. Smith - University of St Thomas - Minnesota Methods to write readFile() methodreadFile() method –Read the file, create and fill in the array matchEntry() methodmatchEntry() method –Search for an entry whose start matches a string argument The main() methodThe main() method –Sets up the array –Loops looking for requested names –How do we ‘end it’? Pick a way

6 March 2005 6R. Smith - University of St Thomas - Minnesota Sportsmanship It’s you versus the computerIt’s you versus the computer Sometimes the computer ‘wins’Sometimes the computer ‘wins’ But you win in the long run if you keep at itBut you win in the long run if you keep at it Laughter – yes, the computer gets its tricks in, so we have to laugh and let it get its pointsLaughter – yes, the computer gets its tricks in, so we have to laugh and let it get its points But we’ll win in the endBut we’ll win in the end

7 March 2005 7R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Lab 16Lab 16 Filling in an arrayFilling in an array Searching for a."

Similar presentations


Ads by Google