BASIC Programming III (One Dimensional Array)

BASIC Programming III (One-Dimensional Array)

CLASS: SSS Three

Definition of Array

An array is a list of variables of the same kind. A variable is a name the computer assigns value to.

Definition of One-Dimensional Array

A one-dimensional array is a linear collection or arrangement of variables of the same kind.

How to Create an Array in BASIC Language

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.

Operations on an Array

Many operations can be performed on an array. Some include:
a. Input operation: With this operation, data can be entered into the subscripted variable of an array
b. Output operation: Data stored into in an array can be accessed.
c. Arithmetic operation: Simple to complex arithmetical operations can be performed on an array.

Simple One-Dimensional array Programs in BASIC Programming Language

Example 1: Create and access array of 10 integers
REM An array to create and access 10 integers
DIM IN (10)
IN (1) = 10
IN (2) = 11
IN (3) = 12
IN (4) = 13
IN (5) = 14
IN (6) = 15
IN (7) = 10
IN (8) = 11
IN (9) = 12
IN (10) = 13
PRINT IN (5)
PRINT “THE SUM OF IN (2) AND IN (7) IS”; IN (2) + IN (7)
END

[run]
OUTPUT: THE SUM OF IN (2) AND IN (7) IS 21

Example 2: Create an array to access your favourite day of the week
REM Array to create and access your favourite the day of the week
DIM DAY$(7)
DAY$(1) = "Sunday"
DAY$(2) = "Monday"
DAY$(3) = "Tuesday"
DAY$(4) = "Wednesday"
DAY$(5) = "Thursday"
DAY$(6) = "Friday"
DAY$(7) = "Saturday"
INPUT "enter the number that corresponds to your favourite day of the week"; n
PRINT "My favourite day of the week is"; DAY$(n)
END


[run]

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 the FOR...NEXT and WHILE WEND.

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 STEP 2
PRINT ODD
NEXT ODD
END

(b)
REM program to print even numbers from 2 to 30
PRINT “odd numbers from 2 to 30 are”
FOR EVEN =2 TO 30 STEP 2
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 STEP 2
PRINT ODD
LET SUM = ODD + SUM
NEXT ODD
PRINT “The sum of odd numbers from 1 to 20 is"; SUM
END

Review of WHILE -WEND Statement

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

  1. Prepare 10 array minimum of 10 element maximum of infinity

    ReplyDelete
  2. This website is a blessing to me as ICT/Data Processing teacher that I am. All Thanks

    ReplyDelete
  3. God bless the person on the back end of this website. You've been a great help to me.

    ReplyDelete
  4. Which IDE is the best for running this

    ReplyDelete
  5. Thank you so much for this note

    ReplyDelete

Post a Comment

Popular posts from this blog

90 Objective Examination Questions in Major Subjects

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

JSS One Objective Questions in Computer Studies/Information Technology