BASIC Programming III: One-Dimensional Array
💻 BASIC PROGRAMMING III: ONE-DIMENSIONAL ARRAY
📘 Introduction
In programming, especially when using BASIC, we often need to store and process a group of related data values. Instead of creating many individual variables, we can use a structure called an array. Arrays make programs shorter, more efficient, and easier to manage.
🔹 What is an Array?
An array is a collection of data items of the same type stored under one variable name. Each item in the array is called an element, and each element is identified by a unique number known as its index or subscript.
Example:
If we want to store the marks of 5 students, instead of using M1, M2, M3, M4, M5, we can simply use one array variable M(5).
🧩 Types of Arrays
There are two main types of arrays:
- One-Dimensional Array: Stores data in a single line or list. Example: student scores, prices, names.
- Two-Dimensional Array: Stores data in rows and columns (like a table or spreadsheet).
This lesson focuses on the one-dimensional array.
📗 One-Dimensional Array
A one-dimensional array is a list of elements stored in a single line, all of which are accessed using a single index.
Example:
DIM MARK(5)
This declares an array named MARK that can store 5 elements: MARK(1) to MARK(5).
🧮 Declaring an Array in BASIC
Before using an array in BASIC, it must be declared using the DIM statement (short for “Dimension”).
Syntax:
DIM ArrayName(Size)
Example:
DIM SCORE(10) – declares 10 memory spaces for variable SCORE.
💡 Assigning Values to an Array
We can assign values to each element of the array using its index.
10 DIM SCORE(5) 20 FOR I = 1 TO 5 30 INPUT "ENTER SCORE "; SCORE(I) 40 NEXT I
Here, the program allows a user to enter 5 scores, which are stored in the array SCORE.
🔁 Processing or Accessing Array Elements
You can access or display array values using a loop, for example:
50 FOR I = 1 TO 5 60 PRINT "SCORE "; I; " = "; SCORE(I) 70 NEXT I
This prints out all the scores entered.
🧠 Example Program: Find Total and Average
10 DIM SCORE(5) 20 S = 0 30 FOR I = 1 TO 5 40 INPUT "ENTER SCORE "; SCORE(I) 50 S = S + SCORE(I) 60 NEXT I 70 A = S / 5 80 PRINT "TOTAL = "; S 90 PRINT "AVERAGE = "; A 100 END
Explanation:
- Line 10: Declares the array
SCORE. - Line 20: Initializes the total variable
Sto zero. - Lines 30–60: Loop to input scores and add them up.
- Line 70: Calculates the average.
- Lines 80–90: Displays the total and average.
🧩 Array Operations
Common operations that can be performed on arrays include:
- Reading (input values into an array)
- Processing (performing calculations using array values)
- Displaying (printing array elements on the screen)
📊 Advantages of Using Arrays
- They reduce the number of variables in a program.
- They make programs more organized and readable.
- They allow for easy data manipulation using loops.
- They make calculations involving many values simpler.
⚠️ Disadvantages of Arrays
- All elements must be of the same data type.
- The size of the array must be known before use.
- They occupy continuous memory space.
🧩 Key Points to Remember
- Array elements are accessed by index numbers.
- Array indexing in BASIC usually starts from 1.
DIMis used to declare arrays.- Loops (FOR...NEXT) are used to process arrays easily.
🧠 Class Activity
- Define an array and give one example.
- Write a BASIC program to enter and print 6 student names using arrays.
- Explain the function of the
DIMstatement in BASIC.
🏠 Take-Home Assignment
- State two advantages of arrays in programming.
- Write a BASIC program to find the largest number in a one-dimensional array of 5 numbers.
✅ Conclusion
One-dimensional arrays are essential tools in BASIC programming. They help programmers store and process large sets of related data efficiently. Understanding how to declare, assign, and process arrays will help you write cleaner and smarter programs.
Comments
Post a Comment