BASIC Programming
An array is a set of like variables arranged in sequence
identified by their subscripts. The subscripted variable names are similar to
simple variable names except that they are3 followed by a subscript. The
subscript must be an integer and must be enclosed in parentheses. For example
A(4), A$(5).
Program
10 Read Items $ (1), Items $ (2), Items $ (3)
20 Data Pen, Book, Pencil
30 Print Items $ (1), Items $ (2), Items $(3)
40 End
Output
Pen Book Pencil
Array using a For…Next Loop
Program
10 For i=1 to 3
20 Read Items $ (i)
30 Next i
40 Data Pen, Book, Pencil
50 For b= 1 to 3
60 Print Items $ (b)
70 Next b
80 End
Output
Pen
Book
Pencil
Dim statement
Dim is a short for dimension. Dim statement is used to
declare an array of more than 10 subscripts.
Program
10 For i=1 to 12
20 Read Names $ (i)
30 Next i
40 Data Masood, Imran, Rahim Jan, Iltiaz, Zaid, Chakar,
Adil, Kamran, Sohail, Aman, Amir, Javid
50 For b= 1 to 12
60 Print names $ (b)
70 Next b
80 End
Output
Masood
Imran
Rahim Jan
Iltiaz
Zaid
Chakar
Adil
Kamran
Sohail
Aman
Amir
Javid
12.2 Two Dimensional
Arrays
Sometimes data is arranged in rows and columns. Thus two or
more subscripts are needed to represent each element in such arrays.
Class
|
Present
|
Leave
|
Absent
|
7
|
60
|
3
|
2
|
8
|
52
|
1
|
1
|
The locations of the data in this table can be described by
numbering both rows and tables as given below.
Col. 1
|
Col. 2
|
Col. 3
|
Col. 4
|
|
Row 1
|
A (1, 1)
7
|
A (1, 2)
60
|
A (1, 3)
3
|
A (1, 4)
2
|
Row 2
|
A (2, 1)
8
|
A (2, 2)
52
|
A (2, 3)
1
|
A(2, 4)
1
|
12.3 Filling and printing Two Dimensional Arrays
One way of filling a two dimensional arrays is by For…Next
nested loop.
Program
10 Dim A(2, 4)
20 For Row=1 to 2
30 For Col = 1 to 4
40 Read A (Row, Col)
50 Next Col
60 Row
70 Data 7, 60, 3, 2
80 Data 8, 52, 1, 1
90 For Row=1 to 2
100 For Col = 1 to 4
110 Print A (Row, Col)
120 Next Col
130 Row
140 End
No comments:
Post a Comment