Advanced BASIC Programming Language III (One Dimensional Arrays)- SS3 NOTES



BASIC Programming Language III 

(One Dimensional Arrays) Operation on Arrays/Writing BASIC Program using loops


Definition of Array

1. An array is  a collection of elements of the same kind stored on a single variable.

2. An array is a data structure consisting of a collection of elements (value or variables, each identified by at least an array index or key.


What is Syntax Error in an Array?

A syntax error is an error resulting from violation of grammatical rules of  language. E.g (Python, C Sharp, Javascript e.t.c) 


How to Create an Array in BASIC Language

Syntax for Creating an Array:

To create an array, the DIM (dimension) command is used.
The DIM statement has the following syntax for a one-dimensional array:
DIM arrayName (n). Where "n" is a whole number.
For example DIM Score (5) will reserve 6 spaces, Score (0) Score (1), Score (2), Score (3), Score (4) and Score (5) in the memory to hold Numeric values. While DIM NAME$ (10) will reserve 11 memory locations to store string values, that is, NAME$(0) ... NAME$(10). The number inside the parentheses of the individual variables are called subscripts, and each variable is called a subscripted variable or element.
The syntax for a two dimensional array is:
DIM arrayName(m,n). "m,n" are whole numbers.


LOOPING

Looping is used to have the computer do repetitive tasks in a fraction of time.
The most common types of loops used in BASIC programming language are :

(i) FOR...NEXT 

(ii) WHILE WEND.

(iii) DO - WHILE 

(iv) NESTED LOOP

(v) INFINITE LOOP

Review of FOR – NEXT Statement

Example 1:
REM This program is to demonstrate the use of FOR-NEXT statement
FOR I = 1 TO 5
PRINT “The bluntest pen is better than the sharpest memory”
NEXT I
END


[run]
This program will display "the bluntest pen is better than the sharpest memory" five times

Example 2: Write a program to print the first ten integers.
Solution
REM program to print the first ten integers
FOR NUM = 1 TO 10
PRINT NUM
NEXT NUM
END

Example 3: Write a program to print
a. odd numbers from 1 to 20
b. even number from 2 to 30 using FOR –NEXT statement
Solution
REM program to print odd numbers from 1 to 20
PRINT “odd numbers from 1 to 20 are”
FOR ODD =1 TO 20 

PRINT ODD

NEXT ODD
END

(b) REM program to print even numbers from 2 to 30
PRINT “even numbers from 2 to 30 are”
FOR EVEN =2 TO 30 
PRINT EVEN
NEXT EVEN
END

Example 4: write a program to add odd numbers from 1 t0 20
Solution
REM program to print odd numbers from 1 to 20
PRINT "odd numbers from 1 to 20 are"
LET SUM = 0
FOR ODD = 1 TO 20 
PRINT ODD
LET SUM = ODD + SUM
NEXT ODD
PRINT “The sum of odd numbers from 1 to 20 is"; SUM
END

 WHILE -WEND Statement

“While WEND” examines the condition of a variable. “While” the variable meets the stated criteria performing a loop until the variable's condition no longer meets the stated criteria.

Example 1
REM program to demonstrate the use of WHILE – WEND statement
CLS
LET A= 1
WHILE A< 11
PRINT “Hello World”
A = A+1
WEND
END

Example 2:
Write a program to print the square of even numbers from 6 to 22 using WHILE – WEND Statement

REM program to print the square of numbers
CLS
LET N = 6
WHILE N<23
PRINT “the square of”; N; “is”; N*N
N=N+2
WEND
END

More Examples using DIM, FOR-NEXT and

 WHILE – WEND statements

1. create and access an array of 10 integers using FOR – NEXT Statement
Solution
REM an array to create and access an array of 10 integers
CLS
DIM IN(10)
FOR I = 1 TO 10
INPUT "ENTER THE NUMBER"; IN(I)
NEXT I
PRINT "IN(3) is"; IN(3)
END

2. Calculate the average of a one-dimensional array with 100 numeric values.
REM an array to calculate the average of 100 numbers
CLS
DIM IN(100)
LET SUM = 0
FOR I = 1 TO 100
INPUT "enter the next number"; IN(I)
LET SUM = SUM + IN(I)
NEXT I
LET AVERAGE = SUM / 100
PRINT "average of 100 numbers is"; AVERAGE
END

3. Calculate the area of 10 different rectangles using the WHILE – WEND statement
REM Program to calculate the area of 10 different rectangles
CLS
DIM LENGTH(10)
DIM WID(10)
DIM AREA(10)
LET I = 1
WHILE I < 11
INPUT "enter the length of the rectangle"; LENGTH(I)
INPUT "enter the width of the rectangle"; WID(I)
LET AREA(I) = LENGTH(I) * WID(I)
PRINT "the area of the rectangle is"; AREA(I)
I = I + 1
WEND
END

Comments

Popular posts from this blog

MID TERM ASSIGNMENT JS1 FOR SECOND TERM 24/25 ACADEMIC SESSION

MID TERM PROJECT - COMPUTER STUDIES (JS2)

MID TERM PROJECT FOR JS1 (MASTERING THE KEYBOARD)