0% found this document useful (0 votes)
47 views7 pages

CSC 112 - Lecture 5

The document discusses loop nesting, BASIC programming features like functions, arrays, and the READ-DATA statement. It provides examples of calculating values using functions, storing data in arrays, and using READ-DATA to input array values. It also gives an example program to find the minimum value in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

CSC 112 - Lecture 5

The document discusses loop nesting, BASIC programming features like functions, arrays, and the READ-DATA statement. It provides examples of calculating values using functions, storing data in arrays, and using READ-DATA to input array values. It also gives an example program to find the minimum value in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

CSC 112 – LECTURE FIVE

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.

30 FOR I=1 TO 10 STEP 2 ’parent loop

40 LET X=4+Y

50 FOR J=1 TO 100 STEP 10 ’child loop

80 NEXT J ’end of child loop

90 …

100 NEXT I ’end of parent loop

Included features of BASIC

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

20 LET Y=INT (3.24) ’Y = 3

20 LET Y=INT (-3.24) ’Y = -4

20 PRINT TAB (4); “This is the illustration of the TAB function”

30 REM The above string in quotes is displayed starting from column 4

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.

20 PRINT “Your name is”;TAB (4);name$

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

10 REM This program calculates the sin and cos of numbers 1 to 10

20 CLS

30 PRINT “TABLE OF RESULTS”

40 PRINT TAB(1); “X”; TAB(5); “SIN(X)”; TAB(15); “COS(X)”

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.
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

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.

10 DIM R(20), G$(20) , A (5,5)

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.

The READ statement

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

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

10 REM This program makes use of an array of 12 numbers

20 DIM number (11)

30 REM Store values into the array

40 FOR I=0 to 11

50 INPUT number (I)

60 NEXT I

70 LET min= number (0)

80 FOR J=1 to 11

90 IF number (J) <min THEN 110

100 IF min<=number (J) THEN 120

110 LET min=number (J)

120 NEXT J

130 PRINT “The least number in the array is ”; min

140 END
Question

1. Write a program that stores words in an array and arranges them in


alphabetical order.

You might also like