0% found this document useful (0 votes)
2 views23 pages

Classnotes Student 23909546658 Week3e-Notecomputersss3basicprogrammingthree

The document provides a comprehensive overview of arrays in programming, defining them as collections of elements of the same type that can be accessed using indices. It covers various types of arrays, how to declare and create them using the DIM statement, and operations that can be performed on arrays, including looping mechanisms like FOR...NEXT and WHILE...WEND. Additionally, it includes examples of BASIC programs that demonstrate the use of arrays and looping structures.

Uploaded by

jibakin4life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Classnotes Student 23909546658 Week3e-Notecomputersss3basicprogrammingthree

The document provides a comprehensive overview of arrays in programming, defining them as collections of elements of the same type that can be accessed using indices. It covers various types of arrays, how to declare and create them using the DIM statement, and operations that can be performed on arrays, including looping mechanisms like FOR...NEXT and WHILE...WEND. Additionally, it includes examples of BASIC programs that demonstrate the use of arrays and looping structures.

Uploaded by

jibakin4life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

WEEK 3

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.

An array allows these related variables to be referred to by the same name,


and to use a number called an index or a subscript, to tell them apart.

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.

An array is a collection of values stored in a single variable.

An array is called a subscripted variable because when we need to access a


certain element; we must use a subscript to point to that element so we can
differentiate it from the rest.

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$

NM$ (1) Olaleye

NM$ (2) Popoola

This means Olaleye is in the first position in the array.

And Popoola is in the second position in the array.

The subscript enclosed in the parentheses can be a numeric constant,


numeric variable, or arithmetic expression, for example:

X (T)

T (3)

P (4 + G) are valid reference to array elements.

In Array X (T), the computer references the current value of T, which


indicates the position of the desired element in array X.

Example 2: Given array P as 2

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:

P(X) refers to P(1), i.e. the first element in the array, 2

P(Y) refers to P(2), i.e. the second element in the array, 4

P(Z+1) refers to P(4), i.e. the fourth element in the array, 8

Types of Dimensional Arrays


1. One-dimensional array
2. Two-dimensional array
3. Three-dimensional array
4. Multi-dimensional array
A one-dimensional array is called LIST and

A two-dimensional array is called TABLE.

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.

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.

The form of declaring a one-dimensional array is as follows:

DIMarrayName (startIndex TO endIndex) e.g. DIM A (1, 10)

Where: arrayName is the variable name of the array,

startIndex is the starting subscript value, and

endIndex is the ending subscript value.

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

Dimension Statement (DIM):


Specifies the maximum values for any variable subscripted, and allocate
storage accordingly. The default is 10.
Syntax:

DIM variable (elements), variable (elements) …

Example:

DIM A (3), B (8)

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

out on an array. This includes Input, Processing and Output operations.

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

instructions to all or some elements of the array; it is more convenient to

put the instructions within a LOOP.

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

Example 1: FOR…NEXT LOOP

FOR cntr = 1 TO 100

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

added to the variable “total.”

This type of loop might be used to calculate the result of depositing 10

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.

Example 2: WHILE…WEND LOOP


WHILE total < 1000
total = total + 10
total = total + (total * (.05/52))
week = week + 1
PRINT total, week
WEND
This WHILE/WEND loop also deposits 10 Naira per week to the savings

account, at the same time it computes an annual interest of 5% divided by

52 weeks and adds that to the total savings.

When the total amount saved is equal or greater than N1, 000,

execution continues to the next block of code.

Example 3: DO…LOOP UNTIL


DO
total = total + 10
total = total + (total * (.05/52))
LOOP UNTIL total > 1000

This DO/LOOP UNTIL executes until total > N1, 000.

Example 4: DO…LOOP WHILE


DO
total = total + 10
total = total + (total * (.05/52))
week = week + 1
LOOP WHILE week < 100

In this DO/LOOP WHILE, executes for only 100 weeks.

Looping is often used to fill arrays, load images or any task


which is repetitive in nature. Sometimes the choice of loop depends upon
what we wish to do with it.

In the FOR/NEXT and WHILE/WEND loops, the comparison is


made at the top of the loop, before the action inside the loop is performed.

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.

It is a syntax error to use BASIC reserved words as variable names.


Variable names must be preceded by letters or alphabets.

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.

Examples of numeric variable are: A1, B2, Love, Book5 etc.

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 (“ “).

It is usually represented by alphabets or as a numeric with or followed by a


dollar sign ($). It is the dollar sign that made it a variable e.g. “Welcome”;
B$ etc.

A character string is differentiated from a numeric data by enclosing the


string in quotation marks.

S/N NUMERIC VARIABLE STRING VARIABLE


1. A A$
2. C2 C2$
3. Book “Book”
4. Welcome “Welcome
5. Hello “Hello”
6.

SIMPLE BASIC PROGRAM

Write simple BASIC program on array involving one- dimensional array:

(i) FOR…NEXT statement

(ii) WHILE …END statement

(iii) DIM statement

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

OTHER EXAMPLES OF SIMPLE BASIC


PROGRAM
EXAMPLE 1:
Write a BASIC program to put 10 integers into the memory and print them
on the screen.

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

20 REM USING DIM Statement

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

60 LET TOTAL = A+B+C+D+E+F+G+H+I+J

70 LET AVERAGE = TOTAL/10

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

FOR NUMBER = 1 T0 100

LET SUM = SUM + NUMBER

PRINT SUM

NEXT NUMBER

PRINT “The Total Sum Is”, SUM

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

30 INPUT “ENTER LENGTH”; L

40 INPUT “ENTER BREADTH”; B

50 LET AREA = L*B

60 PRINT “AREA = “; AREA

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

30 WHILE X < 100

40 INPUT “ENTER LENGTH”; L

50 INPUT “ENTER BREADTH”; B

60 LET AREA = L*B

70 PRINT “AREA = “; AREA

80 X=X+1

90 WEND

100 END
ASSIGNMENT
1. Define an array and give its syntax declaration.

2. Mention the statements in BASIC.

3. State the syntax for the statements stated above

2. Write a BASIC program to store the scores of your classmates in


computer test in array TEST (30).

2. Write a BASIC program to output the values of the elements in array


TEST (30).

3. Write a program that reads 10 values into array score of 10 elements.

4. Write a program to display the standard deviation of 100 sets of


numbers already stored in an array X.

5. Write a BASIC program to calculate the area of a 10 different triangles


with and without the WHILE…END statements.

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)

You might also like