You are on page 1of 6

SHAY-IT

EDUCATION
HOTLINE: 59859898

A LEVEL NOTES & PRACTICALS


LABSHEET 2
1D ARRAYS & LINEAR SEARCH

Syllabus Objective:
Declare and use one-dimensional (1D) arrays.
N.B:
Students are required to spend no more than 2 hours working out this
Labsheet.
After completion, students should save the evidence document as a
pdf and upload it on the google classroom platform. Each Labsheet
has a different submission, so, please make sure you submit your file
to the respective allocated submission. Submission in pdf format is
exclusively for ease of correction; during examinations, you will not be
required to save your work as a pdf.
For any query or difficulty understanding or implementing the
different objectives of the Labsheet, students can ask the lab
assistant, or the teacher for help. We will be there for you!

© SHAY-IT EDUCATION, 2023


LIBRARY ROUTINES – RANDOM NUMBER GENERATOR & ROUNDING
ROUND(<identifier>, <places>)
Returns the value of the identifier rounded to places number of decimal places.

The identifier should be of data type real, places should be data type integer.

RANDOM()
Returns a random number between 0 and 1 inclusive.

Using Round and Random:

// returns a whole number between 0 and 6:


Value ← ROUND (RANDOM() * 6, 0)
// returns a whole number between 0 and 100:
Value ← ROUND (RANDOM() * 100, 0)
// returns a whole number between 1 and 100:
Value ← ROUND ((RANDOM() * 99, 0) + 1)
// returns a 2DP temperature between 34 and 45:
______________________________________________
// returns a whole number between 100 and 150:
______________________________________________
1-D ARRAYS:
Arrays are a fundamental concept in programming that allows you to store and manage multiple pieces
of data of the same type under a single variable name. Imagine it as a collection of numbered storage
compartments, each holding a specific value.

key concepts related to arrays:

1. Indexing: Array elements are accessed using their index, which starts from 0 for the first
element, 1 for the second, and so on. So, if you have an array name, name[0] would refer to
the first name. (Or name[1] in pseudocode)

2. Size: Arrays have a fixed size defined when they're created. This size determines how many
elements can be stored in the array.

3. Data Type: All elements in an array must be of the same data type. If you're storing numbers,
they should all be numbers; if you're storing strings, they should all be strings.

4. Declaration and Initialization: Arrays are declared and initialized with values using various
programming languages.

5. Looping Through Arrays: Arrays are often used with loops to iterate through each element
efficiently. This is handy when performing repetitive tasks on the entire array.
DECLARE Names : ARRAY[1:5] OF STRING
Names
[1]

[2]

[3]

[4]

[5]

Write pseudocode to initialise the above array, then add the names Jon, Tormund, Ser Jorah, Tyrion
and Robb in the array.
Program code:
Declaring an array of Boolean values with size 10. The array is initialised. The user is prompted to input
ten numbers and if the number input is less than 100, the corresponding array value is changed to True
and the content of the arrays are displayed.
LINEAR SEARCH:
• Also known as Serial or Sequential Search.
• A Linear Search assumes the data to be in consecutive locations.
• It does not require the data to be in any particular order.
• To find the position of a value, each value in turn – starting with the first MUST BE
COMPARED with the search value.
• When the value is found, its position in the list is noted.
• In case the whole file/list is searched and the item is not found, this is reported as:
o IsFound  FALSE

It is a method for finding a particular value in an array (in this case) that checks each element in
sequence until the desired element is found or the list is exhausted for which the list need not be
ordered. For an array with n items, the best case is when the value is equal to the first element of the
list, in which case only one comparison is needed. The worst case is when the value is not in the list
(or occurs only once at the end of the list), in which case n comparisons are needed.

Linear Search with 1D Arrays:

Linear search is a simple searching algorithm that checks each element in a collection until a match is
found or the entire collection is traversed. Here is a simplified pseudocode for a Linear Search of a 1D
array of 50 integers to locate the number 493.

DECLARE numArray : ARRAY[1:50] OF INTEGER

PROCEDURE LinearSearch()

DECLARE index : INTEGER //for looping

DECLARE isFound : BOOLEAN //flag variable

isFound ← FALSE

FOR index ← 1 TO 50

IF numArray[index] = 493

THEN

isFound ← TRUE

OUTPUT “Number Found!”

ENDIF

NEXT index

IF isFound = FALSE

THEN

OUTPUT “Number Not Found!”

ENDIF

ENDPROCEDURE
Question 1:
Write program code to:
• Declare an array tempArray of size 20.
• Initialise the array.
• Add random values in the array ranging from 1-150.
• Display contents of the array.
• Search for a number in the array and display appropriate
outputs.
Question 2:
Write program code to:
• Declare 2 arrays, markArr and gradeArr which records marks
and grades for 10 students based on the following criteria:
o >= 70 Distinction
o >=60 and < 70 Credit
o >=50 and <60 Pass
o <50 Fail
• Intialise both arrays.
• Prompt the user to input 10 marks.
• AFTER FILLING markArr, fill the corresponding gradeArr.
• Determine the highest mark in markArr.
• Determine how many students got ‘’Fail’’ in gradeArr.

~END OF LABSHEET~

You might also like