Thursday, 28 January 2010

Variable Arrays

Variable arrays allow the programmer to store data in much the same way as simple variables and although using more memory they are much more useful for data handling applications such as databases. By using dimensional arrays and an index pointer the programmer can load or access any element within the array. The following program shows the basic structure of a simple array, its loading  its display using a for next loop and finally  its accumulated value.

REM This program demonstrates the use a simple array
REM notice first that after the declaration of the variable 'a' are the
REM parenthisis (10) with a figure inside. This figure defines the number of
REM elements in the array

    DIM a(10)
    CLS
REM Here array elements load 0 to 5 are loaded with values for
REM demonstration values

a(0) = 1
a(1) = 500
a(2) = 1000
a(3) = 2000
a(4) = 3000
a(5) = 4000

REM the FOR NEXT loop uses the index 'i' and this value is used to point to
REM the element of the 'a' array in the print statement.

FOR i = 0 TO 5
      PRINT a(i)
NEXT i

PRINT
REM the last statement prints the addition of all the values of the 'a' array
PRINT a(0) + a(1) + a(2) + a(3) + a(4) + a(5)

Once you have written and tested this program adjust it to make it more useful by creating a program that will ask the user to enter  5 numbers then display the numbers and the calculated mean. Their are many ways that this can be achieved ranging from five input statements and five print statements using integer numbers in the the index  e.g. INPUT a(0)   INPUT a(1) PRINT a(0) PRINT a(1) etc, remembering that the array begins at zero and not one. alternatively you could think about using a for next look such as in the previous program but with an INPUT a(i) and PRINT a(i). Again using a separate variable to collect the accumulated figure you could use the following lines such as S= a(0) + a(1) + a(2) + a(3) + a(4) then the line PRINT S/5.
Alternatively you could use a FOR NEXT loop to do the same thing more efficiently with the line
S = S+ a(i). If you feel particularly adventurous you could add  functionality to ask the user how many element are to be used between a 2 and the number of elements in the array. To do this you could use a new variable to that can hold the terminating number for the index in the FOR NEXT loop such as:

INPUT "Please enter the number of elements in your dataset, Number between 2 and 100", T,

T standing for Termination. The Following FOR NEXT would be

FOR i = 0 TO T-1
     INPUT a(i)     PRINT a(i)
     S = S +a(i)
NEXT i

T-1 to account for the fact that the first element of the array is 0 and not 1 to including zero therefore means that the termination needs to be one less. Alternatively for simplicity you could discard the 0 element in the array like such:

FOR i = 1 TO T
     INPUT a(i)
     PRINT a(i)
     S = S +a(i)
NEXT i

Then to compute the mean we need to use the T variable as the divisor:

PRINT "The Average Mean is ", S / T

If you want to test out these ideas before looking at the written program don't look any further on this page.
______________________________________________________

No comments:

Post a Comment