Professional Documents
Culture Documents
CSC 112 - Lecture 5
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
…
80 NEXT J ’end of child loop
90 …
100 NEXT I ’end of parent loop
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
30 PRINT “TABLE OF RESULTS”
50 FOR X=1 TO 10
60 PRINT TAB(1); X; TAB(4); SIN(X); TAB(14); COS(X)
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.
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
This is a 3*7, 2-dimensional array.
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
This is a 5*7, 2-dimensional array.
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.
E.g. 20 READ R, T, E
…
100 DATA 5, -7, 4
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
80 FOR J=1 to 11
90 IF number (J) <min THEN 110
120 NEXT J
Question
1. Write a program that stores words in an array and arranges them in
alphabetical order.