• No results found

1 Summary of Classes 1. 1 Summary of Classes

N/A
N/A
Protected

Academic year: 2022

Share "1 Summary of Classes 1. 1 Summary of Classes"

Copied!
12
0
0

Loading.... (view fulltext now)

Full text

(1)

1 Summary of Classes 1

1 Summary of Classes

– Classes have member variables (data) and member functions (operations on the data) – Can define constructors for initialization.

If you define any constructor, you must define the default constructor. – Use classes in assignment and in vector operations.

– Cannot use other operations like +, −, /, ∗.

– Classes can be passed as input to functions and returned.

(2)

2 Recursion 2

2 Recursion

“Defining a task in terms of itself”

Circular?

Examples:

1. Dictionary Lookup.

fustic: the wood of a large, tropical American tree Chlorophora tinctoria, of the mulberry family, yielding a light yellow dye.

Gaelic: of or pertaining to the Gaels or Gaelic.

2. Course Prerequisites.

CSCI-4320 Parallel Programming . . . . Prerequisites: CSCI-2400, CSCI-2500.

(3)

3 Dictionary Lookup 3

3 Dictionary Lookup

lookup meaning of word(recursion)

1: Get the definition for “recursion”, which will be in terms of other words;

2: Suppose that we have a set of words that we know.

3: if all the words in the definition are known then

4: We understand recursion (add recursion to our set of known words);

5: else

6: for every word that is not known do

7: Use lookup meaning of word(word) to get the meaning of the unknown word.

8: end for

9: end if

• We need to know some “simple” words (not defined in the dictionary).

• Every word needs to ultimately be definable in terms of these simple words.

• The dictionary needs to be consistent. An example of an inconsistency is man: A member of the male subset of the human species.

male: Term used for a man in the human species.

(4)

4 Course Prerequisites 4

4 Course Prerequisites

find prerequisites(foo)

1: Start a prerequisite list of courses {}.

2: Get prerequisites of foo.

3: for each prereq do

4: Add it to list if it is not already there and it is not taken.

5: if prereq is added then

6: find prerequisites(prereq)

7: end if

8: end for

• Some course must need no prerequisites - the “simple” courses.

• The prerequisites should in some sense be “simpler” than the course itself.

• The prerequisite structure needs to be consistent. An inconsistency could be DSA: Prerequisites are CS2 and Discrete Math.

Discrete Math: Prerequisites are Calc2 and DSA.

(5)

5 Course Prerequisites 5

5 Course Prerequisites

find prerequisites(foo)

1: Start a prerequisite list of courses {}.

2: Get prerequisites of foo.

3: for each prereq do

4: Add it to list if it is not already there and it is not taken.

5: if prereq is added then

6: find prerequisites(prereq)

7: end if

8: end for

9: return

CSCI-4320 Parallel Programming . . . . Prereqs: CSCI-2400,2500.

CSCI-2400 Mod. Comp. . . Prereqs: CSCI-2300, MATH-2800.

CSCI-2300 DSA . . . . Prereqs: CSCI-1200, MATH-1010,2800.

CSCI-1200 CS II . . . . Prereqs: CSCI-1100.

CSCI-1100 CS I . . . . Prereqs: None.

MATH-2800 Discrete Structures . . . . Prereqs: MATH-1010.

MATH-1010 Calculus I . . . . Prereqs: none.

(6)

6 Golden Rules of Recursion 6

6 Golden Rules of Recursion

1. Base Cases: There must be simple things you know how to do.

2. Recursive Progress: A complex thing should always be formulated in terms of simpler things.

3. Recursive Faith: In solving the complex problem, act as though the simpler problem is solved.

Dictionary Lookup Course Pre-requisites simple things known words courses with no prereqs complex things complex words courses with prereqs

simpler things ”closer” to known words ”simpler” courses – fewer prereqs

(7)

7 Solving Problems Recursively 7

7 Solving Problems Recursively

1. Identify the base cases – the problems which are easy to solve.

2. Relate the problems which are harder to simpler ones.

3. Use (2) to solve the harder problem as if the simpler one was solved – the base case takes care of the rest.

(8)

8 Two Examples – n!, xn 8

8 Two Examples – n!, xn

Compute n! for integer n ≥ 0.

f actorial(n) prod = 1

for i = 1 to n do prod ← prod ∗ i end for

Compute xn for integer n ≥ 0 and x > 0.

power(x, n) prod = 1

for i = 1 to n do prod ← prod ∗ x end for

(9)

9 Thinking Recursively 9

9 Thinking Recursively

Base Cases:

n! xn n = 0 1 1

1: factorial(n)

2: if n = 0 then

3: return 1;

4: end if

1: power(x,n)

2: if n = 0 then

3: return 1;

4: end if

What problems are harder than the base cases? n > 0.

“Simpler” problems have smaller n (closer to base case).

(10)

9 Thinking Recursively 10

Relating the more complex problem to the simpler one

n! xn

n = 0 1 1

n > 0 n! = (n − 1)! ∗ n xn = x ∗ xn−1

1: factorial(n)

2: if n = 0 then

3: return 1;

4: end if

5: return n*factorial(n-1);

1: power(x,n)

2: if n = 0 then

3: return 1;

4: end if

5: return x*power(x,n-1);

Is this circular – recursive faith: bases cases take care of “circularity” as long as recursive progress is always made.

(11)

10 Coding Recursion – if you can pseudo-code, you can code 11

10 Coding Recursion – if you can pseudo-code, you can code

n! xn

Inductive

int factorial(int n){

int i,prod=1;

for(i=1;i<=n;i++)prod*=i;

return prod;

}

double power(double x, int n){

int i;double prod=1;

for(i=1;i<=n;i++)prod*=x;

return prod;

} Recursive

int factorial_rec(int n){

if(n==0)return 1;

return n*factorial(n-1);

}

int power_rec(double x, int n){

if(n==0)return 1;

return x*power(x,n-1);

}

Code both versions of both functions. See them both work!

(12)

10 Coding Recursion – if you can pseudo-code, you can code 12

1. Reading: Chapters 1–8,10,14.

2. Think! Read!

DO, DO,

DO, DO, DO, DO, DO, DO, DO

!!!!

3. This lecture illustrates:

(i) Golden Rules of Recursion

(ii) Examples of recursion: dictionary lookup, course prereqs, n!, xn. 4. Algorithmic concept: recursion.

References

Related documents

8 Understanding Intent Based Filtering (IBF) Technology Now, the IBF AI engine does three things: (1) it analyzes what words are used in a message, (2) it analyzes how those words

New advances in HDS made by Akonia over the past year have also shown that this new technology can attain transfer rates between 200MB/s and 300MB/s, random access times of

2 On one hand, this literature addresses the causation from growth to inequality, and disputes about the Kuznets (1955) and the “augmented” Kuznets hypothesis (Milanovic,

Recognized by the World Health Organisation (WHO), Ayur-Veda has recently been undergoing an extensive revival under the auspices of the leading Vedic scientist Maharishi Mahesh

Understanding the genetic structure within KhoeSan populations is vital to better understand the genetic history of extant populations that received genetic contributions

Hørselshemmede ønsker å være på lik linje med alle andre, men bruker en person høreapparat så vil man lett kunne bli oppfattet som ”annerledes” (Kvam &amp; Tingvold,

Common nouns are words for people, animals, places, or things.. These are words

Whilst in the majority of the cases, players experiment new strategies when their payoff falls below the average profit, as predicted by the aspiration rule, we find no evidence