Two-Dimensional Array in QBASIC
One dimensional array is good for
sorting long sequence of one dimensional data such as age, name and marks and
so on. But in case of the data with more than one dimension such as students
roll number and obtained marks, we need multi dimensional arrays.
Two dimensional array represents series
of similar type of data by a variable name with double subscripts. Subscripts
for a two dimensional array are used rows and columns. The two dimensional
array is convenient to use to calculation in the form of table.
Syntax:
DIM array_name(subscript[,subscript]…)
Example:
DIM A(3,3)
Above array A has 3 rows and 3
columns. It can be expressed as:
|
Col1 |
Col2 |
Col3 |
Row
1 |
A(1,1) |
A(1,2) |
A(1,3) |
Row
2 |
A(2,1) |
A(2,2) |
A(2,3) |
Row
3 |
A(3,1 |
A(3,2 |
A(3,3) |
Example: 1
CLS OPTION BASE 1 DIM num(3, 4) FOR i = 1 TO 3 FOR j = 1 TO 4 READ num(i, j) NEXT j NEXT i DATA65,99,88,98,90,77,65,90,12,89,32,54 FOR i = 1 TO 3 FOR j = 1 TO 4 PRINT num(i, j); NEXT j PRINT : NEXT i END |
Output |
Example: 2
Program to display the tabular form
using multi dimensional array with their row-wise sum.
CLS DIM n(3, 3) FOR i = 1 TO 3 FOR j = 1 TO 3 READ n(i, j) NEXT j NEXT i DATA 3,5,6,7,8,2,1,4,3 FOR i = 1 TO 3 s = 0 FOR j = 1 TO 3 PRINT n(i, j); s = s + n(i, j) NEXT j PRINT "="; s NEXT i END |
Output |
Example: 3
Program to display the tabular form
using multi dimensional array with their column-wise sum.
CLS DIM num(3, 4) FOR i = 1 TO 3 FOR j = 1 TO 4 READ num(i, j) PRINT num(i, j), NEXT PRINT NEXT PRINT
"---------------------------------------------------" FOR i = 1 TO 4 s = 0 FOR j = 1 TO 3 s = s + num(j, i) NEXT j PRINT s, NEXT i DATA 2,4,5,7,8,9,1,3,6,8,1,3 END |
Output |
Example: 4
Program to display the tabular form
using multi dimensional array with their row and column-wise sum.
CLS
DIM num(3, 4)
FOR i = 1 TO 3
sum= 0
FOR j = 1 TO 4
READ num(i, j)
PRINT num(i, j),
sum = sum + num(i, j)
NEXT
PRINT "="; sum
NEXT
PRINT "====================="
FOR i = 1 TO 4
sum = 0
FOR j = 1 TO 3
sum = sum + num(j, i)
NEXT j
PRINT s,
NEXT i
DATA 2,4,5,7,8,9,1,3,6,8,1,3
END
Output of the program:
No comments:
Post a Comment