+ All Categories
Home > Documents > CS 106X – Programming Abstractions in C++

CS 106X – Programming Abstractions in C++

Date post: 24-Feb-2016
Category:
Upload: katoka
View: 25 times
Download: 0 times
Share this document with a friend
Description:
CS2 in C++ Peer Instruction Materials by  Cynthia Bailey Lee  is licensed under a  Creative Commons Attribution- NonCommercial - ShareAlike 4.0 International License . Permissions beyond the scope of this license may be available at  http://peerinstruction4cs.org . - PowerPoint PPT Presentation
17
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution - NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.or g .
Transcript
Page 1: CS 106X –  Programming Abstractions in C++

CS 106X – Programming Abstractions in C++Cynthia Bailey Lee

              CS2 in C++ Peer Instruction

Materials by Cynthia Bailey Lee is licensed under a 

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Permissions beyond the scope of this license may be available at 

http://peerinstruction4cs.org.

Page 2: CS 106X –  Programming Abstractions in C++

2

Today’s Topics1. Queues contrasted with Stacks2. Queues example: Mouse events3. Queue example: Josephus Survivor

Puzzle4. Iterating using range-based for loop

Page 3: CS 106X –  Programming Abstractions in C++

Queues

Page 4: CS 106X –  Programming Abstractions in C++

Stacks vs. Queues Stack is referred to as a LIFO data structure

“Last in, first out” Queue is referred to as a FIFO data structure

“First in, first out”

How many of these operate as FIFO/Queue? Patients waiting in a hospital Emergency Room Passengers boarding and exiting an airplane Passengers boarding and exiting an elevator (assume all

board on ground floor and exit at the same destination) People waiting in line for an amusement park ride

(A) None (B) 1 (C) 2 (D) All 3

Page 5: CS 106X –  Programming Abstractions in C++

LIFO:

FIFO:

Image is modified by Cynthia Lee based on original from Wikimedia Commons “A seatmap over the Airbus A380” Source=self-made |Date=24 September 2007 |Author= S. Solberg J. Used under Creative Commons License

Page 6: CS 106X –  Programming Abstractions in C++

Queue ApplicationsMouse events

Page 7: CS 106X –  Programming Abstractions in C++

Event queues While your code executes, a separate program is

constantly listening for events and recording them Mouse moved, mouse clicked, mouse dragged,

keyboard key pressed, etc Every once in a while, your code can call

getNextEvent() to see what has happened getNextEvent returns the events one at a time in the

same order they happened In other words, returns them in FIFO order! When it is “recording” events, it is enqueuing

events in an event QUEUE Very common use of the Queue ADT

Page 8: CS 106X –  Programming Abstractions in C++

Queue ApplicationsJosephus Survivor Puzzle

Page 9: CS 106X –  Programming Abstractions in C++

Josephus Survivor Puzzle N people seated in a circle Skip M living people and kill

the (M+1)th, repeatedly around the circle, until two remain

Question:Where do you sit?

12

3

4

56

789

10

11

12

Page 10: CS 106X –  Programming Abstractions in C++

Josephus Survivor Puzzle Question: (you will need paper & pencil)

N = 12, M = 3Where do you sit?A. 1 or 2B. 1 or 6C. 5 or 10D. 5 or 6E. Other/none/more

12

3

4

56

789

10

11

12

Page 11: CS 106X –  Programming Abstractions in C++

Queues and Josephus Puzzle1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever. If more than two

alive, repeat steps 2&3

N = 5, M = 2

1. 2. 3. 2. 3. 2. 3.

?

Page 12: CS 106X –  Programming Abstractions in C++

Queues and Josephus Puzzle1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever. 4. If more than two alive, repeat steps 2&3

N = 5, M = 2

12345

Contents of the queue, reading from top down:A. 1, 2, 4B. 2, 4, 5C. 2, 4, 5, 1D. 4, 5, 1, 2E. Other

4512

1245

?

1. 2. 3. 2. 3. 2.34512

3.

Page 13: CS 106X –  Programming Abstractions in C++

Queues and Josephus Puzzle:What does that look like in code?

1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever 4. If more than two alive, repeat steps 2&3

Page 14: CS 106X –  Programming Abstractions in C++

Iterating over a collection

Page 15: CS 106X –  Programming Abstractions in C++

C++11 has a nice for loop just for collections ADTs Range-based for loop gives you each item in a

collection, one at a time For ADTs with a clear linear order (Vector,

Stack, Queue), they are given to you in that order

[Spoiler for Wednesday!] Some ADTs are not linear. In theory, we often consider the ordering of iteration on these to be “undefined,” but in practice some implementations allow you to rely on a certain order

Page 16: CS 106X –  Programming Abstractions in C++

C++11 range-based for loop One way to iterate over a Vector:for (int i=0; i<classlist.size(); i++){

cout << classlist[i] << endl;}

Range-based for loop makes the code cleaner:for (string name : classlist){

cout << name << endl;}

Page 17: CS 106X –  Programming Abstractions in C++

Other iteration techniques in C++ (NOT recommended for this class) STL vector and iteratorsfor (vector<int>::iterator it = classlist.begin(); it != classlist.end(); ++it){cout << *it << endl;}

Stanford library “foreach” loop (obsoleted by range-based for loop in C++11):foreach (string name in classlist){cout << name << endl;}


Recommended