Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++, Sorting, Convex Hull

Similar presentations


Presentation on theme: "C++, Sorting, Convex Hull"— Presentation transcript:

1 C++, Sorting, Convex Hull
CS2 – Winter 2017

2 Recitation Outline Thoughts on Assignment 1 (as well as thoughts for the future) More C++ - features and how to be an independent programmer rather than a spoon-fed one Sorting algorithms Convex-hull algorithms

3 Thoughts on Assignment 1 (and for the future)
If you’re going to be programming for your career, you need several skills: Reading. Many people don’t read the assignment carefully. There’s a reason they’re so long. Debugging. Take your time on Assignment 2 and learn how to debug. Debugging is an art and distinguishes amateurs from okay programmers. Testing. As you are thinking about a problem, you should be thinking about every possible case that your code will be tackling. Testing is also an art and distinguishes okay programmers from awesome programmers.

4 Some more technical thoughts…
Tips for headers: Include only .hpp files in your .cpp files. Use header guards to prevent redefinition: #ifndef SOMETHING_H #define SOMETHING_H /**** Header file contents here ****/ #endif If you have time and you didn’t do the last eight points of Assignment 1, you should go back and do it. Classes are an important feature of C++…

5 More C++ - Something better than arrays
Arrays are kinda annoying… No easy way to resize. Pointers can be ugly to work with. No bounds checking. Better than arrays? VECTORS – resizable arrays.

6 More C++ - Vectors // Include this library, which provides access to vectors and provided functions #include <vector> // This initializes a vector of 20 ints, called nums. std::vector<int> nums(20); // You can access elements in the vector the same way you do arrays. for(int i = 0; i < 20; i++) { nums[i] = i * 2; } // You can add elements to the end of a vector at will (effectively resizing it each time). for(int j = 0; j < 20000; j++) { nums.push_back(j + 42); // Or you can manually resize it (here to 50 elements) nums.resize(50);

7 More C++ - how to be an independent programmer
Remember the thing about programmers and reading? The ability to read documentation is important. Understanding how to read the documentation will allow you to progress beyond the C++ material covered in CS2. Go to Read through it. Just a small exercise to get you used to this: Which function allows you to get the current size of the vector? Scroll down to “Member functions” I don’t know how to use the function… Click “size”

8 More C++ - Command-line Arguments
argc: the number of command line arguments + 1 argv: the command line arguments as an array (argv[0] is the program name) // Include this library, which allows you to use cout #include <iostream> // Note that you can use char * argv[] or char ** argv – same thing int main(int argc, char * argv[]) { // If no command line arguments were given, print out program name if(argc < 2) std::cout << argv[0] << std::endl; // If command line arguments were given, print out all of them else { for(int i = 0; i < argc; i++) std::cout << argv[i + 1] << std::endl; } return 0;

9 More C++ - File I/O

10 Sorting Algorithms Bubble-sort 10 4 5 1 4 10 5 1 4 5 10 1 4 5 1 10 4 5

11 Sorting Algorithms Merge Sort (Be careful when merging…) 10 4 5 1 10 4

12 Sorting Algorithms Quicksort Less than pivot Greater than pivot
Repeat on each (The less-than-pivot list only has one element so we’re done there) 10 4 (pivot) 5 1 1 (done) 4 10 (new pivot) 5 1 4 5 10 1 4 5 10

13 Who cares about sorting a bunch of numbers???
Very rarely is sorting the end goal of a problem… Think of a library. Why do you want the books sorted in order? Usually, it’s not for the sole purpose of having sorted books… You want to find the books fast! Imagine having to look through the entire library every time you want a book… Same thing in CS.

14 Convex Hull Algorithms

15 Gift Wrapping Algorithm

16 Gift Wrapping Algorithm

17 Graham Scan Algorithm

18 Graham Scan Algorithm

19 Graham Scan Algorithm


Download ppt "C++, Sorting, Convex Hull"

Similar presentations


Ads by Google