Read a File Into Memory and Buffer Contents
CMPUT 201
Vertical Strip
Running, Compiling
nddw Find Double
Word
Python vs C
Test Programs
Makeles
Compiler Warnings
Other
Examples
Unix Component
Model
popen: Child
Processes
Buffer File Contents
Pointers
#inc
Python vs C
CMPUT 201
Curly brackets cfw_ instead of indentation
Vertical Strip
Running, Compiling
nddw Find Double
Word
Python vs C
Test Programs
Makeles
Compiler Warnings
Other
Examples
Unix Component
Model
popen: Child
Processes
Buffer File Contents
Po
Find Double Word (4)
CMPUT 201
Vertical Strip
Running, Compiling
nddw Find Double
Word
Python vs C
Test Programs
Makeles
Compiler Warnings
Other
Examples
Unix Component
Model
popen: Child
Processes
Buffer File Contents
Pointers
int main( int argc, char *
Computing Science (CMPUT) 201
Practical Programming Methodology
Neil Burch
Department of Computing Science
University of Alberta
[email protected]
Based on material by Paul Lu, modied by Jacqueline Smith.
Some material from other instructors (e.g., Piotr
Computing Science (CMPUT) 201
Practical Programming Methodology
Neil Burch
Department of Computing Science
University of Alberta
[email protected]
Based on material by Paul Lu, modied by Jacqueline Smith.
Some material from other instructors (e.g., Piotr
CMPUT 272 Fall 2015
Assignment 2
Posted Thurs Sept 17
Due Thurs Oct 1 at 10:30AM in your seminar sections drop box across from CSC 145
This is the mandatory assignment cover sheet;
without it, your work will not be marked.
Submitting student:
Seminar sect
Example 2: Prove that for any natural numbers n >= 1, if x is a real
number such that 1 + x > 0 then (1 + x)^n >= 1+nx.
. S(n) can be defined as "if x is a real number and 1 + x > 0 then
(1 + x)^n >= 1 + nx".
. We want to prove S(n), for all n >= 1. (Note
Regular Languages
- We have shown that a language is accepted by a FSA iff there is a R.E. for it.
- A language is called regular iff it is denoted by some R.E., or equivalently,
iff it is accepted by a FSA (deterministic or non-deterministic).
- This pro
Proving Logical Equivalences
Prove or disprove each of the logical equivalences below, without
using truth tables:
a) p -> (q /\ r) LEQV (p -> q) /\ (p -> r)
answer: p -> (q /\ r)
LEQV ~p \/ (q /\ r)
(-> law)
LEQV (~p \/ q) /\ (~p \/ r)
(distributivity of
Propositional Logic
Notation for expressing concepts precisely, and formalism for reasoning.
"Propositions" are statements, which are either true or false,
as opposed to other kinds of English sentences (e.g., commands, questions).
Example: "David works h
Propositional Formulas:
- Let PV be a set of propositional variables. The set of propositional formulas
PF is the smallest set such that:
. any variable in PV is in PF.
. if p1 and p2 are in PF then so do the following expressions: ~p1, (p1 /\ p2),
(p1 \/
Structures, valuations, interpretations:
- A "structure" S for a first-order language L consists of:
. a domain D (nonempty)
. for each constant symbol c of L, an element c^S of D
. for each n-ary predicate symbol A of L, a relation A^S over D^n
. if L co
Termination Questions
Termination: Let E = l + 1 - i. E is definitely an integer because l and i
are both integers. Sine f <= l by the precondition: E_0 = l + 1 - i_0 =
l + 1 - f >= 1, and since i_k <= l at the begining of each iteration of the
loop, i_cf
RecBinSearch. Recurrence for worst-case running time of
RecBinSearch is:
cfw_4
if n = 1
T(n) = cfw_
cfw_ 7 + max cfw_ T(ceil(n/2), T(floor(n/2) + 1 if n > 1
1. Repeated substitutions. Since this is used only to get a guess, we
are allowed to make simplif
Regular Expressions
- Regular expressions describe sets of strings using a small number of
basic operations.
- The set of regular expressions (regexps) over an alphabet S is defined
as follows, assuming that S does not contain symbols "cfw_" and "e":
. cf
Test Preparation Questions
1- Let L be the set of strings over cfw_0,1 that contain both 01 and 10
as substrings.
(a) Daw the diagram of a DFSA that accepts L
(b) Give a regular expression that denotes L.
Answer: for part (a) one way is to have q0 -0-> q1
Truth value assignment (or just truth assignment):
- Gives a value (0 or 1) to each propositional variable.
- Value can be extended to all formulas using those variables through the
truth tables, by working "inside-out" (example).
- A truth assignment "sa
Advanced Finite State Automata
- Example: Come up with F.S.A. that recognizes L = cfw_ w in cfw_a,b* : w
contains an even number of a's . Use states that represent information
about string processed so far. In this case, only need to remember if
number of
Algorithm Correctness Examples
Example 1: recursive binary search
RecBinSearch(x,A,f,l)
if f = l then
if A[f] = x then
return f;
else
return 0;
end if
else
m := (f + l) div 2;
cfw_ integer division
if A[m] >= x then
return RecBinSearch(x,A,f,m);
else
ret
Termination:
- Standard method: find an expression E involving program variables such
that:
. E_k is a natural number for all k;
. for all k >= 0, E_cfw_k+1 < E_k if the loop is executed at least k+1
times (E_cfw_k+1 = E_k otherwise);
(where E_k is the va
Algorithm Complexity
- Standard measures for algorithms: space (memory) and time (speed). In
this course, we will consider only time.
- Want measure independent of one machine or implementation, and related
only to algorithm in an abstract way.
- One cand
Algorithm Complexity Tips and Examples
- Example 1: Prove 2n^2 + 6n + 2 is O(n^2).
Scratch work (NOT part of proof): for all n >= 1, 2n^2 <= 2n^2
and 6n <= 6n^2 and 2 <= 2n^2, so 2n^2 + 6n + 2 <= 2n^2 + 6n^2 + 2n^2 =
10n^2. [Alternatively, 2n^2 <= 2n^2 an
Algorithm Correctness
- We don't need just programs that usually work
they should always work correctly.
- We have to prove they produce "correct output" on "acceptable inputs", these
are expressed in terms of Postconditions and Pretconditions.
Example 1:
Closure properties of FSA-accepted languages
If we apply an operation on a language that is accepted by a FSA and
the result is also a language accepted by a FSA we say the operation
has closure property.
- Suppose L and L' are two languages accepted by t
Satisfiability and validity:
- The truth-value of a formula F under an interpretation I=(S,v) is
defined as follows:
. if F is an atomic formula P(t_1,.,t_m), where each t_i is a term
(either a variable or a constant), then F is true iff the relation
A^S
Constructing a NFSA
Example 1: Construct a NFSA that accepts the language of
(010+01)^*010(1)^*+1^*.
Answer:
Following the rules described in the notes (and lectures), we get
the following NFSA, with 13 states cfw_s,a,b,c,d,e,f,g,h,i,j,k,m, initial
state
CNF Form
We can also find a formula in CNF form which has the same truth table as
f(p,q,r) given above, and therefore is LEQV to it:
p q r | f(p,q,r)
-1 1 1 |
1
1 1 0 |
1
1 0 1 |
1
1 0 0 |
0
0 1 1 |
1
0 1 0 |
0
<= p \/ ~q \/ r
0 0 1 |
0
<= p \/ q \/ ~r
0