ARRAY in QBASIC is used to store sequential values of same kind.We can use array instead of using number of separate variables. Using different variables to store values will makes our program lengthy and complicated. So, in such cases QBASIC provides us facility to store values of same type.
So, Basically there are two types of array in QBASIC:
1) SINGLE DIMENSIONAL ARRAY
2) DOUBLE / TWO DIMENSIONAL ARRAY
Declaration:-
We use DIM keyword to declare array.
Syntax: DIM(number of items).
Example:- DIM A(10)
The above will open 10 dimensions in memory to store 10 numbers. i.e.
1 2 3 4 5 6 7 8 9 10
Each index store a single value which is called by A(1), A(2), A(3) ......etc
Here we will make program to store value in an array.
In the above program we have declared array with the name a , size 5 and we have stored values manually in each index of the array by calling them directly.
The program will display each value stored in an array.
Output:-
The double dimensional array is used to store values in two dimensions i.e. columns and rows. You may think of a double dimension array as a table like structure , 4 rows of 4 elements. 4*4 unique elements' space will be created in the memory.
Declaration-
Syntax- DIM(row,column)
Example- DIM NUM(3,3)
In the above declaration NUM is the array name with size of 3 rows and 3 columns. It will create 3*3=9 unique elements to store numeric values because it is numeric array.
In the above table we have addressed the each element of an array in detail.
Now, we will observe example to see how it works.
Example-
Write a program to declare an array with dimension 3*3.
CLS
DIM num(3,3)
END
The above program will created 3*3 i.e. 9 locations to store numeric values. Now, we will see how to store values in an double dimensional array.
Example-
Output:-
The above program will store values only in an array.
Now, we will make program to display values stored in an array using double dimensional array.
Output:-
1. Write the statement for:
a) Declaring a numeric array of 10 numbers
Ans- DIM A(10)
b) To Input numbers into an array named n(15)
Ans-
CLS
DIM n(15)
FOR a= 1 TO 10
INPUT "ENTER NUMBER- " ; n(a)
NEXT a
END
c) To Print the number at 6th Location in the array name arr.
Ans-
REM IN THIS PROGRAM ASSUME THAT YOU HAVE ALREADY STORED VALUES IN AN ARRAY n(10)
CLS
DIM n(10)
PRINT n(6)
END
d) To Print the last element of the array name comp.
Ans-
REM IN THIS PROGRAM ASSUME THAT YOU HAVE ALREADY STORED VALUES IN AN ARRAY num(5)
CLS
DIM num(5)
PRINT num(5)
END
e) Declare an array to store 15 names
Ans-
CLS
DIM N$(15)
FOR X = 1 TO 15
INPUT " ENTER NAME-"; N$(X)
NEXT X
END
f) To be updated......................
2) How do we draw a box in QBASIC? Explain
a) HORIZONTAL LINE
Output:-
b) VERTICAL LINE
Output:-
c) A Box filled with red colour
Output:-
d) A Cirlce
Output:-
So, Basically there are two types of array in QBASIC:
1) SINGLE DIMENSIONAL ARRAY
2) DOUBLE / TWO DIMENSIONAL ARRAY
SINGLE DIMENSIONAL ARRAY
Declaration:-
We use DIM keyword to declare array.
Syntax: DIM
Example:- DIM A(10)
The above will open 10 dimensions in memory to store 10 numbers. i.e.
1 2 3 4 5 6 7 8 9 10
Each index store a single value which is called by A(1), A(2), A(3) ......etc
Here we will make program to store value in an array.
In the above program we have declared array with the name a , size 5 and we have stored values manually in each index of the array by calling them directly.
The program will display each value stored in an array.
Output:-
DOUBLE / TWO DIMENSIONAL ARRAY
Declaration-
Syntax- DIM
Example- DIM NUM(3,3)
In the above declaration NUM is the array name with size of 3 rows and 3 columns. It will create 3*3=9 unique elements to store numeric values because it is numeric array.
NUM(1,1)
|
NUM(1,2)
|
NUM(1,3)
|
NUM(2,1)
|
NUM(2,2)
|
NUM(2,3)
|
NUM(3,1)
|
NUM(3,2)
|
NUM(3,3)
|
In the above table we have addressed the each element of an array in detail.
Now, we will observe example to see how it works.
Example-
Write a program to declare an array with dimension 3*3.
CLS
DIM num(3,3)
END
The above program will created 3*3 i.e. 9 locations to store numeric values. Now, we will see how to store values in an double dimensional array.
Example-
Output:-
The above program will store values only in an array.
Now, we will make program to display values stored in an array using double dimensional array.
Output:-
ACTIVITY
1. Write the statement for:
a) Declaring a numeric array of 10 numbers
Ans- DIM A(10)
b) To Input numbers into an array named n(15)
Ans-
CLS
DIM n(15)
FOR a= 1 TO 10
INPUT "ENTER NUMBER- " ; n(a)
NEXT a
END
c) To Print the number at 6th Location in the array name arr.
Ans-
REM IN THIS PROGRAM ASSUME THAT YOU HAVE ALREADY STORED VALUES IN AN ARRAY n(10)
CLS
DIM n(10)
PRINT n(6)
END
d) To Print the last element of the array name comp.
Ans-
REM IN THIS PROGRAM ASSUME THAT YOU HAVE ALREADY STORED VALUES IN AN ARRAY num(5)
CLS
DIM num(5)
PRINT num(5)
END
e) Declare an array to store 15 names
Ans-
CLS
DIM N$(15)
FOR X = 1 TO 15
INPUT " ENTER NAME-"; N$(X)
NEXT X
END
f) To be updated......................
2) How do we draw a box in QBASIC? Explain
a) HORIZONTAL LINE
Output:-
b) VERTICAL LINE
Output:-
c) A Box filled with red colour
Output:-
d) A Cirlce
Output:-
LAB ACTIVITY
No comments:
Post a Comment
Thank you so much to like us.