WEEK 8 and 9
BASIC programming III (One dimensional Array)
One Dimensional 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 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
(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 instructions
(1) 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
(2) 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 variables including array variables.
Example
10 DIM A as INTEGER
20 DIM array(5)
30 END
Examples using Arrays
1. Store data in a vector 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 y(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:
(2) 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
50 SUM=0
60 FOR I = 1 to 100
70 SUM=SUM + array(1)
80 NEXT I
90 Average = SUM / 100
100 Print "The Average of the content of the array is ",Average
(3) Calculate the area of 10 Different with and without the WHILE WEND statement
Different 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 <= 10
60 Print "Enter Value for Base of Triangle"
70 INPUT bases(i)
80 Print "Enter Value for Height of Triangle
90 INPUT heights(i)
100 i = i + 1
110 Print "Calculating Area of the 10 Triangles"
120 FOR x = 1 to 10
130 Print "Area of Triangle ";1/2" bases(X) heights(X);
140 NEXT X
150 END
(4) Output the sum of the first 100 integers
10 SUM=0
20 FORi = 1to * 100
30 SUM = SUM +1
40 NEXT i
50 Print "Sum of the first 100 Integers is ", SUM
(5) 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)
140 END