CSC 112 - Lecture 5
CSC 112 - Lecture 5
We considered the FOR loop in the last class. Much more complex tasks requiring
more than one loop can be encountered. It is possible to have a FOR loop in
another FOR loop. This is called Loop nesting.
e.g.
40 LET X=4+Y
90 …
There are some additional features of BASIC which are often used for common
operations.
Library functions are in-built features of BASIC. These are called functions. These
functions can be used by calling them using the name of the function and stating or
providing all the necessary arguments to be used by the predefined function.
Table of some functions
Function Description
ABS(X) Calculates the absolute value of X
COS (X) Calculates the cosine of X. X must be in radians
COT (X) Calculates the cotangent of X. X must be in radians
EXP (X) Calculates e raised to the power of X.
INT (X) Determines the largest integer that can be derived
from X and which does not exceed X. If X is a
positive fractional number, the fraction is truncated
and the whole number is the result but if X is
negative, the number is rounded up.
LOG (X) Calculates the natural logarithm of X. ( )
SGN (X) Determines the sign of X. If X is positive, it returns
+1, if negative, -1 and if zero, 0
SIN (X) Calculates the sine of X. X must be in radians
SQR (X) Calculates the square root of X.
TAB (N)X It is used alongside the PRINT keyword and is used
to print the value in the column N. Each character is
displayed in a column and the TAB takes the cursor
to the column specified. The first (leftmost) column
of display is column 0.
TAN (X) Calculates the tangent of X. X must be in radians
Examples
This is the illustration of the TAB function (it is displayed this way)
If the TAB argument is less than the current position of the cursor on the display,
the TAB is ignored.
e.g.
In this statement, the TAB is ignored because the cursor after printing the string
“Your name is” is at column 12. So, the value of the name$ variable is printed
starting from the column 12 immediately after the last printed string.
Example
Write a program that calculates the sin and cosine of numbers 1 to 10. Display the
result in a presentable, table-like format with the use of the TAB function.
Solution
20 CLS
50 FOR X=1 TO 10
70 NEXT X
80 END
Arrays
In BASIC, there exists the kind of variables called Array variables. They are
elements having a type of data structure which involves storing a number of data
values of the same type within a single variable.
There are one-dimensional arrays and two-dimensional arrays.
1-dimensional arrays are arrays which only store a single line or row of values. It
may be referred to as a 1*n matrix. That is, a matrix having n columns and just a
single row.
E.g. 2 3 3 4 1 5 4
2-dimensional arrays are arrays which store in both rows and columns. It may be
referred to as a n*n matrix. That is, a matrix having n columns and n rows and it’s
values can be accessed using the coordinates x and y for row and column numbers
respectively.
E.g. 2 3 3 4 1 5 4
2 3 3 4 1 5 4
2 3 3 4 1 5 4
Or
2 3 3 4 1 5 4
2 3 3 4 1 5 4
2 3 3 4 1 5 4
2 3 3 4 1 5 4
2 3 3 4 1 5 4
In BASIC, array values are referenced with the use of numbers within the range of
zero and positive integers. Formula can be used as the subscripting value, but must
yield a valid integer. If a valid integer is not the result, the fractional part is
truncated and the resulting integer value s used, but if the value of the formula is
negative, an error occurs.
Arrays can be declared with the use of the DIM statement.
This creates two different arrays R having the space for 21 elements and G having
the space for 21(indices 0 to 20) elements. The DIM (dimension) keyword
specifies the maximum number of values to be stored.
If an array is used without the DIM statement, by default, 11 spaces are reserved
for the array if it is 1-dimensional and 121 spaces (11 rows and 11 columns)
reserved for the array if it is 2-dimensional.
When large amount of input is required to be entered into the computer, the INPUT
statement might not be suitable. With the READ statement, the large amount of
data must be already available. The READ statement works in line with the DATA
statement.
E.g. 20 READ R, T, E
The two statements work together in such a way that the READ and DATA
statements can be placed apart but still, the variables to be read in as used in the
READ statement are initialized with the corresponding values entered in the
DATA statement. From the above, R has the value 5, T, the value -7 and E, the
value 4.
If there are multiple READ statements, the values in the DATA statement are
assigned to the variables in the READ statement in order and type. There can be
multiple READ and DATA statements.
The following rues should be followed when using the READ-DATA statement:
1. The data items must correspond in order and type to the variable listed in the
READ statements. Extra data items not corresponding to any variable will
be ignored.
2. The data items in the DATA statement should be separated by commas.
3. The data items must be numbers or string constants and not formulae or
function calls.
4. String values having spaces should be enclosed in quotes.
Example
Write a program that stores values in an array of 12 numbers and then determines
the least of all the numbers in the array.
Solution
40 FOR I=0 to 11
60 NEXT I
80 FOR J=1 to 11
120 NEXT J
140 END
Question