Classnotes Student 23909546658 Week3e-Notecomputersss3basicprogrammingthree
Classnotes Student 23909546658 Week3e-Notecomputersss3basicprogrammingthree
BASIC PROGRAMMING 3
DEFINITION OF AN ARRAY
An array is a series of elements of the same type placed in contiguous
memory locations that can be individually referenced by adding an index to
a unique identifier.
An array is a list of variables with the same data type and name. It is either
an integers or floats, where one variable is used only when we work with a
single item.
The individual’s variables are called the elements of the array. An array is
a list of variables of the same datatype. Arrays are useful for organizing
multiples variables.
EXAMPLES OF ARRAYS
A subscript is a value in parentheses that indicates a position within an
array. The subscripts are enclosed in parentheses.
Example 1: if there are two students in a computer class their names
can be stored in an array called NM$.
Array NM$
X (T)
T (3)
8
Assume that X = 1, Y = 2 and Z = 3, state the value and position of the
arrays: P(X), P(Y), and P(Z+1).
SOLUTION:
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.
DECLARING / CREATING AN
ARRAY
To create an array, use the DIM (dimension) command.
DECLARING / CREATING AN
ARRAY
In order to declare an array, we must use a DIM statement which plays the
role of reserving internal memory space for an array of any desired size.
DECLARING / CREATING AN
ARRAY
Like a regular variable, an array must be declared before it is used. A typical
declaration for an array in BASIC is by using Dimension Statement (DIM).
Example:
Meaning:
This reserves 3 locations for array A and 8 locations for array B respectively.
OPERATIONS ON AN ARRAY
All operations that can be performed on a variable can equally be carried
1. Input of an array
2. Output of an array
3. Arithmetic on array
To carry out any operation on an array involves applying the same set of
LOOPING
WHAT IS LOOPING?
LOOP can be defined as a set of instructions in a computer program that is
repeated in a particular number of times or until a specific objective has
been achieved.
Looping is often used to fill arrays, load images or any task which is
repetitive in nature.
FORMS OF LOOPING
The proper forms of looping are using:
1. FOR…NEXT
2. WHILE…WEND
3. DO…LOOP WHILE
4. DO…LOOP UNTIL
Total = total + 10
NEXT cntr
The FOR/NEXT loop above will execute 100 times before execution
drops to the next block of code. With each pass through the loop, 10 will be
Naira from each week’s paycheck into a savings account for 100 weeks.
Inside the loop, the programmer can include additional code, such as
calculating interest for the week, by multiplying total * (int/52) and adding
to the total.
When the total amount saved is equal or greater than N1, 000,
This is the best choice for reading data statements or loading images
because the comparison is made before the action is performed.
If all the data statements have been read, or all images have been
loaded, execution will drop to the next block of code before we will
encounter an “Input past end…” or “File not found…” error.
VARIABLES
Variables are containers of data. They are the names which you give to
represent some quantities. Its values can change and can have variety of
values at different times.
Variables have names that differentiate them from others. The valid
and common variable names should be formed from alphabets A-Z, digits 0-
9.
TYPES OF VARIABLES
There are two major types of variables in BASIC.
1. Numeric variable
2. String variable
1. NUMERIC VARIABLE
These are used to hold numbers, integers, real numbers or exponential
form. There are 286 numeric variable names allowed by standard BASIC.
2. STRING VARIABLE
String variables are used to hold string of characters, letters and symbols.
The string can be up to 255 characters in length but it must be enclosed on
double quotes (“ “).
1. FOR…TO…NEXT Statement
The FOR…TO…NEXT statement is a loop statement used to repeat certain
portion of a program a number of times.
For example, like in the program below, lines 20, 40 will be repeated 5
times, that is, for five students and after that the program ends.
EXAMPLE:
10 FOR I = 1 TO 5
20 INPUT “Enter Name”; N$
30 INPUT “Enter Course”; C$
40 INPUT “Enter Age”; A1
50 NEXT I
60 END
2. WHILE…WEND statement:
The WHILE…WEND command is used in a loop until a specified expression is
false.
To use WHILE…WEND:
i. Place an expression after WHILE
ii. Enter a list of commands
iii. Place WEND at the end
EXAMPLE
10 CLS
20 X = 10
30 WHILE X < 20
40 PRINT X
50 X=X+1
60 WEND
70 END
Output
10
11
12
13
14
15
16
17
18
19
3. DIM Statements
The DIM is a short form for DIMENSION; it is a declarative statement in
BASIC language. It is used to declare a variable or an array with respect to
its datatype.
Syntax
DIM <Variable_Name> AS <Data_Type> for variable declaration
DIM <Variable_Name> (<Length_of_Array>) for array declaration
Example 1:
10 CLS
20 DIM AGE AS INTEGER
30 DIM CLASS AS INTEGER
40 DIM FEES AS INTEGER
50 AGE = 25
60 CLASS = 3
70 FEES = 20,000
80 PRINT AGE, CLASS, FEES
90 END
Example 2:
10 CLS
20 DIM VAR1 AS STRING
30 DIM VAR2 AS STRING
40 VAR1 = IKECHUKWU
50 VAR2 = AYOMIDE
70 PRINT VAR1, VAR 2
80 END
SOLUTION:
DIM A (10)
FOR I = 1 TO 10
READ A (I) // OR INPUT A (I) in an interactive session, i.e. through keyboard
PRINT A (I) // The PRINT statement output data/value of A (I) on the screen
NEXT I
DATA 80, 60, -10, 11, 100, 8, 99, 67, 9, 14
//DATA statement not required if INPUT statement is used
END
The above program can be seen diagrammatically below:
A(1) A(2) A(3) A(4) A(5) A(6) A(7) A(8) A(9) A(10)
80 60 -10 11 100 8 99 67 9 14
EXAMPLE 2:
Write a BASIC program to calculate the average of a one dimensional array
with 100 numeric values.
SOLUTION:
REM using WHILE…WEND
DIM A (100)
count = 1
total = 0
WHILE count < 101
total = total + A (count)
count = count + 1
WEND
PRINT “Total = “, total
PRINT “Average = “, total/100
END
OR
REM using FOR…NEXT
DIM A (100)
count = 1
total = 0
FOR count = 1 TO 100
total = total + A (count)
count = count + 1
NEXT
PRINT “Total = “, total
PRINT “Average = “, total/100
END
EXAMPLE 3:
Write a BASIC program to calculate the average of a one dimensional array.
SOLUTION:
10 CLS
30 DIM A, B, C, D, E, F, G, H, I, J AS INTEGER
40 READ A,B,C,D,E,F,G,H,I,J
50 DATA 10,20,15,25,40,50,60,40,50,20
80 PRINT TOTAL
90 PRINT AVERAGE
100 END
EXAMPLE 4:
Write a BASIC program to calculate the area of 10 triangles.
SOLUTION:
REM using WHILE…WEND
count = 1
WHILE count < 11
Input “base”, B
Input “height”, H
Area = (B*H)/2
PRINT “AREA =”, Area
count + 1
WEND
END
OR
REM using FOR…NEXT
FOR J = 1 TO 10
Input “base”, B
Input “height”, H
Area = (B*H)/2
PRINT “AREA =”, Area
NEXT J
END
EXAMPLE 5a:
Write a BASIC program to output the sum of the first 100 integers.
SOLUTION:
REM using WHILE…WEND
count = 1
sum = 0
WHILE count < 101
PRINT count * 2
WEND
END
OR
REM using FOR…NEXT
count = 1
sum = 0
FOR I = 1 TO 100
PRINT count * 2
NEXT I
END
EXAMPLE 5b:
Write a BASIC program to output the sum of the first 100 integers.
SOLUTION
REM PROGRAM TO SUM THE FIRST 100 INTEGERS
LET SUM = 0
PRINT SUM
NEXT NUMBER
END
EXAMPLE 6:
Write a BASIC program to calculate the area of 10 different rectangles with
FOR…NEXT statement.
SOLUTION:
10 CLS
20 FOR I = 10 TO 100
70 NEXT I
80 END
EXAMPLE 7:
Write a BASIC program to calculate the area of 10 different rectangles with
WHILE…WEND statement.
SOLUTION:
10 CLS
20 X=1
80 X=X+1
90 WEND
100 END
ASSIGNMENT
1. Define an array and give its syntax declaration.
ASSIGNMENT
NEW COMPUTER STUDIES
BY J.O.E. OTUKA ET.AL.
LEARN AFRICA
FOR S.S.S. 3
PAGE 18
1. MULTIPLE CHOICE QUESTIONS
QUES 1 - 5 (OBJ)
2. ESSAY-TYPE QUESTIONS
THEORY (QUESTIONS 1 - 4)