1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.

Slides:



Advertisements
Similar presentations
1 Arrays … The Sequel Applications and Extensions Chapter 10.
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 10.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Searching and Sorting Arrays
1 Techniques of Programming CSCI 131 Lecture 29 Search.
1 Applied Arrays: Lists and Strings. 2 Chapter 13 Topics  Meaning of a List  Insertion and Deletion of List Elements  Selection Sort of List Elements.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 14: Searching and Sorting
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Data Structures Arrays and Lists Part 2 More List Operations.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Chapter 13 Applied Arrays: Lists and Strings
Arrays … The Sequel Applications and Extensions
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.
Arrays and Strings Chapter 9.
Search,Sort,Recursion.
Engineering Problem Solving with C++, Etter
Search,Sort,Recursion.
Searching and Sorting Arrays
Chapter 13 Applied Arrays: Lists and Strings
Arrays.
Presentation transcript:

1 Applied Arrays Lists and Strings Chapter 12

2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as "STRINGS" strings … get it? hahahaha

3 Lists Defn => A collection of homogeneous components –linear collection –variable length collection Length the actual number of values stored in the list Example -- a file of time card information Joe, 40, Clyde, 38.5, Sniudly,

4 Lists Arrays can be used to implement a list –declare the array large –keep track of how many elements used We often do operations on the lists –create a list, add an item, delete an item –print the list, search the list for a value –sort the list A list of numbers scores :

5 Sequential Search Consider the list unordered (not sorted) For a function to search for a targert we must specify –name of the array to be searched –length of the list (number of array elements) –a flag parameter which tells whether or not the search was successful –an index value to be returned which tells where in the list the item was found scores scores : boolean & found int & location

6 Sequential Search Algorithm example Note use of reference parameters for the found flag and the location void search (int list[ ], int length, int target, boolean & found, int &location) { location = 0; while ((location < length) && (target != list[location])) location++; found = (index < length); } // if found == TRUE, location OK... search (scores, 5, 92, found_it, where_its_at); scores :

7 Sorted List -- Faster Search Sorted list => components arranged in order –alphabetical –numerically ascending or descending Advantage of a sorted list –need to search only until the value found is larger than target value

8 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

9 Sorting Algorithm Make a pass through the list, look for smallest number list 1 : list 2 :

10 Sorting Algorithm Make a pass through the list, look for smallest number Write that number in another column, cross it off first list list 1 : list 2 : 57

11 Sorting Algorithm Make a pass through the list, look for smallest number Write that number in another column, cross it off first list Repeat process, always look for smallest number remaining list 1 : list 2 : 57 68

12 Sorting Algorithm Make a pass through the list, look for smallest number Write that number in another column, cross it off first list Repeat process, always look for smallest number remaining Stop when all numbers have been crossed off

13 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

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 ) ; } Selection Sort Algorithm

15 Sequential Search in a Sorted List Note difference from previous search void search_ord ( int list[ ], int target, int length, int & index, boolean & found) {index = 0; list [length] = target; // store an item beyond end while (target > list [index]) index++; found = (index < length && ltem = = list[index]; ) Explain how the last statement works

16 Inserting into an Ordered List We wish to insert a new number into the list in the right position –find where it goes -- look until you find a number bigger than the new number list 2 : length : 5 59

17 list 2 : Inserting into an Ordered List We wish to insert a new number into the list in the right position –find where it goes -- look until you find a number bigger than the new number –shift that number all the rest of the elements down list 2 : length : 5 59

18 list 2 : Inserting into an Ordered List We wish to insert a new number into the list in the right position –find where it goes -- look until you find a number bigger than the new number –shift that number all the rest of the elements down –insert the new number in the vacated spot list 2 : length : 5 59

19 length : 5 Inserting into an Ordered List We wish to insert a new number into the list in the right position –find where it goes -- look until you find a number bigger than the new number –shift that number all the rest of the elements down –insert the new number in the vacated spot –be sure to increment the length length : 6 list 2 :

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

21 Working with Character Strings String => a collection of characters interpreted as a single item –a structured data item –in C++ a null-terminated sequence of characters stored in a char array All strings in C++ are terminated by the null character –character 0, ‘\0’

22 Initializing Strings When a character array is declared, it is legal to use the assignment operator to initialize Note : use of the = operator only legal for char array initialization But : aggregate array assignment is NOT greeting = “don’t do it;

23 String Output Strings (character arrays) are handled differently than other types of arrays This would NOT be allowed This is legal: int num_list [100];... cout << num_list; char name [30] = “Snidly Q. Fizbane”;... cout << name;

24 String Input Declare strings 1 element bigger than planned size to allow for ‘\0’ When input takes place, C++ automatically places the ‘\0’ in memory at the end of the characters typed in

25 Problems with >> for String Input Cannot be used to input a string with imbedded blanks >> stops reading as soon as it encounters first whitespace character

26 Problems with >> for String Input Solve problem by using getline ( … ) Quits reading after 15 characters or when it hits a newline, whichever comes first. Includes all characters including spaces, tabs, etc (whitespace characters)

27 Problems with >> for String Input If declared string is too small >> keeps putting characters in memory PAST that area in memory s2 contents extend into the memory area of s3

28 Using Strings Instead of “hard coding” file name for the open ( … ) command, –use a string variable, –use keyboard entry with cin.getline(…) –program more flexible, good for different files ifstream inFile; char fname[31]; cout “; cin.getline (fname, 30, ‘\n’); inFile.open (fname);

29 String Library Routines Recall that we could not use the aggregate assignment of one string to another C++ provides some string handling functions to do this (and other similar tasks) Found in

30 String Library Routines String comparison: returns -1 if s1 s2 String assignment Returns length of the string

31 Using typedef with Arrays Specify an array type –this can be used throughout program –helps program self document Example : typedef char default_string [80];... defalt_string fname, descrip; void reverse (default_string s); typedef char default_string [80];... defalt_string fname, descrip; void reverse (default_string s);

32 Testing and Debugging Be sure to account for null character when you manipulate characters individually in a string Remember proper use of the = –correct for initialization at declarationtime –INCORRECT for aggregate assignment Aggregate input/output allowed for strings but NOT for other array types

33 Testing and Debugging If you use the >> for string input, make sure –string is declared large enough –string will have no white spaces The >> operator stops at, but does not consume the first trailing white space –such as ‘\n’ or a space The cin.getline (whatever, 30, ‘\n’ ) function –stops when reading the ‘\n’ –consumes the ‘\n’ –has problems when ‘\n’ is still in the input stream

34 Testing and Debugging When using the strcpy ( ), make sure that the destination array is declared long enough Choose test data carefully for string handling programs –include strings that are too large –include strings with whitespace