Data Structures and Algorithms




      Processing
      Arrays

Arrays
   @     Often advantageous for a user to store several
         values for the same variable in the internal
         memory of the computer because it decreases
         processing time.
   @     This multiple storage means there has to be
         more than one memory location in the
         computer for each variable name.
   @     When more than one memory location is
         designated for a single variable, it is called
         an array.

Static Arrays
   @     This means that once the computer is told
         how many locations to save, that number
         cannot be changed unless the instruction is
         changed.
Processing Arrays                                      *Property of STI
                                                          Page 1 of 17
Data Structures and Algorithms




        Processing
        Arrays

Dynamic Arrays
 @     When using dynamic arrays, the programmer
       designates the number of array locations as a
       variable, which can be expanded or contracted
       during the execution of the solution.

Base-Zero System
 @     Because computers are zero-based, for
       counting purposes, many programming
       languages are also zero-based.
 @     This means that the first array element is
       numbered zero and not one.

Base-One System
 @     Base one is easier for the programmer to
       understand since the first element will have
       an index of 1.
Processing Arrays                                    *Property of STI
                                                        Page 2 of 17
Data Structures and Algorithms




        Processing
        Arrays

Base-Zero Versus Base-One Arrays




Processing Arrays                                 *Property of STI
                                                     Page 3 of 17
Data Structures and Algorithms




        Processing
        Arrays


One-Dimensional Arrays




Processing Arrays                       *Property of STI
                                           Page 4 of 17
Data Structures and Algorithms




        Processing
        Arrays


Parallel Arrays




Processing Arrays                   *Property of STI
                                       Page 5 of 17
Data Structures and Algorithms




        Processing
        Arrays

Entering Data into an Array

               Algorithm            Flowchart

                                            A




       LOOP:R = 1 TO N STEP 1
                                            R
                                      1           N
          ENTER A(R)
                                            1

       LOOP-END:R

                                          ENTER
                                           A(R)




        R = Counter
                                            R
        N = Number of elements in
               the array

        A(R) = Element R                    B
               in the A array

Processing Arrays                                              *Property of STI
                                                                  Page 6 of 17
Data Structures and Algorithms




        Processing
        Arrays
                    Algorithm         Flowchart
                                                 A




                                                R=0
         1. R = 0
         2. REPEAT                                   REPEAT



                     R = R+1                  R=R+1

                     ENTER A(R)

                    UNTIL A(R) = -1           ENTER
                                               A(R)


         *3. N = R-1
                                      F        UNTIL
                                              A(R) = -1



                                                      T

                                          *
                                              N=R-1




                                                 B

Processing Arrays                                                *Property of STI
                                                                    Page 7 of 17
Data Structures and Algorithms




        Processing
        Arrays
                    Algorithm     Flowchart
                                            A




           1. R = 1                       R=1

           2. ENTER A(R)
           3. WHILE A(R) <> -1
                                         ENTER
                                          A(R)
                     R = R+1
                     ENTER A(R)
                                         WHILE       F
                                        A(R) <> -1
                    WHILE - END

           *4. N = R-1
                                        R=R+1




                                         ENTER
                                          A(R)




                                    *
                                        N=R+1




                                            B




Processing Arrays                                         *Property of STI
                                                             Page 8 of 17
Data Structures and Algorithms




        Processing
        Arrays

Printing an Array
                    Algorithm   Flowchart


                                      A

     LOOP: R=1 TO N STEP 1
       PRINT A(R)                     R
                                 1           N
                                       1
     LOOP-END: R


                                     PRINT
     R = Element number               A(R)


     N = Total number
          of elements
                                      R

     A(R) = Rth element of
           the A array
                                      B




Processing Arrays                                *Property of STI
                                                    Page 9 of 17
Data Structures and Algorithms




        Processing
        Arrays

Accumulating the Elements of an Array
                    Algorithm                  Flowchart
                                                       A
         LOOP:R = 1 TO N STEP 1
           SUM = SUM + A(R)
                                                       R
                                                 1          N
         LOOP-END: R                                   1




         N = Number of elements                  SUM = SUM
                                                   + A(R)
         R = Element number
         SUM = Sum of the                              R
               elements of A
         A(R) = Rth element of the
                array                                  B


               TEST:                    R       SUM
                         A
                     1   2            1 2 3     2 6 12
                                     4 5 6 7   20 30 42
                     2   4
                     3   6
                     4   8              N
                     5   10
                                        6
                     6   12



Processing Arrays                                                   *Property of STI
                                                                      Page 10 of 17
Data Structures and Algorithms




        Processing
        Arrays

Two-Dimensional Arrays
  @ A two-dimensional array is a block of memory
       locations associated with a single memory variable
       name and designated by row and column numbers.




Processing Arrays                                       *Property of STI
                                                          Page 11 of 17
Data Structures and Algorithms




        Processing
        Arrays

Loading a Two-Dimensional Array

                                     Row by Row

@ You load a two-       Data Block
                                            A


  dimensional array        1
                                                                     Array

  with nested loops.       2                R            A
                           3          1             3
  The data are
                                                             C
                                                                 1     2   3    4
                           4                1           R

  normally loaded          5                                 1   1     2   3    4
                           6                                 2   5     6   7    8
  row by row. When         7                C                3   9    10 11 12

  you load the data        8
                           9
                                      1
                                            1
                                                    4
                                                             The row remains
  row by row, the          10                                 constant as the
                                                              column varies.
                           11
  outer         loop       12
                                          ENTER
  represents the row,                     A(R, C)

  and the inner loop
  represents the
                                            C
  column.                                                        C = Column




                                            R                         R = Row




                                            B




Processing Arrays                                                    *Property of STI
                                                                       Page 12 of 17
Data Structures and Algorithms




        Processing
        Arrays

 Printing a Two-Dimensional Array
                                      A




                                     PRINT
                                    COLUMN
                                   HEADINGS




                                      R
                               1          NR
                                      1




                               PRINT ROW
                               HEADING (R)




                                      C
                               1          NC
                                      1



                               PRINT A(R,C)
                               W/O CURSOR

      R = Row                    RETURN




      NR = Number of rows             C




      C = Column                   RETURN
                                   CURSOR




      NC = Number of columns          R




                                      B


Processing Arrays                                             *Property of STI
                                                                Page 13 of 17
Data Structures and Algorithms




        Processing
        Arrays

Accumulating the Rows and Columns of a Two-
Dimensional Array




     @ Column 5 holds the sum of each of the rows
     @ Row 4 holds the sum of each of the columns
     @ A (4,5) holds the grand total



Processing Arrays                                    *Property of STI
                                                       Page 14 of 17
Data Structures and Algorithms




        Processing
        Arrays

                    Algorithm      Flowchart
                                           A




     LOOP:R = 1 TO NR STEP 1               R
                                     1         NR
                                           1
     LOOP: C = 1 TO NC STEP 1
       A(R,NC + 1) = A(R,NC + 1)           C
                                     1         NC
           + A(R,C)                        1
       A(NR + 1,C) = A(NR + 1,C)
           + A(R,C)                   A(R, NC + 1) =
                                      A(R, NC + 1) +
                                         A(R,C)
     LOOP-END: C

                                      A(NR + 1,C)=
     A(NR + 1,NC + 1) =               A(NR + 1,C) +
                                         A(R,C)
      A(NR + 1, NC + 1)
          +A(R, NC + 1)
                                          C
     LOOP-END: R
                                     A(NR + 1,NC + 1)
                                    =A(NR + 1, NC + 1)
                                      +A(R, NC + 1)




                                           R




                                           B



Processing Arrays                                      *Property of STI
                                                         Page 15 of 17
Data Structures and Algorithms




        Processing
        Arrays

Multidimensional Arrays
In some cases there is a need for arrays with a third or
even a fourth dimension. These arrays are called
multidimensional arrays.


Advantages :
     @ Facilitate   an
       understanding
       of the data
     @ Improve     the
       readability of
       algorithms
     @ Facilitate
       processing




Processing Arrays                                      *Property of STI
                                                         Page 16 of 17
Data Structures and Algorithms




        Processing
        Arrays
Table Look-up Technique
 @ A common application for arrays is using a value to look up another
   value in a table. A one-dimensional array would be used if the
   element number can be utilized as the given value. A two-
   dimensional array with two columns would be used if the element
   number cannot be utilized.

              element   DAYS     Algorithm                FLOWCHART:


                1        31    1. ENTER MONTH
                               2. DAYS_OF_THE_MONTH =          START
                               DAYS(MONTH)
                2        28
                               3. PRINT DAYS_OF_MONTH
                               4. END
                3        31
                                                               ENTER
                4        30                                    MONTH


                5        31

                6        30                                DAYS_OF_THE_
                                                              MONTH =
                                                            DAYS(MONTH)
                7        31

                8        31
                                                               PRINT
                                                             DAYS_OF_
                9        30                                   MONTH


                10       31

                11       30
                                                                END

                12       31


Processing Arrays                                                       *Property of STI
                                                                          Page 17 of 17

9 processing arrays

  • 1.
    Data Structures andAlgorithms Processing Arrays Arrays @ Often advantageous for a user to store several values for the same variable in the internal memory of the computer because it decreases processing time. @ This multiple storage means there has to be more than one memory location in the computer for each variable name. @ When more than one memory location is designated for a single variable, it is called an array. Static Arrays @ This means that once the computer is told how many locations to save, that number cannot be changed unless the instruction is changed. Processing Arrays *Property of STI Page 1 of 17
  • 2.
    Data Structures andAlgorithms Processing Arrays Dynamic Arrays @ When using dynamic arrays, the programmer designates the number of array locations as a variable, which can be expanded or contracted during the execution of the solution. Base-Zero System @ Because computers are zero-based, for counting purposes, many programming languages are also zero-based. @ This means that the first array element is numbered zero and not one. Base-One System @ Base one is easier for the programmer to understand since the first element will have an index of 1. Processing Arrays *Property of STI Page 2 of 17
  • 3.
    Data Structures andAlgorithms Processing Arrays Base-Zero Versus Base-One Arrays Processing Arrays *Property of STI Page 3 of 17
  • 4.
    Data Structures andAlgorithms Processing Arrays One-Dimensional Arrays Processing Arrays *Property of STI Page 4 of 17
  • 5.
    Data Structures andAlgorithms Processing Arrays Parallel Arrays Processing Arrays *Property of STI Page 5 of 17
  • 6.
    Data Structures andAlgorithms Processing Arrays Entering Data into an Array Algorithm Flowchart A LOOP:R = 1 TO N STEP 1 R 1 N ENTER A(R) 1 LOOP-END:R ENTER A(R) R = Counter R N = Number of elements in the array A(R) = Element R B in the A array Processing Arrays *Property of STI Page 6 of 17
  • 7.
    Data Structures andAlgorithms Processing Arrays Algorithm Flowchart A R=0 1. R = 0 2. REPEAT REPEAT R = R+1 R=R+1 ENTER A(R) UNTIL A(R) = -1 ENTER A(R) *3. N = R-1 F UNTIL A(R) = -1 T * N=R-1 B Processing Arrays *Property of STI Page 7 of 17
  • 8.
    Data Structures andAlgorithms Processing Arrays Algorithm Flowchart A 1. R = 1 R=1 2. ENTER A(R) 3. WHILE A(R) <> -1 ENTER A(R) R = R+1 ENTER A(R) WHILE F A(R) <> -1 WHILE - END *4. N = R-1 R=R+1 ENTER A(R) * N=R+1 B Processing Arrays *Property of STI Page 8 of 17
  • 9.
    Data Structures andAlgorithms Processing Arrays Printing an Array Algorithm Flowchart A LOOP: R=1 TO N STEP 1 PRINT A(R) R 1 N 1 LOOP-END: R PRINT R = Element number A(R) N = Total number of elements R A(R) = Rth element of the A array B Processing Arrays *Property of STI Page 9 of 17
  • 10.
    Data Structures andAlgorithms Processing Arrays Accumulating the Elements of an Array Algorithm Flowchart A LOOP:R = 1 TO N STEP 1 SUM = SUM + A(R) R 1 N LOOP-END: R 1 N = Number of elements SUM = SUM + A(R) R = Element number SUM = Sum of the R elements of A A(R) = Rth element of the array B TEST: R SUM A 1 2 1 2 3 2 6 12 4 5 6 7 20 30 42 2 4 3 6 4 8 N 5 10 6 6 12 Processing Arrays *Property of STI Page 10 of 17
  • 11.
    Data Structures andAlgorithms Processing Arrays Two-Dimensional Arrays @ A two-dimensional array is a block of memory locations associated with a single memory variable name and designated by row and column numbers. Processing Arrays *Property of STI Page 11 of 17
  • 12.
    Data Structures andAlgorithms Processing Arrays Loading a Two-Dimensional Array Row by Row @ You load a two- Data Block A dimensional array 1 Array with nested loops. 2 R A 3 1 3 The data are C 1 2 3 4 4 1 R normally loaded 5 1 1 2 3 4 6 2 5 6 7 8 row by row. When 7 C 3 9 10 11 12 you load the data 8 9 1 1 4 The row remains row by row, the 10 constant as the column varies. 11 outer loop 12 ENTER represents the row, A(R, C) and the inner loop represents the C column. C = Column R R = Row B Processing Arrays *Property of STI Page 12 of 17
  • 13.
    Data Structures andAlgorithms Processing Arrays Printing a Two-Dimensional Array A PRINT COLUMN HEADINGS R 1 NR 1 PRINT ROW HEADING (R) C 1 NC 1 PRINT A(R,C) W/O CURSOR R = Row RETURN NR = Number of rows C C = Column RETURN CURSOR NC = Number of columns R B Processing Arrays *Property of STI Page 13 of 17
  • 14.
    Data Structures andAlgorithms Processing Arrays Accumulating the Rows and Columns of a Two- Dimensional Array @ Column 5 holds the sum of each of the rows @ Row 4 holds the sum of each of the columns @ A (4,5) holds the grand total Processing Arrays *Property of STI Page 14 of 17
  • 15.
    Data Structures andAlgorithms Processing Arrays Algorithm Flowchart A LOOP:R = 1 TO NR STEP 1 R 1 NR 1 LOOP: C = 1 TO NC STEP 1 A(R,NC + 1) = A(R,NC + 1) C 1 NC + A(R,C) 1 A(NR + 1,C) = A(NR + 1,C) + A(R,C) A(R, NC + 1) = A(R, NC + 1) + A(R,C) LOOP-END: C A(NR + 1,C)= A(NR + 1,NC + 1) = A(NR + 1,C) + A(R,C) A(NR + 1, NC + 1) +A(R, NC + 1) C LOOP-END: R A(NR + 1,NC + 1) =A(NR + 1, NC + 1) +A(R, NC + 1) R B Processing Arrays *Property of STI Page 15 of 17
  • 16.
    Data Structures andAlgorithms Processing Arrays Multidimensional Arrays In some cases there is a need for arrays with a third or even a fourth dimension. These arrays are called multidimensional arrays. Advantages : @ Facilitate an understanding of the data @ Improve the readability of algorithms @ Facilitate processing Processing Arrays *Property of STI Page 16 of 17
  • 17.
    Data Structures andAlgorithms Processing Arrays Table Look-up Technique @ A common application for arrays is using a value to look up another value in a table. A one-dimensional array would be used if the element number can be utilized as the given value. A two- dimensional array with two columns would be used if the element number cannot be utilized. element DAYS Algorithm FLOWCHART: 1 31 1. ENTER MONTH 2. DAYS_OF_THE_MONTH = START DAYS(MONTH) 2 28 3. PRINT DAYS_OF_MONTH 4. END 3 31 ENTER 4 30 MONTH 5 31 6 30 DAYS_OF_THE_ MONTH = DAYS(MONTH) 7 31 8 31 PRINT DAYS_OF_ 9 30 MONTH 10 31 11 30 END 12 31 Processing Arrays *Property of STI Page 17 of 17