Introduction to Programming

X52_9239_003

Summer 98

 

Homework 4 - Solution

 

 

Problem #6, p. 177:

assign Count the value 1;

repeat (assign Count the value Count+1;

print the value assigned to Count)

until (Count >= 6)

 

Problem #9, p. 178:

A proposed three-step algorithm written in English is as follows:

Given: Arrangement of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Step1: Scan digit arrangement from right to left

Find first pair of adjacent digits in decreasing order

If there is no such pair then

Announce "No rearrangement possible"

else

Save location of left most digit in pair

Step2: Order all digits located below the location identified in

Step 1 in increasing order

Step3: Scan sublist of digits ordered as part of Step 2 from left to right

Find digit immediately bigger than the one at

the location saved in Step 1

Swap digit just found with the one at the location saved in Step 1

Output: Next larger value obtained via a rearrangement of the input list

 

A more detailed algorithm written in pseudocode follows:

In the proposed algorithm, digit positions start at zero from the rightmost digit.

The arrangement of digits is provided as an input list to our "Rearrange" procedure.

We introduce a "Swap" procedure to interchange the digit values of two distinct positions.

We assume the following function is available:

function d(position): return the value of the digit located at "position"

Comments start with "//" and terminate at the end of the line on which the "//" appears.

procedure Swap(PositionA, PositionB)

assign Safe the value d(PositionA);

assign d(PositionA) the value d(PositionB);

assign d(PositionB) the value Safe;

procedure Rearrange(List)

// Step1

assign Position the value 0;

while (d(Position) < d(Position+1)) do (

assign Position the value Position+1;

if (Position = 9)

then (Announce there is no possible rearrangement;

Terminate program)

}

assign Position1 the value Position+1;

// Step2 - Requires position1 from Step1

assign Position2 the value 1;

assign Position the value Position1-Position2;

while (Position2 <= Position1-1) do (

while (Position > 0) do (

if (d(Position1-Position2)> d(Position-1))

then (Swap(Position1-Position2, Position-1));

assign Position the value Position-1;

)

assign Position2 the value Position2+1;

assign Position the value Position1-position2;

)

// Step3 - Requires position1 from Step1 and Step 2's

ordered sublist from position1-1 downto 0

assign Position the value Position1-1

while(d(Position1) > d(Position)) do (

assign Position the value Position-1;

)

Swap(Position1, Position);

 

An example run of the proposed algorithm:

Given: 5647302981

Step 1: Position1 is set to 3

Step 2: List rearranged as 5647302189

Step 3: List rearranged as 5647308129

Problem #15, p. 178:

In the following abs( ) is a function that computes the absolute value of its argument.

Start following algorithm with input values 34 and 89

procedure MysteryWrite (Last, Current)

if (Current > 0)

then (print the value assigned to Current;

assign Temp the value of abs(Current-Last);

apply MysteryWrite to the values Current and Temp)

Problem #28, p. 179:

function Factorial ( n)

if ( n = 0)

then (return 1);

else (return n*Factorial (n-1));

 

Problem #31, p. 179:

Let us name the individual pegs as A, B, and C. We assume that the initial tower is on peg A.

As mentioned in the problem specification, the sequence of steps required to transfer a tower of 1 ring from tower A to tower B is trivial.

The sequence of steps required to transfer a tower of 2 rings from tower A to tower B is as follows:

A -> C

A -> B

C-> B

The sequence of steps required to transfer a tower of 3 rings from tower A to tower B is as follows:

A -> B

A -> C

B -> C

A -> B

C -> A

C -> B

A -> B

For N rings, the problem can be decomposed as follows:

    1. Transfer a tower of N-1 rings from tower A to tower C.
    2. Transfer the largest ring left on tower A to tower B.
    3. Transfer the tower of N-1 rings from tower C to tower B.

The corresponding recursive algorithm expressed in pseudocode is as follows. To transfer a tower of N rings from tower A to tower B, call this procedure with N, A, B as input.

We use a function TowerLeftOver(Tower, Tower2) which returns the name of the third tower which is not being passed as a parameter (this makes our algorithm more generic and not restricted to moving a tower from peg A to peg B).

procedure TowerOfHanoiRec(N, Tower1, Tower2)

if (N = 1)

then (terminate program)

else (

Tower3 = TowerLeftOver(Tower1, Tower2);

TowerOfHanoi(N-1, Tower1, Tower3);

Transfer ring left on Tower1 to Tower2;

TowerOfHanoi(N-1, Tower3, Tower2);

)

 

Problem #32, p. 180:

To start the non recursive algorithm call the following procedure by passing as input the location of the tower on which the rings are stacked initially (e.g., 4, 8, or 12).

procedure TowerOfHanoi (Tower1)

Move to Tower1;

assign RingNumber the value of the top ring of Tower1;

while (not done) do (

while (MoveRing (Tower1, RingNumber) = OK

and there is a ring left on Tower1) do (

assign RingNumber the value of top ring of Tower1

)

Move to the peg with the largest movable ring;

)

The boolean function MoveRing can be defined as follows:

function MoveRing (Tower, RingNumber)

if (RingNumber is odd)

then (move ring clockwise from Tower as long as that move

does not place a ring on a smaller one)

else (move ring counterclockwise from Tower as long as

that move does not place a ring on a smaller one)

Note that the fact that there are three rings which can only be moved in one direction guarantees that the algorithm will not return a ring to a location it immediately came from.

 

Problem #42, p.180

a)

Preconditions:

Input consists of a list of one or more numbers sorted

in increasing order of magnitude, and the value to be

searched for in the list

Loop invariant:

Each time the loop body is completed, TargetValue

is greater than all the numbers in the list up to but not

including the test entry

Termination condition:

TargetValue <= test entry

Based on the loop invariant, there is no value in the list prior

to the test entry value that causes the termination condition

to occur. Upon termination, TargetValue is either less than

the test entry last selected in which case the search fails,

or equal to test entry in which case the search succeeds.

b)

The while loop in Fig. 4.7 will terminate since there are

a finite number of elements in the list. In the worse case,

TargetValue is larger than the top element in the list.