• Best of luck to the class of 2024 for their HSC exams. You got this!
    Let us know your thoughts on the HSC exams here
  • YOU can help the next generation of students in the community!
    Share your trial papers and notes on our Notes & Resources page
MedVision ad

Algorithms... (1 Viewer)

sig

Member
Joined
Feb 25, 2004
Messages
111
Gender
Male
HSC
2004
I've had a search around the forum however I am still a bit confused...

Can someone link me or tell me where I can find DEFINITE rules in writing algorithms in psuedocode for...


Arrays [Declaring, Accessing, Writing...the whole lot]

Arrays of Records [The Whole Lot]


Thanks

btw I had a look at the stickies...but are there any more sources?
 

hornetfig

Member
Joined
Jun 27, 2004
Messages
65
Location
Sydney. oddly
Gender
Male
HSC
2003
There is no formal rule, so something along the lines of

Code:
myarray is an array of integers indexed from 0 to 120

myrecord is a record of
  a: string
  b: integer
  c: real
END myrecord

myarrayofrecords is an array of myrecords indexed from 0 to 10
etc
 

Freedom_Dragon

The 36th Dragon
Joined
Oct 11, 2003
Messages
154
Location
Behind a door that will never open.
Gender
Undisclosed
HSC
N/A
Best refernece to algorithms is the "Method Of Algorithm Description" Second Edition
http://www.boredofstudies.org/other/1995_SDD_N_Methods_of_Algorithm_Description_BOS.pdf
Its got all the goods u need to know.

Know ur Basic structures ==> sequence, selection, repetition.
Jus made a couple of notes & summaries on these structures hope they help.

Sequence​

Sequence
process 1
Process n

Eg1:
BEGIN
add coffee
add sugar
add hot water(H20)
add milk
stir well
END

Binary Selection(2 possible paths/process)​

Here there is only 1 process (add milk), the other possible path/process is do nothing(not add milk) which is quiet obvious so it is lefted out). NOTE that it does not use "ELSE".

IF condition THEN
do process
ENDIF

Eg1:
IF you want milk THEN
add milk
ENDIF

Here there are 2 process. Process 1 "add milk", the other possible path/process is "add cream".
The use of "ELSE" allows 1 of the 2 accepable paths/process to be done.

IF condition THEN
do process 1
ELSE
do process 2
ENDIF

Eg2:
IF you want milk THEN
add milk
ELSE
add cream
ENDELSEIF

Combining both Sequence & Selection​

Eg3:
BEGIN
add coffee
add sugar
add hot water(H20)
IF you want milk THEN
add milk
ELSE
add cream
ENDELSEIF
END

Multi-way Selection(Casewhere)​

Multi-way slection allows for any number of possible choices & processes.
The process taken is determined by the selection of the choice.

CASEWHERE expression
Choice 1: process 1
Choice 2: process 2

Choice n: process n
OTHERWISE: default process
ENDCASE

Eg1:
CASEWHERE temperature
<16 : run two heating units
Between 16 to 20: run one heating units
Between 21 to 28: run one cooling units
OTHERWISE: run two cooling units
ENDCASEWHERE

Repetition(Loops)​

Repetition allows a portion of an algorithm to be done any number of times
depending on the condition being met. In order to stop a loop, a termination
condition is used. This termination condition can be at the beginning or the
end of the loop(pre-test or post-test).

Pre-Test(Guarded Loop)​

The word "Pre" implies b4. Think in terms of the condition. The condition is b4
the process(es).The body of the loop is executed repeatedly after, WHILE the temination condition holds true(Guarded Loop).
Here the condition has to be met b4 it gets into the loop.
Pre-Test repetitions uses WHILE and ENDWHILE.

WHILE condition is true
do process(es)
ENDWHILE

Eg1:
WHILE there are no barriers in the way //Condition
proceed to next room //Process
ENDWHILE

Post-Test(Unguarded Loop)​

The word "Post" implies after. Think in terms of the condition. The condition is after
the process(es).The body of the loop is executed repeatedly b4 the temination condition,
UNTIL the temination condition holds true(Unguarded Loop). Here the process(es) are done b4
the condition is met. Post-Test repetitions uses REPEAT and UNTIL.

REPEAT do process
UNTIL condition is true

Eg2:
REPEAT kill all monsters //Process
UNTIL there are no monster remaining //Condition

Subprograms​

Complete part programs that are used from within the main program section. Subprograms are
indentified and indicated with an Underline.

BEGIN MAINPROGRAM
process 1
process 2
process 3

process n
END MAINPROGRAM

BEGIN SUBPROGRAM process 3
do this
do that
END SUBPROGRAM process 3

Not a very good example. Only used to illustrate Subprograms.
Eg1:
BEGIN MAINPROGRAM
Get NumOfPlayers
Stop=False
PNum=1
FOR Index=1 To NumOfPlayers
PTotal(Index)=5
ENDFOR
Index=1
WHILE Stop=False
IF Index=11 THEN
Index=1
ENDIF
ENDWHILE
Spin=RND(6) {RND(6) returns a number 1-6}
CASEWHERE Spin
1:WinOne(Index,PTotal)
2:LoseOne(Index,PTotal)
3:WinFive(Index,PTotal)
4:LoseFive(Index,PTotal)
5:AllWin(Index,PTotal,NumOfPlayers)
6:AllLose(Index,PTotal,NumOfPlayers)
ENDCASEWHERE
FOR Count=1 To NumOfPlayers
IF Ptotal(Count)>=50 THEN
Stop=True
ENDIF
ENDFOR

Index=Index+1
Print "The Winner is player number" Findwinner
END MAINPROGRAM

BEGIN SUBPROGRAM LoseOne(Pos,PArray)
Parray(Pos)=PArray(Pos)-1
Return PArray
END SUBPROGRAM LoseOne

Arrays​

An array is a structured data type that lets the programmer store multiple values(common data
type) in such a way that processing & storing data is greatly simplified.

Consider this array called Array_Num containing 6 elements(Index):

[7][4][2][8][5][9]​

Finding Max_Val​

BEGIN
Index=2
LastIndex=6
Max_Val=Array_Num[1] // Assuming the first element is the highest
WHILE Index<=LastIndex
IF Array_Num[Index]>Max_Val THEN
Max_Val=Array_Num[Index]
ENDIF
Index=Index+1
ENDWHILE
Print "The highest number is" Max_Val
END

Ur Output should be 9

Finding Min_Val​

BEGIN
Index=2
LastIndex=6
Min_Val=Array_Num[1] // Assuming the first element is the smallest
WHILE Index<=Lastindex
IF Array_Num[Index] < Min_Val THEN
Min_Val=Array_Num[Index]
ENDIF
Index=Index+1
ENDWHILE
Print "The smallest number is" Min_Val
END

Ur Output should be 2

Linear Search​

Used for unsorted arrays

BEGIN
Index=1
LastIndex=6
Found=False (Not found)
Get Target
WHILE Index<=LastIndex AND Found=False
IF Array_Num[Index]=Target THEN
Found=True
FoundPosition=Index
ENDIF
Index=Index+1
ENDWHILE
IF Found=True THEN
Print "Target found at" FoundPosition
ELSE
Print "Target not found"
ENDELSEIF
END

Binary Search​

Consider this array called Array_Item containing 6 elements(Index):

[Mouse][Keyboard][Printer][Scanner][Speaker][Monitor]​

Which search technique would u use, Linear 0r Binary search? In this case a Binary search would be much more efficient. Used efficiently for ordered arrays.

BEGIN
Lower=1
Upper=6
Found=False (Not found)
Get Target
REPEAT Middle=Int(Lower+Upper)/2
IF Array_Item[Middle]=Target THEN
Found=True
FoundPosition=Middle
ELSE
IF Target < Array_Item[Middle] THEN
Upper=Middle-1
ELSE
Lower=Middle+1
ENDELSEIF
ENDELSEIF
UNTIL Found=True OR Lower=Upper
IF Found=True THEN
PRINT "Target found at" FoundPosition
ELSE
PRINT "Target not found"
ENDELSEIF
END


Records Of Arrays​

Records are a metod of combining 2 or more simple data types that are related.
If we need to store many different items of different data types which is related,
we can use an array of records.

Consider:-

Student Record=Record
Firstname: String
Surname: String
Gender: String
ID: Integer
END Student Record

Student array(1...1000) of student record

This algorithm finds a particular student and obtains all details of that student.
//Finds student ID from Student file (Array, Records) respectively
Function Findstudent(Student ID, Student file)
Index=1
Found=False(Not Found)
WHILE Index<=1000 AND Found=False
IF Student ID=Studentfile(Index).ID THEN
Found=True
RETURN Studentfile(Index).ID,Studentfile(Index).Firstname,Studentfile(Index).Surname,
Studentfile(Index).Gender
ELSE
Index=Index+1
ENDELSEIF
ENDWHILE
END Findstudent


Swap Algorithm​

BEGIN
Get Number 1
Get Number 2
Num(1)=Number 1
Num(2)=Number 2
Temp=Num(1)
Num(1)=Num(2)
Num(2)=Temp
END

Feel free to correct any errors ^_^
 
Last edited:

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
hmm, question


would we have to know the string extraction , joining and deletion algorithms.. ??
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
Also, i was wondering, are we allowed to do something like:
Set high to upper boundary of MyArray()
Set low to lower boundary of MyArray()
?

If so, is it preffered if we use boundaries when they are given to us e.g "MyArray() is an array indexed from 1 to 45", OR is it fine to just determine them everytime we want to use them like above, hence making the function more easier to reuse OR should we get the upper/lower boundaries as input from the user/as parameters?

Another thing, should we assume that the arrays are indexed starting from 0, or should i determine the lower boundary for each array as above (if we're allowed to)?
 

Freedom_Dragon

The 36th Dragon
Joined
Oct 11, 2003
Messages
154
Location
Behind a door that will never open.
Gender
Undisclosed
HSC
N/A
J0n said:
Also, i was wondering, are we allowed to do something like:
Set high to upper boundary of MyArray()
Set low to lower boundary of MyArray()
?
yer u could do that...nothing wrong in that me think

Set Lower to " "
Set Upper to " "

If so, is it preffered if we use boundaries when they are given to us e.g "MyArray() is an array indexed from 1 to 45", OR is it fine to just determine them everytime we want to use them like above, hence making the function more easier to reuse OR should we get the upper/lower boundaries as input from the user/as parameters?
I guess that depends on the situation...if ur given the boundaries e.g "MyArray() is an array indexed from 1 to 45" then Lower=1, Upper=45.

If not then jus determine them Or like u said..get them as input from user as parameters

eg:
Get Lower
Get Upper
Found=False
Get Target


Another thing, should we assume that the arrays are indexed starting from 0, or should i determine the lower boundary for each array as above (if we're allowed to)?
[Mouse][Keyboard][Printer][Scanner][Speaker][Monitor]
.........1..............2.............3............4.............5.............6............

I always use 1 as the first index...cos its less confusing. My teachers index always start with 0. Correct me if im wrong but i dont think it really matters if its indexed from 0 to 5 OR 1 to 6. As long as the algorithm works and the concept is there.

Hope that helps.
 
Last edited:

Freedom_Dragon

The 36th Dragon
Joined
Oct 11, 2003
Messages
154
Location
Behind a door that will never open.
Gender
Undisclosed
HSC
N/A
String Manipulation

Strings are data types used to hold characters or texts. The data structure commonly used to store strings are arrays.

Types of string manipulation:
Concatenation
Extraction
Deletion
Insertion

Concatenation

Concatenation simply means to join 2 strings together to form a new string.

Eg: [C][A][T]+[D][O][G]=[C][A][T][D][O][G]

Function Concatenate (String1, String2)
length1=Strlength(String1) // length of String1
length2=Strlength(String2) // length of String2
FOR Index=1 TO length1
Newstring(Index)=String1(Index)
ENDFOR
FOR Index2=1 TO length2
Newstring(length1+Index2)=String2
ENDFOR
Concatenate=Newstring
END Concatenate

Extraction

Extraction involves making a copy of a section of the original string.

Eg: [C][O][M][P][T][E][R]====>[P][T]

Function Extract (Xstring, Start, Finish)
Index=1
FOR Position=Start TO Finish //Start & finish of the extract
Newstring(Index)=Xstring(Position)
Index=Index+1 //Increments index of Newstring
ENDFOR
Newstring=Extract
END Extract

Deletion

Deletion involves removing a section of the original string.

Eg: [C][O][M][P][T][E][R]-[M][P][T]=[C][O][E][R]

Function Deletion (Dstring,Start,Finish)
//Extracts everything b4 the start of delect string
temp1=Extract (Dstring, 1, Start-1)
//Extracts everything after the finish of delect string
temp2=Extract (Dstring, Finish+1, Strlength(Dstring))
Newstring=Concatenate (temp1, temp2)
END Delete

Insertion

Insertion involves inserting 1 string into another string.

Eg: Insert [1][2][3] in between F & G of [E][F][G][H]=>[E][F][1][2][3][G][H]


Function Insertion (String1, String2, Start)
// the length of string1
length1=Strlength(String1)
// breaking string1 into 2
// first portion of string1
temp1=Extract (String1, 1, Start-1)
// second portion of string1
temp2=Extract (String1, Start, length1)
// join String2 with the first portion of string1
Newstring= Concatenate (temp1, String2)
// join second portion of string1 with the 2 connected string
Newstring= Concatenate (Newstring, temp2)
END Insertion

Please feel free to correct any errors ^^
 
Last edited:

sig

Member
Joined
Feb 25, 2004
Messages
111
Gender
Male
HSC
2004
Ok another quick question from a clueless candidate in SDD...

Can sequential files have a record structure?
ie. I'm referring to the HSC 2003 Exam Question 21(d)(ii)
PDF File Here

If someone can be bothered to post a suitable algortihm?

Thanks
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
Sequential files are just a continuous stream of data. The file does not contain information about the record structure but the code accessing the file may well read and write fields.

In this question it is reasonable to assume that the file can be read one data item at a time. That is, some inbuilt mechanism exists to determine the end of each data item.

I don't think records are really necessary to answer this question. A suitable algorithm could be...

Code:
BEGIN ScanFile
  Open Input File
  Open Output File called Results
  WHILE NOT end of Input file
     Read Version, Lines from Input file
     IF Lines>11000 THEN
         Store Version in Results file
     ENDIF
  ENDWHILE
  Close Files
END ScanFile
HTH
Sam
 

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
QUESTION

Deletion involves removing a section of the original string.

Eg: [C][O][M][P][T][E][R]-[M][P][T]=[C][O][E][R]

Function Deletion (Dstring,Start,Finish)
//Extracts everything b4 the start of delect string
temp1=Extract (Dstring, 1, Start-1)
//Extracts everything after the finish of delect string
temp2=Extract (Dstring, Finish+1, Strlength(Dstring))
Newstring=Concatenate (temp1, temp2)
END Delete

What does Extract(dstring,1,Start-1) mean?

okay i get what ur saying we are calling the extract algorithm with the deletestring (part of the string to be deleted) but what does 1 and STart - 1 mean? Does 1 mean the first character in the string... err and STart - 1????
 

Freedom_Dragon

The 36th Dragon
Joined
Oct 11, 2003
Messages
154
Location
Behind a door that will never open.
Gender
Undisclosed
HSC
N/A
When u go to delete a section of the original string u'll have a "Start" point and "Finish" point.

Eg: In [C][O][M][P][T][E][R]

say u want to delete [M][P][T]...Ur Start is [M] & ur Finish is [T]

"1" is [C] the first element/character of the string
"Start-1" is [O] the element/character b4 [M], "-1" going back one element/character.

Extract (Dstring, 1, Start-1) would extract [C][O]

"Finish+1" is [E] the element/character after [T]
"Strlength(Dstring)" is [R] the 8th element/character of Dstring

Extract (Dstring, Finish+1, Strlength(Dstring)) would extract [E][R]

Finally concatenate [C][O] & [E][R] would give u a Newstring[C][O][E][R] leaving out the deleted string[M][P][T].

Hope That Helped. ^_*
 
Last edited:

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
but to just seek around like that

you would have to use a binary search of some sort , right?
 

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Or do you just say something along the lines of:

Say i want to find a certain record and print it, do i just write:

OPEN file
Find record
print record

and it automatically searches for the record in the random access file.?
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
Random access files allow you to jump to a particular record using its index. In a sense you view the file as an array of records. You don't need to iterate through each record if you know the required record's index. This applies to both reading and writing.

In general something like...

Detemine Index of Required Record
Read Record with this index into a variable with the same record type
Access/process the appropriate fields within this variable

If you're searching for a particular value within a field in a random file then you must revert to searching. (Linear search if records are not sorted on the search field OR binary search if records are sorted on the search field).

HTH
Sam
 

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
Freedom_Dragon said:
When u go to delete a section of the original string u'll have a "Start" point and "Finish" point.

Eg: In [C][O][M][P][T][E][R]

say u want to delete [M][P][T]...Ur Start is [M] & ur Finish is [T]

"1" is [C] the first element/character of the string
"Start-1" is [O] the element/character b4 [M], "-1" going back one element/character.

Extract (Dstring, 1, Start-1) would extract [C][O]

"Finish+1" is [E] the element/character after [T]
"Strlength(Dstring)" is [R] the 8th element/character of Dstring

Extract (Dstring, Finish+1, Strlength(Dstring)) would extract [E][R]

Finally concatenate [C][O] & [E][R] would give u a Newstring[C][O][E][R] leaving out the deleted string[M][P][T].

Hope That Helped. ^_*


aye yes it did :D

but hang on say with your example COMPUTER
now if i wanted to extract COMP
Start - 1 is out of the array.....
does this mean Start -1 is 0 ?? Because if thats the case the Deletion does not work

or does this mean the condition when the extractString Fails and so nothign is extracted... ?? (which i think works)

but is this right? or is something left out

thanks :D

P.S ooh and one more thing is it fair to say if a File has a record structure within it , then the file acts like an array of records? .. i was using your Array of records algorithm as an example
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top