The following notes supplement the material in Chapter 4.
Algorithms:
Definition
Figure 4.1 defines algorithms thus:
We are interested in algorithms that may be ultimately implemented on a computer. You may think of these algorithms as a way of documenting the solution of a problem in an unambiguous way so that it may be readily implemented on a computer (or that it may be readily understood by someone else).
Pseudocode:
The term Pseudocode is often used to describe semi-formal languages that are used to document algorithms. Pseudocode is considered semi-formal because it is a language somewhere between natural language and a programming language. That is, the syntax of a few basic algorithmic constructs is clearly defined and must be used when these constructs are to be expressed but, where these constructs are not needed, we may freely use natural language.
Following are the basic constructs that will be defined for our Pseudocode:
Assignment refers to the case where we assign a value to a variable. The general form for the assignment construct is:
variable := expression
That is, the expression on the right hand side (RHS) is evaluated and its value assigned to the variable named on the left (LHS). See the following example:
totalpoints := quiz1 + quiz2 + midterm + final + 10
Selection is used to refer to the case where there is a choice between one action and another given the truth value of some condition. The general form for the selection construct is:
if condition then t-action else f-action endif
That is, if condition is true then the actions denoted t-action will be executed otherwise f-action will be executed. See the following example:
if (x > 2) then y := x + 1 else y := x - 1 y := 3*x endif
We use the following general form when we are only concerned with specifying an action when condition is true.
if condition then t-action endif
Iteration (or Repetition) is used to refer to the case where we repeat a set of actions based on the truth value of some condition. The general form for the iteration construct is:
while condition do action endwhile
That is, as long as condition is true then action will be repeated. See the following example:
i := 2 while (i > 0) do s := s + i i := i - 1 endwhile
A Procedure is a predefined set of actions that has been named. You may think of procedures as algorithms that have already been defined and have been given names so that they may be referenced by other algorithms. The general form therefore has a definition part as well as an invocation part.
Definition:
procedure name(parameters) action endprocedure
Invocation:
execute name(parameters)
That is, we define a procedure by giving the predefined set of actions a name and parameters and we invoke the procedure by executing it. See the following example:
procedure divide(a, d, q, r) r := a q := 0 while (r >= d) do r := r - d q := q + 1 endwhile endprocedure execute dvide(10, 4, q, r)
We elaborate on the terms constant variable, expression, condition, action, and parameter below.
Problems:
The following problems are like those you should expect for Quiz #6.
x := 1 y := 0 if (x > 0) then if (y > 0) then z := 1 else z := -1 endif else z := 0 endifWhat is the value of z after execution.
Soln: Initially x is assigned the value 1 and y the value 0. As a result, the condition (x > 0) is true and so the action which is executed is the nested if where the condition (y > 0) is false. Hence z is assigned the value -1.
i := 1 s := 0 while (i < 4) do s := s + i i := i + 1 endwhileWhat is the value of s after execution.
Soln: Let us use the following execution trace:
Iteration # ----------------------------- Initial | 1 | 2 | 3 ============================= i: 1 | 2 | 3 | 4 ----------------------------- s: 0 | 1 | 3 | 6 -----------------------------Since i is initially set to 1 the condition (i < 4) is true. Since s is initially set to 0 the first action in the body of the while increments s to 1 and the second action increments i to 2 and so at the end of the first iteration of the loop s has value 1 and i has value 2. The condition (i < 4) is therefore still true and so the actions in the body of the loop are again executed. This continues until at the end of the third iteration s is 6 and i is 4. At this point the condition (i < 4) is no longer true and so the loop ends. The value of s after execution is therefore 6.
x := 2 y := 1 s := 0 while (x not = 4) do while (y not = 3) do s := s + y y := y + 1 endwhile y := 1 x := x + 1 endwhileWhat is the value of s after execution.
Soln: In this case we have a nested loop and so we must keep track of two loops, the inner loop and the outer loop. The execution trace follows:
Iteration # ---------------------------------------------------- Outer | 1 | 2 ---------------------------------------------------- Init Inner | 1 | 2 | | 1 | 2 | ==================================================== x: 2 | | 3 | | 4 ---------------------------------------------------- y: 1 | 2 | 3 | 1 | 2 | 3 | 1 ---------------------------------------------------- s: 0 | 1 | 3 | | 4 | 6 | ----------------------------------------------------Hence the value of s after execution is therefore 6.