chapter_8_-_part2
chapter_8_-_part2
Structures
Variables
• Variable in a computer program , Constants
is a named data store than contains a value,that
and Arrays
may change during
the execution of a program. Putting data into variable is done using an Assignment Statement
Declaring Variable :
Syntax : DECLARE variable name : Data type
Example : DECLARE X : STRING
DECLARE Y : INTEGER
DECLARE Z : INTEGER
• Constant in a computer program is a named data store than contains a value that does not change
during the execution of a program.
Declaring the constants :
Syntax : CONSTANT constant name = value
Example : CONSTANT Pi= 3.14
• An array is a data structure containing several elements(List ) of the same data type; these elements
can be accessed using the same identifier name. The position of each element in an array is identified
using the array’s index.
Chapter 8 : arrays
An array is a data structure containing several elements of the same data type;
• The position of each element in an array is identified using the array’s index.
• Arrays are used to store multiple data items , all the data items use the same identifier and each
Identifier is : MyList
First item : MyList[0]= 27
Last Item : MyList[9] = 27
Some 2-dim arrays are Row Major means the index of the row
comes first then the index of the column Ex. Java,
Chapter 8 : arrays-two dimensional array
The declared array can then be populated using a loop, just like for onedimensional
arrays – however this time there need to be two nested loops, one
for each index:
DECLARE MyTable : ARRAY[0:9, ] OF INTEGER
OUTPUT "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34"
OUTPUT "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76"
OUTPUT "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21"
FOR ColumnCounter ← 0 TO 2
FOR RowCounter ← 0 TO 9
OUTPUT "Enter next value "
INPUT MyTable[RowCounter, ColumnCounter]
NEXT RowCounter
NEXT ColumnCounter
Chapter 8 : arrays
• Totalling and average using arrays
DECLARE StudentMark : ARRAY[1:20] OF REAL
DECLARE Total : REAL
Total ← 0
FOR Counter ← 1 TO 20
Total ← Total + StudentMark[Counter]
NEXT Counter
Avg Total / 20
ELSE
OUTPUT Name, " not found."
Chapter 8 : arrays- Bubble
Lists can be more useful if the items are sorted in a meaningful order in alphabetical order, ascending or descending order. The
method of sorting is called a bubble sort.
Sort -
• Each element is compared with the next element and swapped if the elements are in the wrong order, starting from the
first element and finishing with next-to-last element.
• Once it reaches the end of the list, we can be sure that the last element is now in the correct place.
• However, other items in the list may still be out of order.
• Each element in the list is compared again apart from the last one because we know the final element is in the correct
place.
• This continues to repeat until there is only one element left to check or no swaps are made.
For example, the bubble sort algorithm can be used to sort a list of ten temperatures stored in the array, Temperature[], into
ascending order. It could be written in pseudocode as:
First ← 1
Last ← 10
REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Temperature[Index] > temperature[Index + 1] THEN
Temp ← Temperature[Index]
Temperature[Index] ← Temperature[Index + 1]
Temperature[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1
Chapter 8 : arrays- Bubble
Sort - 0478/21/O/N/17 –Q5
• Each element is compared with the next
element and swapped if the elements
are in the wrong order, starting from the
first element and finishing with next-to-
last element.
• Once it reaches the end of the list, we
can be sure that the last element is now
in the correct place.
• However, other items in the list may still
be out of order.
• Each element in the list is compared
again apart from the last one because
we know the final
element is in the correct place.
• This continues to repeat until there is
only one element left to check or no
swaps are made.
Chapter 8 : arrays
0478/21/O/N/17 –Q4,5
0478/21/M/J/17 –Q5
0478/21/M/J/18 –Q3
0478/21/O/N/19-Q4
0478/23/O/N/19 –Q5
0478/21/M/J/21-Q4
0478_s21_qp_23 – Q4
0478_w21_qp_21-Q2
0478_w21_qp_23-Q2
0478_s22_qp_23 – Q4
0478_s22_qp_22-Q3
Creating a maintainable program
Once a program is written, it may need to be maintained or updated by another
programmer at a later date. A maintainable program should:
• always use meaningful identifier names for:
– variables
– constants
– arrays
– procedures
– functions
• be divided into modules for each task using:
– procedures
– functions
• be fully commented using your programming language’s commenting feature.
File handling
The storage and access of data in a file is called File handling
Read from a file :
To read a value from a file you should :
• OPEN the file using it’s filename and the mode .
• READ the value then save it (using assignment statement , print it , or use it in the conditional statement )
• CLOSE the file
Writing to a file
To write a value to a file you should :
• OPEN the file using it’s file name , and the mode
• Write the value/data to the file .
• CLOSE the file
File handling
OPEN :
OPENFILE <File identifier> FOR <File mode>
The file identifier will be the name of the file with data type string. The following file modes are used:
• READ for data to be read from the file
• WRITE for data to be written to the file. A new file will be created and any existing data in the file will be lost.
A file should be opened in only one mode at a time.
READ :
Data is read from the file (after the file has been opened in READ mode) using the READFILE
command as follows:
READFILE <File Identifier>, <Variable>
When the command is executed, the data item is read and assigned to the variable.
WRITE :
Data is written into the file after the file has been opened using the WRITEFILE command as follows:
WRITEFILE <File identifier>, <Variable>
When the command is executed, the data is written into the file.
CLOSE :
Files should be closed when they are no longer
needed using the CLOSEFILE command as follows:
CLOSEFILE <File identifier>
File handling
This example uses the operations together, to copy a line of text from FileA.txt to
FileB.txt