Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009."— Presentation transcript:

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

2 Repeated Slides Some of the following slides are repeated from lecture 8 – character strings/character arrays 2

3 3 Storing Strings in Character Arrays Character 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 ‘\0’ So, name is a 5 character array.

4 4 Strings in Character Arrays An 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.

5  2003 Prentice Hall, Inc. All rights reserved. 5 4.4Examples Using Arrays Strings (more in ch. 5) KFM – note Ch 9 in Savitch –Arrays of characters –All strings end with null ( '\0' ) –Examples char string1[] = "hello"; –Null character implicitly added –string1 has 6 elements char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ }; –Subscripting is the same String1[ 0 ] is 'h' string1[ 2 ] is 'l'

6 6 Reading Character Strings Character 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 << last_name;

7  2003 Prentice Hall, Inc. All rights reserved. 7 4.4Examples Using Arrays Input from keyboard char string2[ 10 ]; cin >> string2; –Puts user input in string Stops at first whitespace character Adds null character –If too much text entered, data written beyond array We want to avoid this (section 5.12 explains how) Printing strings –cout << string2 << endl; Does not work for other array types –Characters printed until null found

8  2003 Prentice Hall, Inc. All rights reserved. Outline 8 fig04_12.cpp (1 of 2) 1 // Fig. 4_12: fig04_12.cpp 2 // Treating character arrays as strings. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 char string1[ 20 ], // reserves 20 characters 12 char string2[] = "string literal"; // reserves 15 characters 13 14 // read string from user into array string2 15 cout << "Enter the string \"hello there\": "; 16 cin >> string1; // reads "hello" [space terminates input] 17 18 // output strings 19 cout << "string1 is: " << string1 20 << "\nstring2 is: " << string2; 21 22 cout << "\nstring1 with spaces between characters is:\n"; 23 Two different ways to declare strings. string2 is initialized, and its size determined automatically. Examples of reading strings from the keyboard and printing them out.

9  2003 Prentice Hall, Inc. All rights reserved. Outline 9 fig04_12.cpp (2 of 2) fig04_12.cpp output (1 of 1) 24 // output characters until null character is reached 25 for ( int i = 0; string1[ i ] != '\0'; i++ ) 26 cout << string1[ i ] << ' '; 27 28 cin >> string1; // reads "there" 29 cout << "\nstring1 is: " << string1 << endl; 30 31 return 0; // indicates successful termination 32 33 } // end main Enter the string "hello there": hello there string1 is: hello string2 is: string literal string1 with spaces between characters is: h e l l o string1 is: there Can access the characters in a string using array notation. The loop ends when the null character is found.

10 Some recursive functions with arrays From 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. 10

11 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). 11

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

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

14 BinarySearch D&D 4.34 14

15 15 4.8Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search –Compare each element of array with key value Start at one end, go to other –Useful for small and unsorted arrays Inefficient If search key not present, examines every element

16 16 4.8Searching Arrays: Linear Search and Binary Search Binary search –Only used with sorted arrays –Compare middle element with key If equal, match found If key < middle –Repeat search on first half of array If key > middle –Repeat search on last half –Very fast At most N steps, where 2 > # of elements 30 element array takes at most 5 steps 2 > 30 N 5

17 Arguments to Binary Search The (const) array to be searched The key (value) to look for Low_index High_index 17

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

19 SelectionSort D&D 4.31 A 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. 19

20 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 20

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


Download ppt "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009."

Similar presentations


Ads by Google