Announcements HW6 due this Wednesday

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Searching Arrays Linear search Binary search small arrays
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
1 CSC 222: Computer Programming II Spring 2004 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search Class design  templated.
Searching a vector We can search for one occurrence, return true/false or the index of occurrence Search the vector starting from the beginning Stop searching.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Lists in Python.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Pointers OVERVIEW.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSC 211 Data Structures Lecture 13
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
2nd Midterm Exam December 26th, 2008, Friday 17:40 – 19:20 Places: FENS G032 Acar - Avcı FENS G035 Aydeniz - Büyükkıdık FASS G022 Büyüknacar - Demir FASS.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Announcements Midterm 2 is on TODAY, December 19 th at 19:40. Extra Recitation TODAY at 15:40-18:30 in FENS L062 Midterm Places: if (LastName
A Computer Science Tapestry 8.1 Vectors l Vectors are homogeneous collections with random access ä Store the same type/class of object, e.g., int, string,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Vectors of structs We can define vectors of structs struct student { unsigned int id; string name, lastname; double gpa; }; vector class(11); // a vector.
A Computer Science Tapestry 8.1 Vectors l Vectors are homogeneous collections with random access  Store the same type/class of object, e.g., int, string,
Announcements Midterm2 on April 18 th Monday at 19:40 This week recitations, we will solve sample midterm questions. I will also send previous years’ exams.
Arrays Chapter 7.
Searching Arrays Linear search Binary search small arrays
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 11 - JavaScript: Arrays
Recursion Version 1.0.
Data Structures I (CPCS-204)
Chapter 9: Searching, Sorting, and Algorithm Analysis
The Ohio State University
Chapter 7 Part 1 Edited by JJ Shepherd
2nd Midterm Exam December 5th, 2015, Saturday 10:00 – 12:00
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Announcements Final Exam on August 17th Wednesday at 16:00.
Arrays … The Sequel Applications and Extensions
What’s Left in the Course
Announcements Midterm2 grades were announced
Computer Science 2 Hashing
Topics Introduction to File Input and Output
Chapter 8 Search and Sort
Searching and Sorting Arrays
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements HW6 due this Wednesday
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
Data Structures Sorted Arrays
Announcements Final Exam on December 25th Monday at 16:00.
Vectors Vectors are homogeneous collections with random access
Search,Sort,Recursion.
Chapter 9: Data Structures: Arrays
Data Structures and Algorithm: SEARCHING TECHNIQUES
Chapter 9: More About Data, Arrays, and Files
Vectors Vectors are homogeneous collections with random access
Topics Introduction to File Input and Output
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Vectors Vectors are homogeneous collections with random access
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors Vectors are homogeneous collections with random access
Presentation transcript:

Announcements HW6 due this Wednesday Output of Sample Run 1: 2000 2009 Konya Turkey 2000 2009 Tuzla Turkey 2000 2009 Istanbul Turkey Name of output file in Sample Run 3: 2000_2002_Turkey.txt HW7 will be assigned this week and will be due January 6th Wednesday (last week) You may see your Midterm2 papers on Thursday, 12:30 – 14:30 in FENS L068 (Office Hour Room). Please complete the Course Evaluations BAGEM should have sent a message about this

Announcements about HW6 You may need to use clear() member function before you try to reopen an input file stream object that you failed to open previously (for example due to wrong file name), or that you opened and processed but for some reason if the error flags are set (for example due to reaching the end of the file). Possible reasons for run time errors in this homework Attempting to read from a file that has not been opened yet Attempting to write to a file that has not been opened yet Range and index problems while trying to access characters of a string using find, substr and at member functions.

Announcements about HW6 “Should we check if the output file is opened successfully or not?” Not required, but advised There might be some cases that the output files are not opened successfully If you check and the output file is not opened, then do not continue with the program. What happens if the files are opened but the content is irrelevant? What happens if the file names are entered in the wrong order? What happens if both file names are the same? All of these questions are reduced into the same main question: should we make the content check for the files that are opened successfully? NO. As mentioned in the HW document, the content of the files are assumed to be correct. What you have to do is only to check if the files are opened successfully or not and continue to read file names until opened. Once opened, we assume that the first file is the business database, and the second one is the distance database files.

Example Counting letters of a file display number of occurrences of each letter at the end counting is case insensitive see letters.cpp (the one in book is a bit different)

tvector as a return type Vector can be return type of a function tvector<int> Count (istream & input, int & total); Example: modify letters.cpp such that count returns the vector (not as reference parameter) see letters2.cpp

Vectors of structs We can define vectors of structs struct student { 1250 id gpa name lastname We can define vectors of structs struct student { unsigned int id; string name, lastname; double gpa; }; tvector<student> class(11); // a vector with 11 students class[1].gpa = 3.2; for (i = 0; i <= 10; i++) class[i].id = i + 1250; 1251 id 3.2 gpa name lastname 1 1260 id gpa name lastname 10

Vector of struct Example define a struct for a track on a CD track number and title are fields define a vector for 10 tracks shuffle these 10 tracks at random see shuffle.cpp (in book, but this version is slightly modified)

Vectors as lists The “vector as counters” example constructs and initializes a vector with a specific number of elements Other uses of vector require the vector to “grow” to accommodate new elements Consider reading words from a text file, storing them in a vector How big should we define vector initially? What are potential problems? When a vector is used as a list, we’ll use a different method for adding elements to the vector so that the vector can “grow”

Reading words into a vector (problematic version) tvector<string> words(1000); string w; int i = 0; string filename = PromptString("enter file name: "); ifstream input(filename.c_str()); while (input >> w) { words[i]=w; i++; } cout << "read " << i << " words" << endl; What is the problem? there might be more than 1000 words in the file in this case index runs out of range

Reading words into a vector (with index range control but still problematic) tvector<string> words(1000); string w; int i = 0; string filename = PromptString("enter file name: "); ifstream input(filename.c_str()); while ((input >> w) && (i < 1000)) { words[i]=w; i++; } cout << "read " << i << " words" << endl; What is the problem? works fine if there are no more than 1000 words but if there are more than 1000 words, the rest is not read

Reading words into a vector (no problems) One method would be to pass over the file two times one to find out number of words second to read the words into array Another method is to benefit from tvector class utilities as in the following code tvector<string> words; //create empty vector string w; string filename = PromptString("enter file name: "); ifstream input(filename.c_str()); while (input >> w) { words.push_back(w); //adds the next word to the vector //also increases the size if necessary } cout << "read " << words.size() << " words" << endl;

Using tvector::push_back The method push_back adds new objects to the “end” of a vector, Internally, the vector keeps track of its capacity If there is capacity, then there is no problem; the new item is added to the end of the vector When the capacity is reached and push_back attempts to add a new element to the vector, then the vector automatically “grows” by doubling the capacity 0, 2, 4, 8, 16, 32, ... If you want to use push_back mechanism, then the vector should be defined initially without specifying a size empty vector (zero size)

Size versus Capacity Capacity is the allocated size of the vector Size is how many elements are in the vector so far They are not the same concepts, but related as described in the previous slide and illustrated below tvector<string> names; // size is 0, capacity is 0 names.push_back("Ali"); // size is 1, capacity is 2 names.push_back("Husnu"); // size is 2, capacity is 2 names.push_back("Ayse"); // size is 3, capacity is 4 names.push_back("Cem"); // size is 4, capacity is 4 names.push_back("Jale"); // size is 5, capacity is 8

size()member function size() member function basically returns the number of elements in the vector When a vector is defined with no initial capacity, and push_back is used to add elements, size() member function returns the number of elements exist in the vector This is the number of calls of push_back() if no elements are deleted If elements deleted using pop_back(), size updated too (decremented) If a non-empty vector is created, then the capacity and the size is set to the number of elements of the vector. This capacity is considered full, so the first push_back causes to double the capacity. What about size() in case the vector is created as a non- empty one returns the size specified during declaration if no push_back() is used returns the size specified during declaration + the number push_back()s, if push_back() is used

capacity()and reserve() The capacity of vector is accessible using capacity() member function programmers don’t often need this value An initial capacity of N elements can be specified using reserve(N) member function

Demo Example Read some strings from keyboard and store in a tvector of strings. At the end display the vector. version 1: no reserve version 2: (decomment the reserve lines): with reserve version 3: vector is not created empty (decomment second definition and comment out first one and reserve lines) See tvectordemo.cpp (not in the book)

Vector Processing Examples – 1 (vectorproc.cpp – not in book) write a function that takes a tvector of integers as parameter and returns the maximum of numbers in it process all array elements – for loop from 0 to vector’s size - 1 int max (const tvector<int> & v) //pre: vector v is not empty //post: return max of elements in v { int i, max_so_far = INT_MIN; for (i=0; i < v.size(); i++) if (v[i] > max_so_far) max_so_far = v[i]; } return max_so_far;

Vector Processing Examples – 2 (vectorproc.cpp – not in book) Write a function that takes a tvector of integers as parameter and returns true if the vector is sorted in ascending manner, false otherwise may not process all vector elements In this type of rule-checking applications, a possible method is to assume that the rule is satisfied before the loop and find a counterexample in the loop bool issorted (const tvector<int> & v) //post: returns true if the array is acsending sorted { bool s = true; // initially assume that array is sorted //in the function try to break this assumption int i =1; while (i < v.size() && s == true) { //check until the end of array or until a counterexample is found if (v[i-1] > v[i]) // if not sorted s = false; // counterexample is found i++; } return s;

Searching a vector We can search for one occurrence, return true/false or the index of occurrence Search the vector starting from the beginning Stop searching when match is found We can search and count the number of occurrences and return count Search entire vector Similar to one occurrence search, but do not stop after first occurrence We can search for many occurrences, but return occurrences in another vector rather than count In all these cases, we search the vector sequentially starting from the beginning This type of search is called “sequential search”

Counting search int countmatches(const tvector<string> & a, const string& s) // post: returns # occurrences of s in a { int count = 0; int k; for(k=0; k < a.size(); k++) { if (a[k] == s) count++; } return count; How can we change this code to return the index of the first occurrence? see next slide

One occurrence search int firstmatch(const tvector<string> & a, const string& s) // post: returns the index of occurrence of s in a, -1 // otherwise { int k; for(k=0; k < a.size(); k++) { if (a[k] == s) return k; } return -1; Does not search the entire array if one match is found good for efficiency purposes How could you modify this to return true/false?

Collecting search Collect the occurrences in another vector void collect(const tvector<string> & a, tvector<string> & matches) // pre: matches is empty // post: matches contains all elements of a with // first letter 'A' { int k; for(k=0; k < a.size(); k++) if (a[k].substr(0,1) == "A") matches.push_back(a[k]); }

Binary search Alternative to sequential search for sorted vectors If a vector is sorted we can use the sorted property to eliminate half of the vector elements with one comparison What number (between 1 and 100) do we guess first in number guessing game? Idea of creating program to do binary search Check the middle element If it has the searched value, then you’re done! If not, eliminate half of the elements of the vector search the rest using the same idea continue until match is found or there is no match how could you understand that there is no match? let’s develop the algorithm on an example we need two index values, low and high, for the search space

Binary Search (search for 62) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 24 34 52 55 62 67 75 80 81 90 92 100 101 111 low=0 mid=7 high=14 low = 0 mid=3 high=6 low=4 high=6 mid=5 => FOUND

Binary Search (search for 60) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 24 34 52 55 62 67 75 80 81 90 92 100 101 111 low = 0 mid=7 high =14 low=0 mid=3 high=6 low=4 high=6 mid=5 low=4 high=4 mid=4 low=5 high=4 => NO MATCH FOUND – STOP

Binary search code int bsearch(const tvector<string>& list, const string& key) // pre: list.size() == # elements in list // post: returns index of key in list, -1 if key not found { int low = 0; // leftmost possible entry int high = list.size()-1; // rightmost possible entry int mid; // middle of current range while (low <= high) { mid = (low + high)/2; if (list[mid] == key) // found key, exit search { return mid; } else if (list[mid] < key) // key in upper half { low = mid + 1; else // key in lower half { high = mid - 1; return -1; // not in list

Comparing Sequential and Binary Search Given a list of N elements: Binary search makes on the order of log N operation O(log N) Linear (sequential) search takes on the order of N operations O(N)