0% found this document useful (0 votes)
24 views

B 29 CDF

The document discusses one dimensional arrays in basic programming. It defines what an array is, how to create an array using the DIM statement, and different operations that can be performed on arrays. It provides examples of creating and accessing integer and string arrays, as well as using FOR-NEXT and WHILE-WEND loops to iterate through arrays.

Uploaded by

Etta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

B 29 CDF

The document discusses one dimensional arrays in basic programming. It defines what an array is, how to create an array using the DIM statement, and different operations that can be performed on arrays. It provides examples of creating and accessing integer and string arrays, as well as using FOR-NEXT and WHILE-WEND loops to iterate through arrays.

Uploaded by

Etta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UC2 WEEK 9

TOPIC: BASIC PROGRAMMING III


Sub-Topic: One Dimensional Array

Definition of Array

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

To create an array, the DIM (dimension) command is used. The DIM statement has the following syntax:

DIM arrayName(n)

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 value. The number inside the parentheses of the individual

variables are called subscripts, and each individual variable is called subscripted variable or

element

Operations on Array

The following operation can be performed on an array. They are;

a. Input operation

b. Output operation

c. Arithmetic operation

Example1: 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 of the day of the week

REM Array to create and access your favourite the day of the week

REM Array to create and access the days 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 favorite day of the week"; n
PRINT "My favorite 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

loop used in QBASIC programming is the FOR...NEXT and WHILE WEND loop that repeats a series of

instructions a specified number of times.

Review of FOR – NEXT Statement

Example 1:

FOR I = 1 TO 5

PRINT “the bluntest pen is better than the sharpest memory”

NEXT I

END

The program will print the dullest pencil 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

*Solve (b) part as class work/assignment.

Example 4 write a program to add odd numbers from 1 to 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 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 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

You might also like