0% found this document useful (0 votes)
15 views

chapter_8_-_part2

Uploaded by

mayare800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

chapter_8_-_part2

Uploaded by

mayare800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 8 : Programming concepts –Data

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;

• 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.

• The first index can be zero or one

• Arrays are used to store multiple data items , all the data items use the same identifier and each

data item can be accessed separately by the use of an index.


Chapter 8 : arrays
Arrays can be one-dimensional or two-dimensional arrays
One-dimensional arrays
A one-dimensional array can be referred to as a list / one column of items ,with the same identifier and
the same data type , each element in an array is identified using the array’s index.
Here is an example of a list
with 10 elements in it where the first element has an index of zero.

When a one-dimensional array is declared in pseudocode:


» the name of the array
» the first index value
» the last index value
» and the data type
Syntax : DECLARE array name : ARRAY[first index value:last index value] OF datatype
Example : DECLARE MyList : ARRAY[0:9] OF INTEGER
Chapter 8 : arrays-one dimensional array
DECLARE MyList : ARRAY[0:9] OF INTEGER
OUTPUT "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72"
FOR Counter ← 0 TO 9
OUTPUT "Enter next value "
INPUT MyList[Counter]
NEXT Counter

Identifier is : MyList
First item : MyList[0]= 27
Last Item : MyList[9] = 27

The variable Counter is used as Index to the array


Chapter 8 : arrays-two dimensional array
Two-dimensional arrays
• A two-dimensional array can be referred to as a table,
with rows and columns, with the same data type,
and two indices to represent the position of the item .
• Here is an example of a table with 10 rows and 3
columns, which contains 30
elements.
• The first element is located at position 0,0.

When a two-dimensional array is declared in


pseudocode:
» the first index value for rows
» the last index value for rows
» the first index value for columns
» the last index value for columns
» and the data type
are included.
For example:
DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER

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

• Counting : using arrays


DECLARE Dessert : ARRAY[1:100] OF STRING
DECLARE ChoiceCount : INTEGER
ChoiceCount ← 0
FOR Counter ← 1 TO 100
IF "ice cream" = Dessert[Counter]
THEN
ChoiceCount ← ChoiceCount + 1
NEXT Counter
OUTPUT ChoiceCount, " chose ice cream as their favourite dessert."
Chapter 8 : arrays
• Maximum, minimum and average using arrays
DECLARE StudentMark : ARRAY[1:60] OF REAL
DECLARE MaximumMark : REAL
DECLARE MinimumMark : REAL
MaximumMark ← StudentMark[1]
MinimumMark ← StudentMark[1]
FOR Counter ← 2 TO 60
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT Counter
Write a pseudocode to do the following :
• Input 25 positive whole numbers that are less than or equal 100 .
• Count and output how many that are greater than 50
• Output the sum , average , highest number, and lowest number
Chapter 8 : arrays- Linear search
A search is used to check if a value is stored in a list, performed by systematically working through
the items in the list.
linear search: which inspects each item in a list in turn to see if the item matches the value
searched for.
Example :
Setting a variable,
Found, as a flag,
OUTPUT "Please enter name to find " using
INPUT Name TRUE and FALSE to
Found ← FALSE indicate if the name
has been found or
Counter ← 1 not
Checking if the
REPEAT name
IF Name = StudentName[Counter] THEN input matches a
Found ← TRUE name
Setting the flag to
in the list
ELSE TRUE when a match
Counter ← Counter + 1 is found
ENDIF
Stopping the search
UNTIL Found OR Counter > ClassSize when a match is
IF Found THEN found or the whole
OUTPUT Name, " found at position ", list has been
Counter, " in the list." searched

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

DECLARE LineOfText : STRING


OPENFILE FileA.txt FOR READ
OPENFILE FileB.txt FOR WRITE
READFILE FileA.txt, LineOfText
WRITEFILE FileB.txt, LineOfText
CLOSEFILE FileA.txt
CLOSEFILE FileB.txt
File handling
pseudocode
DECLARE TextLine : STRING // variables are declared as normal
DECLARE MyFile : STRING // a variable to save the filename
MyFile ← "MyText.txt"
// writing the line of text to the file
OPEN MyFile FOR WRITE // opens file for writing
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE, TextLine // writes a line of text to the file
CLOSEFILE(MyFile) // closes the file

// reading the line of text from the file


OUTPUT "The file contains this line of text:"
OPEN MyFile FOR READ // opens file for reading
READFILE, TextLine // reads a line of text from the file
OUTPUT TextLine
CLOSEFILE(MyFile) // closes the file

You might also like