lecture 20 the last function of reason is to recognize that there are an infinity of things which...
TRANSCRIPT
![Page 1: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/1.jpg)
42510011 0010 1010 1101 0001 0100 1011
Lecture 20
The last function of reason is to recognize that there are an infinity of things which surpass it.
Blaise Pascal
![Page 2: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/2.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
State machines
• Lets take a look at the programs that we’ve written so far. We should realize that we haven’t been to strict about erroneous input.
• Our programs lack fundamental error handling. What are some standard ways for dealing with this problems?
![Page 3: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/3.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
The phone conversion program
• Given a phone number in the letter form, print it out using digits. (This is one of the problems from last time quiz).
• We need to design a robust program which can perform this task.
![Page 4: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/4.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Defining the input format
First thing to do is to enforce user input. There are many different ways to format phone number, so just pick one:
(XXX)XXX-XXXX
We have area code in parenthesis then three digits, dash, then four last digits.
![Page 5: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/5.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Observe the details to extract states.
(XXX)XXX-XXXX
Area code - enclosed in ()
First three digits are followed by ‘-’
Finally four last digits.
What are the states and what is a state machine?
![Page 6: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/6.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
State machine
State machine is a part of the program, usually used to keep track of where we are in the certain process. For example, in the phone number problem, we can be reading the area code, which is one state, or we can be reading first three digits of a phone number which is a different state.
![Page 7: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/7.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
State transition diagram
S0
S1
S2
S3
a
b
c
If you are in state 0 and you see a transition to state 1If you are in state 0 and you see b transition to state 2If you are in state 0 and you see c transition to state 3
![Page 8: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/8.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Parsers (essentially a ‘format-enforcer’)
- Read next input character
- Look in what state you are and what is the character that you read. Decide if this character is a valid input at the moment, figure out what is the next state. If the input was not valid, issue an error message.
![Page 9: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/9.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Phone number parser
(XXX)XXX-XXXX
States:
1. Init (didn’t see any input)
2. Area code (reading area code)
3. First three digits (reading first three digits)
4. Last four digits (reading last four digits)
![Page 10: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/10.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Phone number parser
(XXX)XXX-XXXX
What are the transitions?
When we see ‘(‘
transition from State 1 to State 2
(Init ----> read area code)
When we see ‘)’
transition from State 2 to State 3
( Read area code --> read first 3 digits)
![Page 11: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/11.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Phone number parser
(XXX)XXX-XXXX
What are the transitions?
When we see ‘-‘
transition from State 3 to State 4
( Read first 3 digits --> read last four digits)
The characters ‘(‘, ‘)’ and ‘-’ are signal characters that help us identify transitions.
![Page 12: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/12.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
How do we code the state machine?
State machines are usually encoded using case statement, in pseudo code:
case iState of
State1 : do something
State2 : do something
State3 : do something
etc.
![Page 13: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/13.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Why use state machines?
• They provide logical, methodical approaches to sequential input processing. It is easier to design state machine, then have a a lot of if-then-else statements. Lets try to design the state machine for the phone number problem.
![Page 14: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/14.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Phone number example state machine
Init
Area code
Last fourLetters
Input is ‘(‘
Input is digitand count <= 3
First threeletters
Input is ’)’and count = 4
Input is digitand count <= 3
Input is ’-’and count = 4
(XXX)XXX-XXXX
Input is digit In every state, if the input is not correct issue the error and halt.
![Page 15: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/15.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Optional (hard) problem for next time
• Write a program that converts letter phone numbers into digit phone numbers using the state machine that we discussed.
![Page 16: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/16.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
While loops
• We have already encountered construct in Pascal which are not necessary but convenient. For example case statement.
• Pascal provides two more for-loop-like constructs of which we will learn one.
![Page 17: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/17.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Format
While ( <BOOLEAN EXPRESSION> ) DO
BEGIN
…
END; { of while loop }
![Page 18: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/18.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
While loop
This simply allows us to perform set of operations while certain condition is true.
Why is this useful? Lets see an example.
![Page 19: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/19.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
While loop is actually more powerful than for-loop
• Can we simulate while-loop with for-loop?
• Can we simulate for-loop with while loop?
![Page 20: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/20.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
While loop is actually more powerful than for-loop
• We can’t simulate while loop with for-loop because we will not know when we should terminate.
• On the other hand, we can simulate for-loop with while loop like this:
![Page 21: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/21.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Simulating the for-loop with while loop
For I := 1 to 10 do
…
I := 1;
while ( I != 10 ) do begin
…
I := I + 1;
end;
![Page 22: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/22.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Home work
• Program 6 is due Monday, November 30
• Read chapter 8, sections 1,2 ignore whatever you don’t understand.
• Attempt the optional problem (spend at least 1/2 hour thinking about it)
![Page 23: Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649f3f5503460f94c5fb0c/html5/thumbnails/23.jpg)
4251
0011 0010 1010 1101 0001 0100 1011
Program from this lecture
• Count words