Lecture 9 There are ain’t no way to find out why a snorer can’t hear himself snore Mark Twain, Western koan
Lecture 8 review for-loops nested for-loops
Can the for-loops be even cooler? Hmm…. What about looping over characters… What if we want to go through alphabet and do something?
Ordinal types Ordinal types are the ones which are countable For example integers, characters, boolean but not reals.
Ordinal types Orinal types have concept of successor and predecessor succ( ‘A’ ) is (major wow) ‘B’ pred( ‘A’ ) is (major MOM!) Lets see more detailed example
Ordinal types succ( pred( ‘A’ ) ) chr( ord( ‘A’ ) ) succ( TRUE ) pred( TRUE )
Characters as loop counter! Running through the alphabet: for cMyChar := ‘a’ to ‘z’ do Lets see examples
You can have as many loops as you want in any program
Standard Pascal functions We need to be able to do basic math sqr- squares a number sqrt- takes square root of a number abs- absolute value of a number exp- e^x ln- natural logarithm (base e) cos- cosine sin- sine * for sin & cos argument is considered to be in radians
Turbo Pascal built in functions upcase - gets upper case version of a character frac - gets fractional part of a real int - gets integer part of a real random - gets random number in the range [ 0, 1 ] random(N) - gets random number in the range [ 0, N-1 ]
Why do we need random??? A lot of different applications Lets look at some (fun) examples
Homework Read the rest of the Chapter 5 Program 3 Page 204 #15 a,b due Monday, October 19th
Programs from this lecture Ordinal types Alphabet 1 letter at a time GenX Standard Pascal functions TP functions Random stars Dice game