Traversing Linear Arrays

Let A be a collection of data elements stored in the memory of the computer. Suppose we want to print the content of each element of A or suppose we want to count the number of elements of A with given property. This can be accomplished by traversing A, that is, by accessing and processing (frequently called visiting) each element of An exactly once.

The following algorithm traverses a linear array:


Algorithm: (Traversing a linear array.)

Here, A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS to each element of A.

Step I: [Initialize counter] Set I:= LB.
Step II: Repeat steps 3 and 4 while I<= UB:
Step III: Apply PROCESS to A. [Visit element.]
Step IV: Set I:= I+1. [Increase counter.]
[End of step 2 loop.]
Step V: Exit.
Powered by Blogger.