B 29 CDF
B 29 CDF
Definition of Array
An array is a list of variables of the same kind. A variable is a name which the computer assigns value to.
To create an array, the DIM (dimension) command is used. The DIM statement has the following syntax:
DIM arrayName(n)
For example, DIM Score (5) will reserve 6 spaces, Score (0) Score (1), Score (2), Score (3), Score (4)
and Score (5) in the memory to hold value. The number inside the parentheses of the individual
variables are called subscripts, and each individual variable is called subscripted variable or
element
Operations on Array
a. Input operation
b. Output operation
c. Arithmetic operation
DIM IN (10)
IN (1) = 10
IN (2) = 11
IN (3) = 12
IN (4) = 13
IN (5) = 14
IN (6) = 15
IN (7) = 10
IN (8) = 11
IN (9) = 12
IN (10) = 13
PRINT IN (5)
END
[run]
Example 2 Create an array to access your favourite of the day of the week
REM Array to create and access your favourite the day of the week
DIM DAY$(7)
DAY$(1) = "Sunday"
DAY$(2) = "Monday"
DAY$(3) = "Tuesday"
DAY$(4) = "Wednesday"
DAY$(5) = "Thursday"
DAY$(6) = "Friday"
DAY$(7) = "Saturday"
INPUT "enter the number that corresponds to your favorite day of the week"; n
PRINT "My favorite day of the week is"; DAY$(n)
END
[run]
LOOPING
Looping is used to have the computer do repetitive tasks in a fraction of time. The most common types of
loop used in QBASIC programming is the FOR...NEXT and WHILE WEND loop that repeats a series of
Example 1:
FOR I = 1 TO 5
NEXT I
END
The program will print the dullest pencil is better than the sharpest memory” five times.
Solution
FOR NUM = 1 TO 10
PRINT NUM
NEXT NUM
END
Example 3 Write a program to print
Solution
PRINT ODD
NEXT ODD
END
Solution
LET SUM = 0
PRINT ODD
NEXT ODD
Example 1
CLS
LET A= 1
WHILE A< 11
A = A+1
WEND
END
Example 2 Write a program to print the square of even numbers from 6 to 22 using WHILE – WEND
Statement
CLS
LET N = 6
WHILE N<23
N=N+2
WEND
END
More examples using DIM, FOR-NEXT and WHILE – WEND statements
Solution
CLS
DIM IN(10)
FOR I = 1 TO 10
NEXT I
END
CLS
DIM IN(100)
LET SUM = 0
FOR I = 1 TO 100
NEXT I
LET AVERAGE = SUM / 100
END
3. Calculate the area of 10 different rectangles using the WHILE – WEND statement
CLS
DIM LENGTH(10)
DIM WID(10)
DIM AREA(10)
LET I = 1
WHILE I < 11
I=I+1
WEND
END