1 cisc181 introduction to computer science dr. mccoy lecture 13 october 13, 2009

21
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009

Upload: hilary-gaines

Post on 14-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

  • *CISC181 Introduction to Computer Science

    Dr. McCoy

    Lecture 13October 13, 2009

  • Repeated SlidesSome of the following slides are repeated from lecture 8 character strings/character arrays*

  • *Storing Strings in Character ArraysCharacter arrays may be initialized using a string literal:Char name[ ] = Jane;Initializes the char array name to hold the individual characters J-a-n-e plus a special string termination character called the null character writte \0So, name is a 5 character array.

  • *Strings in Character ArraysAn alternative:char name[ ] = {J, a, n, e, \0};

    Common mistake when using a char array to hold strings, forgetting to leave the extra spot for the null termination character.

    2003 Prentice Hall, Inc. All rights reserved.

    *4.4Examples Using ArraysStrings (more in ch. 5) KFM note Ch 9 in SavitchArrays of charactersAll strings end with null ('\0')Exampleschar string1[] = "hello";Null character implicitly addedstring1 has 6 elements char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0 };Subscripting is the sameString1[ 0 ] is 'h'string1[ 2 ] is 'l'

  • *Reading Character StringsCharacter arrays input and output are straightforward. Can read a character string directly from the keyboard:char last_name[20];cin >> last_name;NOTE: no indication last_name is an array in the input statement!NOTE: the read-in string must be at most 19 characters long else will have problems!Character array output:cout > string2;Puts user input in stringStops at first whitespace characterAdds null characterIf too much text entered, data written beyond arrayWe want to avoid this (section 5.12 explains how)Printing stringscout
  • Some recursive functions with arraysFrom D&D 4.37 Printing a string backwards.Write a recursive function stringReverse that takes a character array containing a string as an argument, prints the string backwards, and returns nothing. The function should stop processing and return when the terminating null character is encountered.*

  • Palindromes (D&D 4.32)A pelindrome is a string that is spelled the same way forwards and backwards. Some examples of palindromes are radar, able was I ere I saw elba (if blanks are ignored), billib, and a man a plan a canal panama (if blanks are ignored).*

  • Write a recursive function testPalindrome that returns true if the string stored in the array is a palindrome, and false otherwise.The function should take 3 arguments a (const) character array, the index to the left end of the string, the index to the right end of the string.*

  • Can you write a main program that will enable this function to be used when the string read in actually contains spaces?*

  • BinarySearch D&D 4.34*

  • *4.8Searching Arrays: Linear Search and Binary SearchSearch array for a key valueLinear searchCompare each element of array with key valueStart at one end, go to otherUseful for small and unsorted arraysInefficientIf search key not present, examines every element

  • *4.8Searching Arrays: Linear Search and Binary SearchBinary searchOnly used with sorted arraysCompare middle element with keyIf equal, match foundIf key < middleRepeat search on first half of arrayIf key > middleRepeat search on last halfVery fast At most N steps, where 2 > # of elements30 element array takes at most 5 steps2 > 30

    N5

  • Arguments to Binary SearchThe (const) array to be searchedThe key (value) to look forLow_indexHigh_index

    *

  • ExampleA[0] = 1A[1] = 2A[2] = 4A[3] = 8A[4] = 16A[5] = 32A[6] = 64A[7] = 128A[8] = 256A[9] = 512*

  • SelectionSort D&D 4.31A selection sort searches an array looking for the smallest element in the array. Then, the smallest element is swapped with the first element of the array. The process is repeated for the subarray beginning with the second element of the array.*

  • Each pass of the array results in one element being placed in its proper location.

    void SelectionSort(int arr[], int size);// takes an integer array and its size// sorts the array using the selection// sort algorithm*

  • This sort performs comparably to the bubble sort for an array of n elements, n-1 passes must be made, and for each subarray, n-1 comparisons must be made to find the smallest value. When the subarray being processed contains one element, the array is sorted.*