1. Fill in the blanks:
a. array b. similar c. store,...use d. subscripts e. subscripts f. DIM
2. State whether each of the following statements are true or false:
a. false b. false c. true d. false e. false f. false
3. Answer the following questions:
Here are the brief answers to your questions about variables and arrays in QBASIC:
a. What is a simple variable? Write its disadvantage comparing with array.
-
Simple Variable: A simple variable is a single memory location used to store a value. It holds one value at a time and is declared using the
DIM
orLET
statement in QBASIC. For example,DIM num AS INTEGER
. -
Disadvantage Compared with Array:
- Limited storage: A simple variable can only hold one value at a time, whereas an array can hold multiple values.
- Less efficient for large data: If you need to store a large set of related data, using individual variables becomes impractical (e.g.,
num1, num2, num3,...
), while an array stores them in one structure. - Harder to manage: When dealing with large amounts of data, a simple variable requires more manual effort to organize, unlike an array that allows easy manipulation of related data.
b. What is an array? Write some advantages of it.
-
Array: An array is a data structure that allows you to store multiple values of the same type in a single variable. The values are indexed (subscripted) and can be accessed via their index number.
-
Advantages of Array:
- Efficient data storage: Arrays can store multiple values in a single variable, making it easier to manage and access large datasets.
- Simplifies code: Reduces the need for multiple individual variables (like
num1, num2, num3, ...
) and makes the code cleaner. - Easier to process: Operations on arrays, like loops for iteration, make processing of multiple elements easier and faster.
- Dynamic sizing (with
REDIM
): Arrays can be resized during runtime, providing flexibility.
c. What is a single-dimensional array? Give an example.
-
Single-Dimensional Array: A single-dimensional array is a linear collection of elements, all of which are stored in contiguous memory locations. It is accessed using a single index (subscript).
-
Example in QBASIC:
DIM nums(5) ' Declares an array of 6 elements (0 to 5) nums(0) = 10 nums(1) = 20 nums(2) = 30 nums(3) = 40 nums(4) = 50 nums(5) = 60 PRINT nums(3) ' Outputs: 40
d. What are the elements of an array? Give an example.
-
Elements of an Array: Elements of an array are the individual values stored within the array. Each element can be accessed using an index or subscript.
-
Example:
DIM arr(3) arr(0) = 5 arr(1) = 10 arr(2) = 15 arr(3) = 20 PRINT arr(2) ' Outputs: 15
- Here,
5
,10
,15
, and20
are the elements of the arrayarr
.
- Here,
e. Why is an array called a subscripted variable?
-
Array as a Subscripted Variable: An array is called a subscripted variable because it allows you to access its individual elements using a subscript (index). The subscript identifies the position of each element in the array.
-
Example:
DIM nums(5) nums(0) = 10 ' Here, nums(0) is a subscripted variable nums(1) = 20 ' nums(1) is another subscripted variable
f. What is a double-dimensional array? Give an example.
-
Double-Dimensional Array: A double-dimensional array is an array with two dimensions, often visualized as a table or matrix with rows and columns. You can access its elements using two subscripts (row and column indices).
-
Example in QBASIC:
DIM matrix(2, 3) ' Declares a 2x3 array (2 rows, 3 columns) matrix(0, 0) = 1 matrix(0, 1) = 2 matrix(0, 2) = 3 matrix(1, 0) = 4 matrix(1, 1) = 5 matrix(1, 2) = 6 PRINT matrix(1, 2) ' Outputs: 6
Summary of Array Types:
- Single-Dimensional Array: A list of values accessed by a single index (e.g.,
nums(0), nums(1)
). - Double-Dimensional Array: A table of values accessed by two indices (e.g.,
matrix(1, 2)
).
These are the key concepts and examples related to variables and arrays in QBASIC.
Some Array Example code in QBAISC
a. Input 10 different numbers in an array, print them, and find the sum of numbers which are multiples of 5.
Explanation:
DIM nums(10)
: Declares an arraynums
with 10 elements to store the numbers.FOR i = 1 TO 10
: Loops through to get input for 10 numbers.IF nums(i) MOD 5 = 0
: Checks if the number is divisible by 5 (i.e., a multiple of 5).SUM = SUM + nums(i)
: If the number is a multiple of 5, it is added to the sum.PRINT
: Outputs the numbers and the sum of multiples of 5.
b. Write a program to print a two-dimensional array and sum of each row using the given data:
Given Data:
1 2 3 4
5 6 7 8
9 10 11 12
QBasic Program using READ
and DATA
:
CLS
DIM matrix(3, 4) REM Declare a 3x4 array (3 rows and 4 columns)
DIM rowSum(3) REM Array to store the sum of each row
REm Use the READ statement to fill the matrix with data
DATA 1, 2, 3, 4
DATA 5, 6, 7, 8
DATA 9, 10, 11, 12
REm Initialize the matrix with the values from DATA
FOR i = 1 TO 3 REM Loop through rows
FOR j = 1 TO 4 REM Loop through columns
READ matrix(i, j) REM Read values into the matrix
NEXT j
NEXT i
REM Print the matrix and sum of each row
PRINT "Matrix:"
FOR i = 1 TO 3 REM Loop through rows
rowSum(i) = 0 REM eset row sum for each row
FOR j = 1 TO 4 REM Loop through columns
PRINT matrix(i, j); " "; REM Print each element
rowSum(i) = rowSum(i) + matrix(i, j) REM Calculate the sum for each row
NEXT j
PRINT REM Move to the next line after each row
NEXT i
REM Print the sum of each row
PRINT "Sum of each row:"
FOR i = 1 TO 3
PRINT "Sum of row "; i; " is "; rowSum(i)
NEXT i
END
Explanation:
DATA
statement: This is used to initialize a list of values that will be read into the array. The data is provided in rows corresponding to the matrix.READ matrix(i, j)
: Reads values from theDATA
statement into the matrix. Each value is placed in the corresponding position in the two-dimensional array.- Matrix and row sum calculations: The program then prints the matrix and calculates the sum of each row using the
rowSum(i)
array.
Output for Question b:
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Sum of each row:
Sum of row 1 is 10
Sum of row 2 is 26
Sum of row 3 is 42
Summary:
- This program demonstrates how to use the
DATA
statement to initialize a matrix and calculate the sum of each row. - The
READ
statement reads values from theDATA
statement into the array.
No comments:
Post a Comment