Page 56 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 56

Unit 4: Arrays




          4.   [Increase counter] Set K:= K + 1                                                 Notes
               [End of Step 2 loop]
          5.   Exit.
          We also state an alternative form of the algorithm which uses a repeat-for loop instead of the
          repeat-while loop. This algorithm traverses a linear array LA with lower bound LB and upper
          bound UB.

          1.   Repeat for K = LB to UB:
               Apply PROCESS to LA[K].
               [End of loop]
          2.   Exit.


                 Example: Again consider the previous one [example 4.2] array AUTO, which records the
          number of automobiles sold each year from 1932 through 1984. Each of the following algorithms
          carry out the given operation involves traversing AUTO.

          1.   Find the number NUM of years during which more than 300 automobiles were sold.
               (a)  [Initialization Step] Set NUM := 0.
               (b)  Repeat for K = 1932 to 1984:
                    If AUTO[K] > 300, then: Set NUM := NUM + 1
                    [End of loop]

               (c)  Return
          2.   Print each year and the number of automobiles sold in that year.
               (a)  Repeat for K = 1932 to 1984:

                    Write: K, AUTO[K].
                    [End loop]
               (b)  Return.

          Self Assessment

          Fill in the blanks:
          1.   The array forms a .................... list in memory.

          2.   .................... array that may be defined as a finite ordered set of homogeneous elements,
               which is stored in contiguous memory locations.
          3.   The amount of .................... required to hold an array is directly related to its type and size.

          4.   The first array index value is referred to as its ....................
          5.   Expression indicates the number of ....................  in the array.

          4.2 Multidimensional Arrays

          Suppose that you are writing a chess-playing program. A chessboard is an 8-by-8 grid. What
          data structure would you use to represent it? You could use an array that has a chessboard-like



                                           LOVELY PROFESSIONAL UNIVERSITY                                   49
   51   52   53   54   55   56   57   58   59   60   61