Sunday, November 16, 2014

Arrays

Arrays are indexed list of elements of the same data type.  They are categorized into one dimensional and two dimensional.  One dimensional arrays are called vectors; two dimensional arrays are called matrices.

One dimensional array
-a structured collection of components all of the same type, that is given a single name.  Each component is accessed by an index that indicates the components' position within the collection.

Arrays require three steps:

1. Declare the array
     * Command to declare the array
     * State the name of the array
     * State the size of the array
     * State the data type of the array
     * Example:

DIM num (1 to 5) as integer
     OR
DIM sname (1 to 3) as string

2. Attach the loop variable to the array wherever it is used in the algorithm
3. Output an indexed element

Example:

Dim sname (1 to 3) as string
for x = 1 to 3 do
   print "enter the name of a classmate"
   input sname$(x)
next
print "the second name entered is", sname$(2)
input key

This algorithm is used to enter the names of three classmates, store them in an indexed order as they are entered, and output the second entry.

No comments:

Post a Comment