Lecture 29 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation

lecture 29
SMART_READER_LITE
LIVE PREVIEW

Lecture 29 Log into Linux. Copy files on csserver from - - PowerPoint PPT Presentation

Lecture 29 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture29/*.* Reminder: Project 5 and Homework 7 are due on Wednesday. Questions? Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 1


slide-1
SLIDE 1

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 1

Lecture 29

 Log into Linux. Copy files on csserver from

/home/hwang/cs215/lecture29/*.*

 Reminder: Project 5 and Homework 7 are due

  • n Wednesday.

 Questions?

slide-2
SLIDE 2

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 2

Outline

 Stacks applications

 Balanced parentheses  Arithmetic expression evaluation

 Queue applications

 Radix sort - this application is not in the textbook

slide-3
SLIDE 3

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 3

Balanced Parentheses

 Consider strings consisting of left and right

  • parentheses. Such a string is said to be

balanced if there are the same number of left and right parentheses and they are properly nested.

 Some examples of balanced parentheses:

( ( ) ) ( ( ) ( ) ) ( ) ( ) ( ( ) ) ( )

 Some examples of unbalanced parentheses:

( ( ) ( ( ) ) ) ( ) ) ( ( )

slide-4
SLIDE 4

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 4

IsBalanced

 We want to write a function IsBalanced that

receives a string. It returns true if the parentheses in the string are balanced; false

  • therwise.

 The basic idea is that we scan the string from

left to right, keeping track of the left parentheses so that we can match them up with the right parentheses as they are encountered.

slide-5
SLIDE 5

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 5

IsBalanced

 In particular, a right parenthesis matches the

most recently processed left parenthesis. This is LIFO behavior, so a stack is needed.

 The basic algorithm is:

  • 1. Initialize failed to false and i to 0
  • 2. While not failed and i less than the expression length

2.1 Set ch to expression[ i ] 2.1 If ch is '(', then push it on the stack 2.2 Else if ch is ')' and stack is not empty, then pop the stack 2.3 Else if ch is ')' and stack is empty, then set failed to true 2.4 Increment i

  • 3. Return (stack is empty) and (not failed)

 Complete file parens.cpp

slide-6
SLIDE 6

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 6

Arithmetic Expression Evaluation

 A fully-parenthesized numeric expression is

  • ne in which every binary operation is

surrounded by parentheses. For example:

((((12 + 9)/3) + 7.2)*((6-4)/8))

 For this example, we will assume that the

expression is well-formed, and we will restrict the operators to binary operators +, -, *, and /, and will restrict the operands to the positive numbers.

slide-7
SLIDE 7

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 7

ReadAndEvaluate

 We want to write a function ReadAndEvaluate

that reads a line of input from an input stream. The input is a fully-parenthesized numeric expression, and the function evaluates the expression and returns the result.

 The main idea is that the right parentheses

mark when computation must occur. Until one is encountered, we must keep track of the

  • perands and the operators.
slide-8
SLIDE 8

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 8

ReadAndEvaluate

 When a right parenthesis is seen, then the

previous operator and two operands must be used to compute the expression finished

  • subexpression. Once again this is LIFO order.

Since operands and operators are of different types, we need two stacks.

slide-9
SLIDE 9

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 9

ReadAndEvaluate

 The basic algorithm is

  • 1. While input stream is valid and the next char is not a newline

1.1 If next char is a digit or period 1.1.1 Read in a number and push it onto the operand stack 1.2 Else if next char is operator 1.2.1 Read in a symbol and push it onto the operator stack 1.3 Else if next char is a right parenthesis 1.3.1 Ignore the char and evaluate the expression at the top of the stacks using EvaluateStackTops 1.4 Else ignore the char

  • 2. Return top of operand stack

 Complete file calc.cpp

slide-10
SLIDE 10

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 10

EvaluateStackTops

 The function EvaluateStackTops receives and

passes back the operand and operator stacks. It computes the result of applying the top

  • perator to the top two operands. These items

are popped of their respective stacks and the result is pushed onto the operand stack.

 Note that the top of the operand stack is the

right operand and the second from the top is the left operand.

slide-11
SLIDE 11

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 11

EvaluateStackTops

 The basic algorithm is

  • 1. Set operand2 to the top of the operand stack, then pop it off
  • 2. Set operand1 to the top of the operand stack, then pop it off
  • 3. Select on the top of the operator stack

3.1 Case '+' 3.1.1 Push (operand1 + operand2) onto the operand stack 3.2 Case '-' 3.2.1 Push (operand1 - operand2) onto the operand stack 3.3 Case '*' 3.3.1 Push (operand1 * operand2) onto the operand stack 3.4 Case '/' 3.4.1 Push (operand1 / operand2) onto the operand stack

  • 4. Pop the operator stack
slide-12
SLIDE 12

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 12

RadixSort

 In the early days of computing, data were

stored on punch cards. A mechanical sorter could sort data in the following way:

 Suppose all numbers are positive and have two

  • digits. Create 10 bins labeled 0-9.

 Pass 0: Look at the ones (i.e. rightmost) digit,

and distribute cards into the appropriate bin. Collect the cards from each bin in 0-9 order.

 Repeat with the tens digit. At the end the cards

are sorted.

slide-13
SLIDE 13

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 13

RadixSort

 The "trick" can be seen by considering the last

  • pass. In the second pass, the numbers with a

lower tens digit appear before ones with a higher tens digit since collection is from 0-9.

 All numbers in a particular bin ended up in

sorted order, because the previous pass ensures that the numbers with lower ones digit are before the numbers with higher ones digit.

slide-14
SLIDE 14

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 14

RadixSort

 The algorithm relies on the numbers in a bin

being accessed in FIFO order, so need queues. We will use an array of 10 local queues, one for each digit value 0-9.

 Generalize this process to d-digit numbers with

d >= 1. Recall that a decimal number with digits xi is the sum of the products of its digits with powers of 10:

value = xd-1*10d-1 + xd-2*10d-2 + ... + x1*101 + x0100

slide-15
SLIDE 15

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 15

RadixSort

 We want to write a function RadixSort that

receives and passes back a vector of integers, and receives the maximum number of digits a number can have. The function sorts the vector using the radix sort algorithm

 The numbers are distributed from the vector to

the appropriate queues using function Distribute and then are collected back to the vector using function Collect.

slide-16
SLIDE 16

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 16

RadixSort

 The basic algorithm is

  • 1. Initialize power to 1
  • 2. For i from 0 to d-1

2.1 Distribute the values using Distribute (v, digitQueue, power) 2.2 Collect the values using Collect (digitQueue, v) 2.3 Multiply power by 10

 Complete file radixsort.cpp

slide-17
SLIDE 17

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 17

Distribute

 The Distribute function receives the vector,

passes back the queues, and receives the power that represents which digit place is being used to distribute.

 The basic algorithm is

  • 1. For i from 0 to v.size() -1

1.1 Push v[i] onto digitQueue[(v[i] / power) % 10]

slide-18
SLIDE 18

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 18

Collect

 The Collect function receives and passes back

the queues and the vector.

 The basic algorithm is

  • 1. Initialize i to 0
  • 2. For digit from 0 to 9

2.1 While digitQueue[digit] is not empty 2.1.1 Set v[i] to first element of digitQueue[digit] 2.1.2 Pop digitQueue[digit] 2.1.3 Increment i

slide-19
SLIDE 19

Monday, November 1 CS 215 Fundamentals of Programming II - Lecture 29 19

Analysis of RadixSort

 Radix sort is very fast. It is linear in the maximum

number of digits, O(d).

 However, it has limited applicability. Need to know

maximum number of digits, d.

 Need a finite number of "digit" values, since there is a

queue for each value. Could apply to strings, but there would be ~80 queues.

 Takes up twice as much space. Every number must

be stored in the vector and in a queue.

 Generally only used for a relatively small number of

positive integers.