It is a special type of variable that holds a collection of related data having the same data-type. The individual data item in an array is called an Element.
Advantages of an Array
→ Array can store multiple values of same data type. So, it reduced the number of variables.
→ Program will be more efficient and well managed.
→ Sorting and searching the data can be done easily.
Array Declaration
Before using an array, it must be declared. For this, we need to use DIM statement.
One dimensional array
Syntax:
DIM Array_Name(subscript)
- A subscript is a value that is used to declare the size of array.
Example:
DIM N%(5)
Here the array name is N with data-type integer and value of subscript is 5, that is you can store maximum 5 integers in this array.
Storing data in an Array
CLS
DIM N%(5)
FOR i = 1 TO 5
INPUT "Any integer "; N%(i)
NEXT i
END
Sample Program
1. REM To find the sum and average of 10 integers
DIM n%(10)
FOR i = 1 TO 10
INPUT "Any integer "; n%(i)
sum = sum + n%(i)
NEXT i
avg = sum / 10
PRINT "Sum = "; sum
PRINT "Average = "; avg
END
Searching data in an Array
Linear search or sequential search
Once the data are stored in an array, we can search a particular data by comparing each element stored in the array one after another in a sequential order. This type of search method is called linear search. The search process stops when either the value is found or the end of the array is encountered.
Program of Sequential/linear search
REM Linear search from the data
CLS
DIM n(10)
FOR i = 1 TO 10
READ n(i)
NEXT i
INPUT "Type a number to search"; x
FOR a = 1 TO 10
IF n(a) = x THEN
check = 1
END FOR
END IF
NEXT a
IF check = 1 THEN
PRINT "Data Found!!!"
ELSE
PRINT "Data Not Found!!!"
END IF
DATA 44,34,21,56,76,87,44,51,29,39
END
Sorting data in an Array
Sorting means the arrangement of data in ascending or descending order numerically or alphabetically. Data can be sorted in various methods like Quicksort, Insertion sort, Bubble sort, Heap sort, Shell sort etc. But we are using only bubble sort. Normally, we sort the data in order to search the data in an efficient manner.
Bubble sort
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
Program of Bubble sort
REM Bubble sort
CLS
DIM n(5)
FOR i = 1 TO 5
READ n(i)
NEXT i
FOR x = 1 TO 5
FOR y = 1 TO 5 - x
IF n(y) > n(y + 1) THEN SWAP n(y), n(y + 1)
NEXT y
NEXT x
PRINT "Sorted data..."
FOR x = 1 TO 5
PRINT n(x);
NEXT x
DATA 44,34,21,4,9,9
END
Two dimensional array
In two dimensional array, the data are stored in a tabular form in rows and columns.
Syntax:
DIM array_name(m,n)
- m, a number that declares the number of rows
- n, a number that declares the number of columns
Example:
DIM N%(3,4)
CLS
DIM N%(3, 4)
FOR x = 1 TO 3
FOR y = 1 TO 4
INPUT "Any number "; N%(x, y)
NEXT y
NEXT x
END
Here, the number of rows is 3 and number of columns is 4. So, the data will be stored in the following format.
Summary
The array is a data structure that holds a collection of data, all of the same type and accessed using a common name.
Types of Array: One-dimensional, Two-dimensional
Array is declared using DIM statement.
Array makes sorting and searching the data easy and fast.
Comments
Post a Comment