NOS : Certificate in Computer Applications
| Home | Table of Contents |


Up: Online Course Material


LESSON 27

HANDLING OF ARRAYS, FUNCTIONS AND SUB-ROUTINE

27.1 INTRODUCTION

In the previous lesson, you have gone through the programming technique and various INPUT/OUTPUT and control statements in BASIC. But the features that make this programming language more useful are use of subscripted variables, subroutine and various functions provided to help the programmer. We shall be discussing all these features in this lesson. We have also included various sample programs, which will help you further in writing more complex programs in BASIC language.

27.2 OBJECTIVES

This lesson will explain you about handling of lists, tables, subroutine, sub-programs and some of the functions of BASIC. At the end of this lesson, you should be able to:

27.3 HANDLING LISTS AND TABLES (ARRAYS) IN BASIC

In all our programs so far, a single variable (one storage location in internal memory) has been associated with each variable name. In this lesson, we will discuss the concept of an array: a collection of variables, all of which are referenced by the same name. We will discuss one-dimensional arrays (lists) and two-dimensional arrays (tables), concentrating on the former. Each item of data in an array does not have a unique variable name, on the other hand, a group of similar data is given one name. The smallest component of an array is called an element of the array.

Example 1: suppose we want to represent five numbers (10,12,42,91,7,) which represent the marks of five students

Solution:

We can use an array name M(I) which is also called the subscripted variable and I is the subscript which varies from 1 to 5. M is the name of array and M(1) is an element of the array M.

In the memory it is stored as,

M(I)

Cells Quantities

M(1) 10

M(2) 12

M(3) 42

M(4) 91

M(5) 7

Problem1: Write a program to read the above data into the memory.

Program 1

10 DIM M(5)

20 FOR I = 1 TO 5

30 READ M(I)

40 NEXT I

50 DATA 10,12,42,91,7

60 END

Let us go back to the above example.

M is an array of 5 numbers 10,12,42,91,7, where under the array name M the five different elements M(1),M(2), M(3), M(4), M(5), are given one subscript I. This M(I), with a single subscript I, is an example of a one-dimensional array. Let us study a practical example of one-dimensional array.

Example 2:

We have an item biscuit in a shop. We have prices for 10 different biscuits as 60, 45.5, 32.6, 19.5, 52, 49,53.75, 29, 43, 38.6. We write a program to print these different values or we want to know the price of any type of the above biscuits. We will do it by using an array.

Solution

Since they are similar items, we give them a single name BISCUIT or in short BIS. Since we know only one specification i.e. price we have a single subscript, say I. So the array is now BIS(I). Since there are 10 prices for 10 types of biscuits I varies from 1 to 10 i.e. BIS(1), BIS(2)…….BIS(10).

If we want to know the price for the biscuit of type 4, we will print BIS(4) which will give us the required price.

Program 2

10 DIM BIS(10)

20 REM PRICES FOR 10 TYPES OF BISCUITS ARE TO BE READ

30 REM THE 10 PRICES HAS TO BE PRINTED ALSO

\40 FOR I = 1 TO 10

50 READ BIS(I)

60 NEXT I

70 PRINT "THE PRICES OF TEN DIFFERENT BISCUITS"

80 FOR I =1 TO10

90 PRINT BIS(I)

100 NEXT I

110 DATA 60,45.5, 32.6, 19.5, 52,49,53.75

120 DATA 29,43,38.6

130 END

OUTPUT

Line number 70 will print

THE PRICES OF TEN DIFFERENT BISCUITS

Line number 90 due to line number 80 and 100 will print

60

45.5

32.6

19.5

52

49

53.75

29

43

38.6

The line number 10 DIM BIS(10) is compulsory. This informs the machine that we are giving an array with single subscript having 10 elements. So, 10 cells in the memory location will be kept for the variable name BIS.

If we want an array of strings instead of numeric value, see the following example:

Example 3

To store the names of six motor cars in any array (table) such as:

FORD MARUTI AMBASSADOR FIAT STANDARD VAUXHAL L

We can write a program as follows:

Program 3

10 REM DECLARE THE NAME AND SIZE OF THE TABLE

20 DIM C$(6)

30 REM… USE THE READ/DATA TECHNIQUE

40 REM TO STORE STRINGS

50 FOR J = 1TO 6

60 READ C$(J)

70 NEXT J

80 REM.. NOTE THE STRINGS USED AS DATA ARE

90 REM.. ENCLOSED BETWEEN QUOTATION MARKS

100 REM.. ARRAY NAME TO STORE STRINGS IS

110 REM.. GIVEN AN STRING VARIABLE NAME C$

120 DATA "FORD", "MARUTI", "AMBASSADOR"

130 DATA "FIAT", "STANDARD", "VAUXHALL"

140 END

The REM (i.e. remarks) given in the line number 30,40,80,90,100,110, explains the program.

Though DATA is given in two lines 120 to 130, the data should be in the order they are placed.

C$(1) = "FORD"

C$(2) = "MARUTI"

C$(3) = "AMBASSADOR"

C$(4) = "FIAT"

C$(5) = "STANDARD"

C$(6) = "VAUXHAULL"

Example 4

Let the 10 data values are given as 4, -6, 7, 2.3, - 6.1, 5. 3

-1, 0, - 2.7,9

WE give this set of values the name, say VAR (I) where I is the subscript.

If I = 1, then VAR (1) has the value 4

I = 5, then VAR (5) has the value -6.1

I = 8 then VAR (8) has the value 0, and so on.

The program for adding all the 10 numbers.

Program 4

10 REM ADDING 10 NUMBERS IN A ONE DIMENSIONAL TABLE

12 DIM VAR (10)

15 LET SUM = 0

20 FOR I = 1 TO 10

30 READ VAR (I)

40 LET SUM = SUM +VAR (I)

50 NEXT I

60 DATA 4, -6,7, 2.3, -6.1, 5.3, -1, 0, -2.7,9

70 PRINT SUM

80 END

27.3.1 The DIM Statement

When subscripted variables are used in a program, certain information about them must be supplied to the computer before it is used. These are:

(a) Which variables are subscripted?

(b) What is the maximum size for each subscript?

DIM is the short form of DIMENSION.

By using this statement in line number 10 of the program 1 above, the array whose name is M has been allotted 5 cells in the memory location. Syntax for the DIM statement is

Line number DIM array name (unsigned integer)

The unsigned integer specifies the size of the array variable.

If we write 10 DIM A(100), X(10), then 100 locations are reserved for the array name A and 10 locations for the array name X.

DIM should be the first statement in the program barring REM statement.

Problem 5

Suppose we want to read the roll number of a student and his marks obtained in five subjects in the board examination. Now print the roll number and average marks secured by him.

Program 5

10 DIM M(5)

20 INPUT ROLLNO

25 LET TOT = 0

30 FOR I = 1 TO 5

40 READ M(I)

50 LET TOT = TOT + M(I)

60 NEXT I

70 LET AVERAGE = TOT/5

80 PRINT ROLLNO, AVERAGE

90 DATA 60,52,49,80,72

100 END

On the execution of line number 30, initially the control variable I becomes 1 and line number 40 reads M(I), i.e. the first mark from line number 90, that is 60 and adds to the variable TOT which is initially zero. So for I = 1 line 50 gives TOT = 0 +60 = 60, control then goes to line number 60 and back to 30. Now I becomes 2 and line number 40 reads M(2), i.e. 2nd marks from DATA, i.e. 52. In line number 50 TOT = 60 + M(2), i.e. TOT = 60+52 = 112 and so on. So line number 30 to 60 is executed five times. Thus finally in the variable name TOT we have TOT = 60+52+49+80+72=313.

So, when line number 70 is executed AVERAGE = 313/5 =62.6. Line number 80 will print the ROLLNO which is entered in line number 20 and the average AVERAGE as 62.6.

If we want to do this process for a large number of students say 10 we have to give more data in DATA statement, i.e. 45 more data values and put another loop to repeat the process for ten students.

Program 5(a)

10 DIM M (5)

15 FOR J = 1 TO 10

20 INPUT ROLLNO

25 LET TOT =0

30 FOR I = 1 TO 5

40 READ M (I)

50 LET TOT = TOT + M(I)

60 NEXT I

70 LET AVERAGE = TOT/5

80 PRINT ROLLNO, AVERAGE

85 NEXT J

90 DATA 60,52,49,80,72,98,69,72,80,75

100 DATA 88,61,54,48,60,52,92,86,81,65

110 DATA _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

120 DATA _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

130 DATA _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

140 END

 


Top

IN-TEXT QUESTIONS 1

1.

Given the following code:

10 LET S =0

20 FOR I = 1 TO N STEP 2

30 LET S = S+2*I-1

40 NEXT I

50 END

You are required to complete each statement.

(a) The accumulator is the variable ___________

(b) The control variable is _______________

(c) After execution, S contains the sum of the first N _________ Integers.

2. Correct the errors in each of the following program segments:

(i)

10 FOR K = 1 TO 5

20 FOR J = 1 TO K

30 PRINT K + J

40 NEXT K

50 NEXT J

(b)

10 LET S = 0

20 FOR I = 1 TO 10

30 LET S = S + I

40 PRINT S

50 NEXT K

 

3. What is the output of each of the following program segments?

(a)

10 FOR I = 7 TO 10 STEP 2

20 PRINT I;

30 NEXT I

40 PRINT I

50 END

(b)

10 FOR I = 1TO 10

20 IF I = 5 THEN 40

30 PRINT I + 1

40 NEXT I

50 END

 


Top

27.3.2 Double-Subscripts or two-dimensional arrays

A subscripted variable name in BASIC can have up to three subscripts. The use of two subscripts has very wide applications, especially manipulation of tables or any such things, which has rows and columns both in it.

Example: Represent subscripted variable or two-dimensional array having name TABLE(I.,J) where I indicates row and J indicates column.

Where, TABLE (I,J) =

2 4 8 10

1 3 5 7

3 7 13 17

Thus we see TABLE (I,J) has 3 rows, i.e. I = 1 TO 3 and 4 columns, i.e. J = 1 TO 4.

Problem 6

We want to read and print the values of TABLE (I,J) as given above

Program 6

10 DIM TABLE (3,4)

15 FOR I = 1 TO 3

20 FOR J = 1 TO 4

30 READ TABLE (I,J)

35 PRINT TABLE (I,J)

40 NEXT J

50 NEXT I

70 DATA 2,4,8,10,1,3,5,7,3,7,13,17,

80 END

See how it reads the data

TABLE(1,1) TABLE(1,2) TABLE(1,3) TABLE(1,4)

              2 ---------> 4 --------> 8 ----------> 10

TABLE(2,1) TABLE(2,2) TABLE(2,3) TABLE(2,4)

              1 ---------> 3 --------> 5 ----------> 7

TABLE(3,1) TABLE(3,2) TABLE(3,3) TABLE(3,4)

             3 ---------> 7 --------> 13 ----------> 17

 

As the subscript J corresponding to the column is in the inner FOR loop, the table is read row-wise. J being in the inner loop changes more frequently from 1 to 4 for every value of I, which is in the outer loop. If we want to read the data column-wise program will be as follows.

Program 7

10 DIM TABLE (3,4)

20 FOR J =1 TO 4

30 FOR I =1 TO 3

40 READ TABLE (I, J)

50 NEXT I

60 NEXT J

70 DATA 2, 1, 3, 4, 3, 7, 8, 5, 13, 10, 7, 17

80 END

More applications relating to subscripted variables will be shown in the last section.

We have shown you to store numeric data in a table (array) with row and column subscripts.

Now, You will see how string data is stored in a two-dimensional array.

Example

The following are data values (names), which are to be stored in the table form with rows and columns.

HARIOM DINESH RAJESH PANKAJ
BIMLA UPMA SANJU ANAMIKA

The program is as follows:

Program 7

10 REM DECLARE THE NAME AND SIZE OF THE TABLE

20 DIM N$ (2,4)

30 REM SET UP AN OUTER LOOP TO CONTROL THE ROW SUBSCRIPT

40 FOR M = 1 TO 2

50 REM…..SET UP AN INNER LOOP TO CONTROL THE COLUMN SUBSCRIPT

60 FOR P = 1 TO 4

70 REM .. USE A READ/DATA STATEMENT TO STORE DATA AS

80 REM .. SHOWN IN DIAGRAM

90 READ N$ (M, P)

100 NEXT P

110 NEXT M

120 DATA "HARIOM", "DINESH" "RAJESH"

130 DATA "PANKAJ", "BIMLA"

140 DATA "UPMA", "SANJU", "ANAMIKA"

150 END


Top

IN-TEXT QUESTIONS 2

1. Determine whether each of the following statements is true or false.

(a) Array of string data and array of numeric data can be declared in the same DIM statement.

(b) All elements of an array must be of either string type or numeric type.

(c) One and two-dimensional arrays cannot be declared in the same DIM statement.

2. A two-dimensional array FAX has two rows and four columns

5 10 15 20

25 30 35 40

(a) What are the values of FAX (1,3) and FAX (2,1) ?

(b) Which elements of FAX contain the numbers 30 and 20 ?

3. What is displayed when the following program segment is executed?

10 DIM A (2,3)

20 FOR I = 1 TO 2

30 FOR J = 1TO3

40 LET A(I,J) = I+J+1

50 NEXT J

60 NEXT I

70 PRINT A(1,2); A(2,1); A(2,2)

80 END

4 Write a BASIC program that determines and prints the smallest and largest elements of a two-dimensional array C (assuming that it; has been already inputted) with four rows and five columns.

5. Write a BASIC program to arrange the following numbers in an ascending order:

-71, -20, 14, 0, 5

 


Top

27.4 FUNCTION AND SUBROUTINE

27.4.1 Defining a function---the DEF statement

To avoid repeated programming of the same set of calculations, the programmer would like to write his or her own functions, which are similar to the Library functions. It may so happen that a particular calculation or a set of calculations occurs more than once in the program. If the calculation can be defined by a single statement then we use the function statement DEF FN.

Syntax: line number DEF FNV (a) = expression

Where "V" is the one-letter name of the function and "a" is the argument, which appears as a variable in the expression on the right.

Problem 1

Suppose we want to write a program to calculate y for various values of x, say for x = 1,2,3,4,5, for the expression y(x) =ax2 + bx + c

Program 1

10 DEF FNY (X) = A*X*X + B*X + C

20 INPUT A, B, C

30 FOR X = 1 TO 5

40 PRINT X, FNY(X)

50 NEXT X

60 END

On execution of the program line number 30 TO 50 will give the value of y(1) through y(5) for the respective value of x, calculated at line number 10. The value of x is supplied in line number 30 and values of A, B and C are input at line 20.

Problem 2

Let us consider the program to calculate

y = 1.5x + 3 for x<= 2

y = 2x + 5 for x>2

Program 2

100 DEF FNY(X)

105 INPUT X

110 IF X < = 2 THEN 140

120 LET FNY = 2*X + 5

130 GO TO 150

140 LET FNY = 1.5*X + 3

150 FNEND

170 PRINT X, FNY(X)

180 END

Here DEF FNY (X) statement in line number 100 is used without being equated to expression. The FNEND statement in line number 150 indicates the end of the function. The line numbers 160 to 180 consist the main program. On execution of line 170 a call to the function FNY(X) is made causing the execution of line number 100 to 150 with the input value of X.

The DEF statement must have DEF FN and variable name with one character and arguments included in the parenthesis. Arguments can be more than one also. The argument is a variable, which must appear in the right side after the equal to (=) sign, in case of DEF FN statement is equated to an expression. (For example, see problem 1)

Example 1

Evaluate the algebraic formula z =(u/v + x/y)/2 for different set of values of u, v, x, y.

Solution

10 DEF FNZ(U,V,X,Y) =(U/V + X/Y)/2

20 INPUT U,V,X,Y

30 PRINT FNZ(U,V,X,Y)

40 END

This is for one set of values for U,V,X,Y. Now the program given below will evaluate the formula for different set of values for U,V,X,Y

10 DEF FNZ (U,V,X,Y) = (U/V + X/Y)/2

20 INPUT U,V,X,Y

30 PRINT FNZ(U,V,X,Y)

35 IF U = O THEN 50

40 GO TO 20

50 END

Line number 40 sends the control back to INPUT. Now, you can enter another set of values for U,V,X,Y. When you want to stop, you enter O for U. Then line number 35 will bring the program to END.

Example 2

Define a function for subprogram for the algebraic formula

p = log(t2 -a) for t2 > a

log(t2) for t2 = < a

Solution

30 DEF FNP(T,A)

40 INPUT T,A

50 IF T * T < = A THEN 80

60 LET FNP =LOG (T ^ 2 -A)

70 GO TO 90

80    LET FNP = LOG (T ^ 2)

90 FNEND

100 PRINT T, A FNP (T,A)

110 END

In line number 40, the values of T,A can be input by READ/DATA or by using LET statements twice.

Note: Please consult the machine manual for DEFFN statement before running the programs in the machine.

27.4.2 Defining a subroutine -The GOSUB and RETURN statements

A subroutine is a collection of statements belonging to a process, which requires to be repeated very frequently in the program. A subroutine consists of set of program statements that may be used repeatedly at different places throughout the main program. In the main program the subroutines are called at different places using GOSUB statement.

GOSUB Statement

Syntax: line number GOSUB n

Where n is the line number of the 1st statement in the subroutine.

Subroutines are placed at the end of the main programs in order to repeat a process. The program control has to exit from the main program to enter a subroutine and after the process is completed it comes back to the main program using RETURN statement.

RETURN statement

Syntax: line number RETURN

The RETURN statement in the subroutine transfers the control back to the main program to the line immediately following the corresponding GOSUB statement. RETURN is the last statement in the subroutine.

GOSUB and RETURN are always used together in the program not independently.

Properties of GOSUB

Syntax: Line number IF (Logical expression) THEN COSUB (line number) ELSE GOSUB (line number)

Problem 1

Write a program to add, subtract, multiply and divide any two numbers.

10 INPUT A, B

20 INPUT "CHOICE", C$

30 IF C$ = "ADD" THEN GOSUB 80 : GOTO 65

40 IF C$ = "SUB" THEN GOSUB 120 : GOTO 65

50 IF C$ = "MUL" THEN GOSUB 170 : GOTO 65

60 IF C$ = "DIV" THEN GOSUB 210

65 PRINT C

70 END

80 REM "SUBROUTINE FOR ADDITION"

90 LET C = A + B

91 RETURN

120 REM "SUBROUTINE FOR SUBTRACTION"

130 LET C =A-B

150 RETURN

170 REM "SUBROUTINE FOR MULTIPLICATION"

180 LET C = A*B

200 RETURN

210 REM "SUBROUTINE FOR DIVISION"

220 LET C = A/B

240 RETURN

Short form of Problem 1 for addition

10 INPUT A, B

20 INPUT "CHOICE" C$

30 IF C$ = "ADD" THEN GOSUB 50

40 END

50 PRINT A+B

60 RETURN

So whatever will be the input of C$ in line number 20, GOSUB will go to these subroutines as it is written for C$ = "ADD" similar subroutines will follow for "SUB", "MUL", "DIV" ON GOSUB statement is similar to ON GOTO.

Syntax is given as:

Line no ON Numeric variable

Or

Numeric expression

GOSUB

(Line number of sub-routine

1, sub. 2, sub .3….)

Program 2

10 INPUT A,B

20 INPUT "1-ADD, 2-SUB, 3-MUL, 4-DIV"; N

30 ON N GOSUB 50, 60, 70, 80

40 END (To make the program repeat we should add GOTO 10)

50 PRINT A+B : RETURN

60 PRINT A-B : RETURN

70 PRINT A*B : RETURN

80 PRINT A/B : RETURN

Note: To make the program repeat, we should add GOTO 10 at line 35

 


Top

IN-TEXT QUESTIONS 3

1.

(a) What is a subscripted variable?

(b) What is the function of a DIM statement?

(c) Define a subroutine?

(d) What does a RETURN statement do?

2. What is displayed when each of the following program segments is run?

(a)

10 GOSUB 400

20 GOSUB 500

30 PRINT "ONE"

40 END

400 PRINT "TWO"

410 RETURN

500 PRINT "THREE"

510 RETURN

(b)

10 DEF FNA (W) = 2*W+1

20 PRINT FNA (2)

30 LET x = 3

40 PRINT SQR (FNA (x+1))

50 END

 


Top

27.5 EXAMPLES AND PROGRAMS SHOWING USAGE OF SOME BASIC FUNCTIONS: INT, MOD, RND, LOCATE, LEN, VAL, STR$, RIGHT$, MID$

27.5.1 INT Functions

It takes a numeric value as its argument and returns its value after truncating the decimal part. The value returned is always smaller than the number provided as the argument. For example,

10 Y= INT(13.2)

20 X= 15.6

30 Y= INT(X)

Statement 10 will return Y = 13 and

Statement 30 will return Y = 15.

Example 1

Two numbers A and B are given. To find the higher number between them:

Program 1

10 INPUT "HIGHER NO"; A

20 INPUT "SMALLER NO ";B

30 LET C = A/B

40 IF INT(C)= C THEN PRINT " HIGHER NUMBER="; A

50 END

This shows the use of the INT function.

Example 2

Write a program to find whether any year of the current century is a leap year or not.

Program 2

10 REM ** PROGRAM TO FIND WHETHER THE YEAR IS A LEAP YEAR OR

15 REM NOT **

20 PRINT "ENTER THE YEAR"

30 PRINT

50 INPUT YEAR

60 LET Y1 =INT(YEAR/4)

70 LET Y2 =YEAR/4

80 IF Y1 =Y2 THEN 110

90 PRINT YEAR "IS NOT A LEAP YEAR"

100 GO TO 120

110 PRINT YEAR; "IS A LEAP YEAR"

120 END

(A) Suppose we input year as 1988, then line number 60, we compute Y1 as 497 and also line number 70 gives Y2 as 497. Thus line number 80 gives Y1=Y2 and so the control is transferred to 110.

(B) Suppose we input year as 1989. The line number 60, we compute Y1 as 497. In line number 70, we get Y2 as 497.25. Thus in line number 80 Y1 is not equal to Y2, therefore control passes to 90.

27.5.2 MOD FUNCTION

Program 3

10 INPUT C, D

20 LET X= C MOD D

30 PRINT X

40 END

The MOD function determines the remainder on dividing a number by another. For example, 9 MOD 2 will give 1.

27.5.3 THE RND, LOCATE function

RND generates random numbers between 0 and 1.

Program 4

10 LET C= INT (RND*80)+1

20 LET R= INT( RND*25)+1

30 LOCATE R, C : PRINT "*"

40 GO TO 10

50 END

RND*80 and RND*25 generates random numbers between 0 to80 and 0 to25 respectively. Thus, in line number 10,20 C is assigned the integer value of RND*80 plus 1 and R is assigned the value RND*25 plus I respectively. The LOCATE clause followed by R,C followed by PRINT "*", prints a "*'in the Rth row an Cth column.

27.5.4 THE LEN, VAL, STR$ function

LEN is used to count the number of character in a string. It takes a string as an argument and counts every character including a blank.

Example

10 LET C$ = "BEAUTIFUL"

20 LET C = LEN (C$)

30 PRINT C

40 END

Output is 9 (as there are 9 characters in the word BEAUTIFUL. Note that the function LEN returns a numeric constant.

VAL(X$) function returns a numeric constant equivalent to the value of the number represented by the string X$. This string X$ should consist of all digits.

Example

10 LET X$= "12345"

20 LET Y = VAL(X$)

30 PRINT Y

40 END

Output of this program, on execution, will be 12345.

STR$(Y)

This is the reverse operation of VAL. The value of Y here is converted into a string literal. Y can be a numeric constant, variable or expression.

Example

10 LET Y =7384

20 LET X$=STR$(Y)

30 PRINT X$

40 END

The output will be "7384". The numeric value of Y is converted to string in line number 20, i.e. X$ = "7384"

27.5.5 String Processing in BASIC

Following are the few functions, which operate on string variables and help in string processing.

Left String

LEFT$ (X$,Y) Returns the left most Y characters from the string X$

Example 1

10 LET X$ = "MATHEMATICS"

20 PRINT LEFT$ (X$, 4)

30 END

The output will be MATH

Example 2

10 LET C$ ="INFORMATICS"

20 LET D$ = LEFTS(C$,6)

30 PRINT D$

40 END

Output will be INFORM.

Right string

RIGHT$(Y$,X) returns the rightmost X characters from the string Y$

Example 3

10 LET X$ = "PORTBLANK"

20 PRINT RIGHT$(X$,5)

30 END

Output will be BLANK.

Example 4

10 LET A$ = "MANAGED"

20 LET B$ = RIGHT$ (A$,4)

30 PRINT B$

40 END

Output will be AGED.

Middle String

MID$(X$,X,Y) Returns a sub string of X$ starting at the character position X from the left and containing Y characters.

Example 5

10 LET X$ = "MANHATTAN"

20 PRINT MID$(X$,4,3)

30 END

Output will be HAT.

 


Top

IN-TEXT QUESTIONS 4

1.

(a) What is the use of an INT function?

(b) What would be the value of X in the following program segments?

(i)      C$ = "AVAILABLE"

X = LEN(C$)

(ii)      C$ = "899"

X = VAL(C$)

2.  If A$ = "TO ERR IS HUMAN" B$ = "TO FORGIVE DIVINE", find the values of following functions:

(a) LEFT$(A$,2)

(b) MID$(A$, 2, 3)

(c) RIGHT$ (B$, 4)

(d) A$ +" " +B$

(e) MID$ (A$,4, 3) + MID$ (B$,5,2)

 


Top

27.6 EXAMPLE PROGRAMS

Problem 1

Write a program to invert TAERG to GREAT.

Program 1

5 LET L$ = " "

10 LET C$ = "TAERG"

15 L = LEN(C$)

20 FOR X = L TO 1 STEP -1

30 LET L$ = L$ +MID$(C$,X,1)

40 NEXT X

50 PRINT L$

60 END

Output GREAT

Problem 2

Write a program to give the word GRAPEFRUIT and FRUITGRAPE when GRAPE and are separately. Given

Program 2

10 LET F$ = "GRAPE"

20 LET B$ = "FRUIT"

30 LET C$ = F$ +B$

40 PRINT C$

50 LET A$ = B$ +F$

60 PRINT A$

70 END

Line 40 prints GRAPEFRUIT

Line 60 prints FRUITGRAPE

Problem 3

The prices of different articles are stored in a one-dimensional table, there product code is also given as:

 

Price in Rs.      20 15   16  18     14

Product Code 1     2     3    4       5

Let us assume that product code and quantity sold for an article is inputted through keyboard. Write a program to calculate and print the cost of sale along with product code and quantity sold. Terminate the procedure when the product code is out of range.

Program 3

10 DIM P (5)

20 FOR C = 1 TO 5

30 READ P(C)

40 NEXT C

50 DATA 20,15,16,18,14

60 INPUT "PRODUCT CODE"; C

70 IF C<1 OR C>5 THEN END

80 INPUT "QUANTITY SOLD"; Q

90 LET S = Q*P(C)

100 PRINT "PRODUCT CODE"; C ; "QUANTITY SOLD "; Q; COST OF SALE" ; S

110 GO TO 60

Line number 70 contains END.

Problem 4

A set of given numbers are there. Write a program to arrange them in descending order.

Program 4

10 REM PROGRAM TO ARRANGE THE GIVEN NUMBERS IN DESCENDING ORDER

15 DIM X (25)

20 INPUT "HOW MANY NUMBERS ARE THERE",N

50 FOR I = 1 TO N

60 READ X (I)

65    NEXT I

70 PRINT "ORIGINAL ORDER IS:"

80 FOR I = 1 TO N

90 PRINT X(I) ;

100 NEXT I

110 PRINT "NUMBERS IN DECREASING ORDER"

120  FOR I = 1 TO N-1

130  FOR J= 1 TO N-1

140 IF A (J)>=A (J+1) THEN 180

150 TEMP= A (J)

160 A (J)= A (J+1)

170 A (J+1) =TEMP

180 NEXT J

190 NEXT I

200 FOR I = 1 TO N

210 PRINT A (I);

220 NEXT I

230 END

Problem 5

From the word "MAIDAMS" write the message "MADAM IS MAD"

Program 5

10 LET A$ = "MAIDAMS'

20 PRINT LEFT$(A$, 2)+MID$(A$,4,3); MID$(A$,3,1)+

RIGHT$(A$, 1);LEFT$(A$,2)+MID$(A$,4,1)

30 END

Line number 20 can be put in two consecutive PRINT statement also.

Problem 6

Write a program to print the Prime Numbers between any two numbers A (say 1) and B (say 100).

Program 6

10 INPUT "TWO NUMBERS"; A,B

20 FOR N= A TO B STEP 1

30 IF N MOD 2= 0 OR N<= 1 THEN GO TO 80

40 FOR X=3 TO SQR(N) STEP 2

50 IF N MOD X =0 THEN 80

60 NEXT X

70 PRINT N; "IS THE PRIME NO"

80 NEXT N

90 END

Explanation

In the line number 30, N MOD 2 is same as MOD(N,2) it gives the remainder of N/2, similarly in line number 50,N MOD X means remainder of N/X.

 


Top

27.7 WHAT YOU HAVE LEARNT

In this lesson we have discussed various statements relating to decision making, looping and branching. Use of subroutine, sub-programs and arrays has been explained clearly by taking number of examples. These commands play very important role in writing programs. The illustrations given in this lesson will help you for a better grasp of BASIC programming. Usage of some BASIC functions has been clearly defined for the benefit of the students.

27.8 TERMINAL QUESTIONS

1. Write a BASIC program to run up the following series

(a) 1,3,5,7, 9 ………………100

(b) 2,4,6,8,10………………100

(c) 1,4,9,16,25………………100

(d) 1,8,27,64,125……………1000

2. What will be the output of the following programs?

(a)

10 FOR I = 1 TO 3

20 FOR J = 1 TO 5

30 PRINT I, J, I+J

40 IF J = 3 THEN 100

50 PRINT I, J , I*J

60 PRINT I+J, I-J

70 PRINT

100 NEXT J

110 NEXT I

120 END

(b)

10 FOR I = 1 TO 5

20 READ K

30 PRINT I ;

40 FOR J = 1 TO K

50 PRINT "* ";

60 NEXT J

70 PRINT

80 NEXT I

90 DATA 5,9,3,2,6

100 END

3. Write a program to invert LOOHCS to SCHOOL.

4. Write a program to write

PANKAJ KUMAR GOEL as P.K. GOEL.

5. Write a program to write your own address 10 times on the screen.


Top

27.9 FEEDBACK TO IN-TEXT QUESTIONS

IN-TEXT QUESTIONS 1

1.

(a) S

(b) I

(c) Positive odd

2.

(a)

40 NEXT J

50 NEXT K

(b )     50 NEXT I

3.

(a) 7 9 11

(b)

2

3

4

5

7

8

9

10

11


Top

IN-TEXT QUESTIONS 27.2

1.

(a) True

(b) True

(c) False

2.

(a) FAX(1,3,) = 15, FAX(2,1) = 25

(b) FAX(2,2) = 30, FAX(1,4) = 20

3.

4 4 5

4.

10 LET SMALL = X(1,1)

20 LET LARGE = X(1,1)

30 FOR I = 1 TO 4

40 FOR J = 1 TO 5

50 IF X(I,J) < SMALL THEN LET SMALL = X(I,J)

60 IF X(I,J) > LARGE THEN LET LARGE = X(I,J)

70 NEXT J

80 NEXT I

90 PRINT "SMALLEST VALUE ="; SMALL

100 PRINT "LARGEST VALUE ="; LARGE

110 END

5

10 DIM A(5)

20 REM READ NUMBER OF DATA VALUE

30 READ N

40 REM READ DATA VALUE INTO ARRAY A

50 FOR I =1 TO N

60     READ A(I)

70 NEXT I

80 REM NOW SORTING STARTS

90 FOR M = 1 TO N-1

120 IF A (M+1) > = A(M) THEN 18 0

130 REM INTERCHANGE OF DATA VALUES

140 LET P = A(M)

150 LET A (M) = A (M+1)

160 LET A(M+1) = P

180 NEXT M

190 REM PRINT THE SORTED ARRAY

200 FOR K = 1 TO N

210 PRINT A (K);

220 NEXT K

230 DATA 5, -71, -20, 14, 0, 5

240 END


Top

IN-TEXT QUESTIONS 3

1.

(a) A list of quantities can be given one variable name using a subscript. Such a variable is called a subscripted variable.

(b) It defines the number of memory locations required for the array name.

(c) It is a collection of statements, which requires to be repeated very frequently in a program. It is placed at the end of the program and may be referred to at different places in the main program using GOSUB statement.

(d) RETURN transfers the control back to the main program, to the statement immediately following the subroutine call.(i.e. GOSUB statement)

2. (a)

TWO

THREE

ONE

(b)

5

3

 

IN-TEXT QUESTIONS 4

1

(a) It takes numeric value as its argument and returns its value after truncating its decimal part.

(b)

(i) 9

(ii) 899 (As numeric value)

2

(a) TO

(b) O E

(c) TO ERR IS HUMAN TO FORGIVE DIVINE

(e) ERROR


Top