Week 8 (CS 200)
Table of Contents
Debugging, searching, sorting
1. Class schedule
- Tuesday: Debugging tools, Linear Search
- Thursday: Sorting, Binary Search
2. Assignment links
| Assignment | Reading link | Canvas link |
|---|---|---|
| 📚 Intro: General debugging | Read | Canvas quiz |
| 📚 Intro: Debugging tools - lldb and gdb | Read | Canvas quiz |
| 📚 Intro: Searching algorithms | Read | Canvas quiz |
| 📚 Intro: Sorting algorithms | Read | Canvas quiz |
| 💾 Lab: Debugging, search/sort algorithms | Read | Canvas assignment |
| 💬 Discussion: Case study: Facebook/Meta | Canvas discussion | |
| 🙆 Update: Week 08 | Canvas assignment |
3. Example code
3.1. Searching
#include <iostream> #include <vector> #include <string> using namespace std; int SearchFirstMatch( vector<string> arr, string findme ) { for ( int i = 0; i < arr.size(); i++ ) { if ( arr[i].find( findme ) != string::npos ) { return i; } } return arr.size(); } vector<int> SearchAllMatches( vector<string> arr, string findme ) { vector<int> matches; for ( int i = 0; i < arr.size(); i++ ) { if ( arr[i].find( findme ) != string::npos ) { matches.push_back( i ); } } return matches; } int main() { vector<string> courses = { "CS 134: Programming Fundamentals", "CS 200: Concepts of Programming Algorithms Using C++", "CS 201: Concepts of Programming Algorithms using C#", "CS 202: Concepts of Programming Algorithms using Python", "CS 205: Concepts of Programming Algorithms using Java", "CS 235", "CS 250", }; cout << "Search for which class? "; string text; getline( cin, text ); cout << endl; // Return all matches vector<int> matchIndices = SearchAllMatches( courses, text ); cout << matchIndices.size() << " match(es) returned" << endl; for ( int i = 0; i < matchIndices.size(); i++ ) { int courseNum = matchIndices[i]; cout << "Found at index " << courseNum << ": "; cout << courses[courseNum] << endl; } // Return only 1 result int courseNum = SearchFirstMatch( courses, text ); if ( courseNum == courses.size() ) { // not found cout << "Course does not exist!" << endl; } else { cout << "Course is at: " << courseNum << endl; cout << "This course is:" << endl; cout << courses[courseNum] << endl; } cout << endl; return 0; }