### Teaching algorithm-related topics

• Algorithm: Level of comprehension of a Programming Language

• Different levels of language comprehension: (i.e., development stages of a programmer)

• Syntax

 What is a correct statement (= sentence in a programming language)

• Semantics

 What is the meaning of a (correct) statement

• Statement construction

 How to construct the appropriate statement in a given situation

• Atomic problem solving

• Specifying the steps to solve an atomic problem

• Atomic problems:

 Find the minimum Find the GCD ...

• Complex problem solving

• I teach the top-down problem solving technique

 Solve a complex problem = solve a number of smaller easier problems

• Example: format input text into 2 columns, right adjusted

• Input text:

 Your browser does not support the &amp;amp;lt;iframe&amp;amp;gt; tag.

• Output text:

 Your browser does not support the &amp;amp;lt;iframe&amp;amp;gt; tag.

• My style to teach topics at various levels

• Syntax:

1. I first give illustrative examples

Example: syntax of the for-statement

 ``` int i, s; ... for (i = 1; i <= 10; i = i + 1) s = s + i; ```

2. I use historical development to clarify syntax.

The for-statement syntax in Algol 60:

 ``` for i:=1 step 1 until 10 do s := s + i; ```

3. After familiarizing student with some examples, I state the syntax in a user-friendly way (close to the examples):

Example: syntax of the for-statement

 ``` for ( statement1 ; condition ; statement2 ) statement3 ; ```

(Always be as complete as possible)

• Semantics:

• I teaching the semantic of statements by giving illustrative examples

• Example : meaning of the for-statement

 ``` for (i = 1; i <= 10; i = i + 1) s = s + i; ```

Effect:

 The variable i starts at value 1 (i = 1) and runs to the value 10 (i <= 10) with an increment step of 1 (i = i + 1) At each step, the statement s = s + i is executed once.

Then I would ask student what is the outcome of:

 ``` s = 0; for (i = 1; i <= 10; i = i + 1) s = s + i; ```

(Help them work things out by tracing the execution)

• Then re-inforce with a similar example: meaning of the for-statement

 ``` for (i = 1; i <= 10; i = i + 2) s = s + i; ```

Effect:

 The variable i starts at value 1 (i = 1) and runs to the value 10 (i <= 10) with an increment step of 2 (i = i + 2) At each step, the statement s = s + i is executed once.

• Active and passive language knowledge:

 One can understand sentences of a language without knowing how to use the language !!! (I can understand Spanish, but I can't speak it fluently)

• Teach them to use the learned statement on their own:

 I teach students how to use a statement with small problems that can be solved with the statement that is being taught Example: compute 102 + 112 + ... 202 I would ask the students to come up with the solution Example: compute the sum of the even numbers between 10 and 20 I would ask the students to come up with the solution

• Next step: Atomic (small) problem solving

• I first make it clear that computers solve a problem differently than humans

• I use examples to illustrate this difference:

 Find the maximum in: 2, 6, 9, 8, 9999, 1, 3, 7, 5, 4

• A human look at all the numbers at once and will immediately point out that the answer is 9999 (we exploit the length of a number without looking at their values !)

• A computer can only compare 2 numbers at a time

 how would you solve the problem if you can only look at 2 values at a time

• Keys to keep an algorithm clear:

 Use psuedo code in decribing the solution Sometimes, I even sacrifice correctness for clarity I would have an initial solution that omits some non-essential steps - just to keep the focus on the part of the algorithm that is essential

E.g.: find the maximum

 Initialize max = 0 (this is not completely correct, but it gives the students something to compare against and kick-starts the algorithm) Compare every number against max Switch value if the value is larger than max

Then I would translate the psuedo code into a program: (in several steps if necessary)

 ``` max = 0; // This is not completely correct for (i = 0; i <= n ; i = i + 1) { Check value number[i] Switch value if the value is larger than max } max = 0; for (i = 0; i <= n ; i = i + 1) { if ( number[i] > max ) max = number[i]; } ```

• Afterwards, I'll should then an example (all negative values) that finds the wrong maximum and fix the initialization setp.

• Complex problem solving

• This requires creativity from the student's part...

• I can only stimulate the student's imagination; I don't think this can be taught

• A complex problem is solved by:

 Breaking the complex problem into smaller less complex problem This requires creativity ....

• The way I teach students on how to break a complex problem apart is to ask:

 What would you do ? Specify the steps that YOU would do in minute detail

• Example: text formatting sub-problems

The smaller problems are:

• Find the words that fits on one line

This step will transform the original input into:

 ```WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, .... ```

• Right-justify one line.

This step will transform the result into:

 ```WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, .... ```

• Print result in 2 column format.

(After breaking the problem into smaller problems, solve each one separately)