193k views
3 votes
Consider an algorithm that uses only assignment statements that replaces the quadruple (w, x, y, z) with (x, y, z, w). What is the minimum number of assignment statements needed?

1 Answer

3 votes

Answer:

The minimum number of assignment statements needed is 5

Explanation:

To write the algorithm, we apply the strategy of interchanging the values of variables in the assignment statements.

Assume "tmp" is the new variable, let assign tmp to w

The algorithm is:

Procedure exchange (w,x,y,z: integers)

tmp := w

w := x

x := y

y := z

z := tmp

return (w,x,y,z)

end

From the algorithm, it is obvious that there will be a minimum of 5 assignment statements needed.

User Kareem Dabbeet
by
5.5k points