BASIC Programming III (One Dimensional Array)
BASIC Programming III (One-Dimensional Array)
CLASS: SSS Three
Definition of Array
An array is a list or collection of variables that all store the same kind of data.
Definition of One-Dimensional Array
A one-dimensional array is a linear collection or arrangement of variables of the same kind. This means the data is organized in a single row or column, and each piece of data (an element) is accessed using a single number, called a subscript or index.
How to Create an Array in BASIC Language
To create an array in BASIC, we use the DIM (short for DIMension) command. This command tells the computer to reserve a specific amount of memory for your array.
The syntax for a one-dimensional array is:
DIM arrayName(n)
Where "n" is a whole number that represents the highest index you want to use for the array. It's important to remember that in many versions of BASIC, arrays start counting from index 0 (zero-based indexing). This means `DIM Score(5)` actually creates 6 memory spaces:
Score(0)
Score(1)
Score(2)
Score(3)
Score(4)
Score(5)
Similarly, `DIM NAME$(10)` will reserve 11 memory locations to store string values (text), from `NAME$(0)` up to `NAME$(10)`. The `$` sign indicates a string array.
Each individual variable within the array (e.g., `Score(3)` or `NAME$(5)`) is called an element, and the number inside the parentheses is its subscript or index.
The syntax for a two-dimensional array (which organizes data in rows and columns) is:
DIM arrayName(m,n)
Where "m" represents the highest row index and "n" represents the highest column index.
Operations on an Array
Many common programming operations can be performed on an array:
- a. Input operation: This allows you to store data into the individual elements of an array. You can use commands like
INPUT
(for user input) orREAD
(for data defined within the program). - b. Output operation: You can retrieve and display the data stored in an array's elements using commands like
PRINT
. - c. Arithmetic operation: For arrays holding numeric values, you can perform simple to complex mathematical calculations on their elements (e.g., adding two elements, finding an average, etc.).
Simple One-Dimensional Array Programs in BASIC Programming Language
Example 1: Create and access an array of 10 integers
Solution
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:
14
THE SUM OF IN(2) AND IN(7) IS 21
Example 2: Create an array to access your favourite day of the week
Solution
REM Array to create and access your favourite 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 (1-7): "; n
PRINT "My favourite day of the week is "; DAY$(n)
END
[run] Example Interaction:
Enter the number that corresponds to your favourite day of the week (1-7): ? 4
My favourite day of the week is Wednesday
LOOPING
Looping is a powerful programming concept used to make the computer do repetitive tasks many times in a fraction of the time it would take to write out each step individually. It makes programs more efficient and shorter.
The most common types of loops used in BASIC programming language are the FOR...NEXT loop and the WHILE...WEND loop.
Review of FOR – NEXT Statement
Example 1: Demonstrating the use of FOR-NEXT statement
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] Output:
This program will display "The bluntest pen is better than the sharpest memory" five times.
The bluntest pen is better than the sharpest memory
The bluntest pen is better than the sharpest memory
The bluntest pen is better than the sharpest memory
The bluntest pen is better than the sharpest memory
The bluntest pen is better than the sharpest memory
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
[run] Output:
1
2
3
4
5
6
7
8
9
10
Example 3: Write a program to print:
a. 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:"
FOR ODD = 1 TO 20 STEP 2
PRINT ODD
NEXT ODD
END
[run] Output:
Odd numbers from 1 to 20 are:
1
3
5
7
9
11
13
15
17
19
b. Even numbers from 2 to 30 using FOR – NEXT statement
Solution:
REM program to print even numbers from 2 to 30
PRINT "Even numbers from 2 to 30 are:"
FOR EVEN = 2 TO 30 STEP 2
PRINT EVEN
NEXT EVEN
END
[run] Output:
Even numbers from 2 to 30 are:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
Example 4: Write a program to add odd numbers from 1 to 20
Solution:
REM program to add odd numbers from 1 to 20
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
[run] Output:
1
3
5
7
9
11
13
15
17
19
The sum of odd numbers from 1 to 20 is 100
Review of WHILE - WEND Statement
Example 1: Demonstrating the use of WHILE – WEND statement
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
[run] Output:
This program will display "Hello World" ten times.
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Example 2: Write a program to print the square of even numbers from 6 to 22 using WHILE – WEND Statement
Solution:
REM program to print the square of even numbers
CLS
LET N = 6
WHILE N < 23
PRINT "The square of "; N; " is "; N * N
N = N + 2
WEND
END
[run] Output:
The square of 6 is 36
The square of 8 is 64
The square of 10 is 100
The square of 12 is 144
The square of 14 is 196
The square of 16 is 256
The square of 18 is 324
The square of 20 is 400
The square of 22 is 484
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 "The value of IN(3) is "; IN(3)
END
[run] Example Interaction:
ENTER THE NUMBER ? 5
ENTER THE NUMBER ? 8
ENTER THE NUMBER ? 12
ENTER THE NUMBER ? 15
ENTER THE NUMBER ? 2
ENTER THE NUMBER ? 7
ENTER THE NUMBER ? 9
ENTER THE NUMBER ? 1
ENTER THE NUMBER ? 4
ENTER THE NUMBER ? 10
The value of IN(3) is 12
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 "The average of the 100 numbers is "; AVERAGE
END
```
[run] Example Interaction:
(This program will ask for 100 numbers. Let's assume you enter numbers that sum up to 5000.)
Enter the next number: ? 25
Enter the next number: ? 75
... (98 more input prompts)
Enter the next number: ? 50
The average of the 100 numbers is 50
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
PRINT "For Rectangle "; I
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 this rectangle is "; AREA(I)
PRINT "" ' Print an empty line for spacing
I = I + 1
WEND
END
[run] Example Interaction:
For Rectangle 1
Enter the length of the rectangle: ? 5
Enter the width of the rectangle: ? 4
The area of this rectangle is 20
For Rectangle 2
Enter the length of the rectangle: ? 7
Enter the width of the rectangle: ? 3
The area of this rectangle is 21
... (and so on for 10 rectangles)
Prepare 10 array minimum of 10 element maximum of infinity
ReplyDeleteThis website is a blessing to me as ICT/Data Processing teacher that I am. All Thanks
ReplyDeleteThanks Echofu George
DeleteGod bless the person on the back end of this website. You've been a great help to me.
ReplyDeleteAMEN OOO
Deletethanks alot
ReplyDeleteWhich IDE is the best for running this
ReplyDeleteThank you so much for this note
ReplyDeleteThanks so much sir, I really appreciate 🙏🙏💜💚
ReplyDelete