BASIC PROGRAMMING III (ONE – DIMENSIONAL ARRAY)
ONE DIMENSIONSIONAL ARRAY
An Array is a list of variables of the same datatype. Arrays are useful for organizing multiple variables.
To create an array, use the DIM (dimension) command.
Example of Code without an Array
10 a = 5
20 b = 10
30 c = 15
40 Print a, b, c
50 END
Example of Code with an Array
10 DIM nos(3)
20 nos(1) = 5
30 nos(2) = 10
40 nos(3) = 15
50 Print nos(1), nos(2), nos(3)
List of Operations on Array
Arrays can be manipulated in the following ways:
(i) Input of an Array
This example illustrates how to store values in an array
10 DIM ages(5)
20 ages(1) = 12
30 ages(2) = 15
40 ages(3) = 11
50 ages(4) = 10
60 ages(5) = 12
(ii) Output of an Array
This example illustrates how to output values stored in an array. The code below is an
extension of the example above.
10 DIM ages(5)
20 ages(1) = 12
30 ages(2) = 15
40 ages(3) = 11
50 ages(4) = 10
60 ages(5) = 12
70 PRINT ages(1), ages(2), ages(3), ages(4), ages(5).
(iii) Arithmetic on Array
This example illustrates how to perform arithmetic operation with values stored in an array
10 DIM ages(5)
20 ages(1) = 12
30 ages(2) = 15
40 ages(3) = 11
50 ages(4) = 10
60 ages(5) = 12
70 Total = ages(1) + ages(2) + ages(3) + ages(4) + ages(5).
80 Average = Total /5
90 Print “The Average of the values in the array is”, Average
Review of Loop instruction
FOR …TO NEXT Statement
The FOR …. TO…NEXT statement is a loop statement used to repeat certain portion of a
program a number of times. Like in the program below, lines 20, 40 will be repeated 5 times
that is for five students and after that the program ends.
Example:
10 CLS
20 DIM classes(3)
30 classes(1) = 50
40 classes(2) = 23
50 classes(3) = 35
60 FOR x 1 to 3
70 PRINT “Number in class”, x; “is”; classes(x);
80 NEXT x
90 END
WHILE…WEND Statement
The WHILE…WEND command is used in a loop until a specified expression is false.
To use WHILE…WEND:
1. Place an expression after WHILE
2. Enter a list of commands
3. Place WEND at the end
Example:
10 CLS
20 Print “How Many Friends Do you have?”
30 INPUT friends
40 DIM NoOfFriends(friends)
50 x = 0
60 WHILE x <friends
70 Print “What is the Name of the “;x;
80 INPUT NoOffriends(x)
90 x = x +1
100 WEND
110 END
DIM Statement
The DIM statement can be used to create a variables including array varriables.
Example
10 DIM A as INTEGER
20 DIM array(5)
30 END
Examples using Arrays
1. Store data in a vestor of 10 integer with and without a FOR NEXT Statement.
Example without FOR NEXT
10. CLS
20 DIM array(10)
30 array(1) = 5
40 array(2) = 10
50 array(3) = 15
60 array(4) = 20
70 array(5) = 25
80 array(6) = 30
90 array(7) = 35
100 array(8) = 40
110 array(9) = 45
120 array(10) = 50
130 END
Example with FOR NEXT
10 CLS
20 DIM array(10)
30 FOR I = 1 to 10
40 array(i) = 5 * i
50 NEXT i
60 END
Example
Calculate the average of a one-dimensional array with 100 numeric values
10 DIM array(100)
20 FOR i = 1 to 100
30 array(i) = i
40 NEXT i
50 SUM = 0
60 FOR i = 1 to 100
70 SUM = SUM + array(i)
80 NEXT i
90 Average = SUM/100
100 Print “ The Average of the content of the array is “, Average
Calculate the area of 10 different rectangles with and without the WHILE WEND
statement.
Example without WHILE WEND
10 CLS
20 DIM heights(10)
30 DIM bases(10)
40 FOR x = 1 to 10
50 Print “ Enter Value for Base of Triangle”
60 INPUT bases(x)
70 Print “ Enter Value for height of triangle”
80 INPUT heights(x)
90 NEXT x
100 Print “ Calculating Area of the 10 Triangles”
110 For X = 1 to 10
120 Print “Area of Triangle”;1/2*bases(x)*heights(x);
130 NEXT X
140 END
Example with WHILE WEND
10 CLS
20 DIM heights(10)
30 DIM bases(10)
40 i = 1
50 WHILE I < = 10
60 Print “Enter Value for Base of Triangle”
70 INPUT bases(i)
80 Print “Enter Value for Base of Triangle”
70 INPUT bases(i)
80 Print “Enter Value for Height of Triangle”
90 INPUT height(i)
100 i = i + 1
110 Print “Calculating Area of the 10 Triangles”
120 FOR x = 1 to 10
130 Print “ Area of Triangle”; ½*bases(x)*heights(x);
140 NEXT X
150 END.
Output the sum of the first 100 integers
10 SUM = 0
20 FOR I = 1 to 100
30 SUM = SUM + i
40 NEXT i
50 Print “Sum of the first 100 integers is “,SUM
60 END
Output the value elements of a given array
10 DIM array(5)
20 array(1) = 10
30 array(2) = 100
40 array(3) = 1000
50 array(4) = 10000
60 array(5) = 100000
70 PRINT array(1), array(2), array(3), array(4), array(5)
80 END