Algorithm Answers (1 Viewer)

51684

Member
Joined
Dec 21, 2007
Messages
137
Gender
Male
HSC
2009
So I'm bored so I decided to do the algorithm answers. I got some of these wrong in the actual exam but after looking at them now these are most likely all correct:

22 b)

(i)
position end current maximum Index(1) Index(2) Index(3) Index(4)
1 4 2 1 S F W A
1 4 3 3 S F W A
1 4 4 3 S F W A
1 4 5 3 W F S A
2 4 3 3 W F S A
2 4 4 3 W F S A
2 4 5 3 W S F A
3 4 4 3 W S F A
3 4 5 3 W S F A

(ii) A selection sort is used as the largest number is idenitified in the names array and then swapped with the number at the start of the array. After each swap, the number at the start of the array in increased by one (position = position +1) so the next largest number can be swapped into that position. This is the modelling of a selection sort.

(iii)
Code:
BEGIN SUBPROGRAM swap(names, maximum, position)
temp = names(maximum)
names(maximum) = names(position)
names(position) = temp
END SUBPROGRAM
23 b)

(i) The error is WHILE touchPad = TRUE, it should be FALSE. This is so the timer is updated constantly until a person touches the touchpad which is makes sense. In it's current state, swimmer_time is only gotten when the gun goes off, the program would end as soon as the swimmer left the blocks as TouchPad = FALSE.

(ii) If a person false started, the swimmer_start_time is never gotten because startBlockOccupied would be FALSE and therefore the variable never gets the value of the timer as it is located in the conditional loop. So a false start is detected if swimmer_start_time = -1.000 as it would be greater than 0 if they didn't false start.

(iii) Not sure why this is worth 2 marks but I think you just need to have some sort of output (PRINT) of the swimmer_time between lines 140 and 150. Maybe 1 mark for the correct variable output and 1 mark for the correct placement of the code.

23 c)

(i) Array of Records.

(ii) - First error is that WHILE i < 100 should be i <= 100. Currently, the 100th record would miss out as when i is 100, the loop would end so only the first 99 records would be printed.
- Second error is that i is never incremented and so there would be an endless (infinite) loop as the value of i never changes and only the first record would be printed. Add i = i + 1 between lines 60-70.

(iii) There's obviously numerous ways to do this, here's mine:
Code:
BEGIN maxKingdom

set i to 1
set count(1) to 0
set count(2) to 0
set count(3) to 0

WHILE i <= 100
IF organismDB(i).kingdom = "Animalia" THEN
count(1) = count(1) + 1
ELSEIF organismDB(i).kingdom = "Plantae" THEN
count(2) = count(2) + 1
ELSE
count(3) = count(3) + 1
END IF
i = i + 1
ENDWHILE

IF count(1) > count(2) THEN
max = 1
ELSE
max = 2
END IF

IF count(max) > count(3) THEN
ELSE
max = 3
END IF

SELECT CASE max
CASE 1: Print "Animalia"
CASE 2: Print "Plantae"
CASE 3: Print "Fungi"
END SELECT

Print count(max)

END maxKingdom
 
Last edited:

sidereal

Member
Joined
Feb 15, 2008
Messages
38
Location
Canberra
Gender
Male
HSC
2009
Uni Grad
2013
you didn't increment i in the while loop in maxKingdom ;)

other than that it all looks good, exactly what i had for every question. only difference was i decided for the desk check that "a" should be > "b" rather than < "b" such that the algorithm sorted in ascending order. it's not really clearly defined in the course or the question whether a>b or a<b (there are arguments on both sides) and i explained that decision of mine on the paper, so hopefully that's still worthy of the marks.
 

51684

Member
Joined
Dec 21, 2007
Messages
137
Gender
Male
HSC
2009
you didn't increment i in the while loop in maxKingdom ;)

other than that it all looks good, exactly what i had for every question. only difference was i decided for the desk check that "a" should be > "b" rather than < "b" such that the algorithm sorted in ascending order. it's not really clearly defined in the course or the question whether a>b or a<b (there are arguments on both sides) and i explained that decision of mine on the paper, so hopefully that's still worthy of the marks.
Hopefully I didn't forget to increment that in the actual question. :mad1:

I'm pretty sure B>A because of the ASCII values - that's how the computer knows which is bigger. So in terms of a maximum/highest value it goes ZYX...CBA. But if you justified A>B that it should be all good.
 

cata

New Member
Joined
Jan 29, 2009
Messages
17
Gender
Female
HSC
2009
I had no idea how to write all those codings so i just tried and bulled my way through ;)
Just drew random flowcharts, hopefully they acknowledge it
 

Fluorescent

Banned
Joined
Apr 5, 2009
Messages
266
Location
Sydney
Gender
Female
HSC
2009
So I'm bored so I decided to do the algorithm answers. I got some of these wrong in the actual exam but after looking at them now these are most likely all correct:

22 b)

(i)
position end current maximum Index(1) Index(2) Index(3) Index(4)
1 4 2 1 S F W A
1 4 3 3 S F W A
1 4 4 3 S F W A
1 4 5 3 W F S A
2 4 3 3 W F S A
2 4 4 3 W F S A
2 4 5 3 W S F A
3 4 4 3 W S F A
3 4 5 3 W S F A

(ii) A selection sort is used as the largest number is idenitified in the names array and then swapped with the number at the start of the array. After each swap, the number at the start of the array in increased by one (position = position +1) so the next largest number can be swapped into that position. This is the modelling of a selection sort.

(iii)
Code:
BEGIN SUBPROGRAM swap(names, maximum, position)
temp = names(maximum)
names(maximum) = names(position)
names(position) = temp
END SUBPROGRAM
23 b)

(i) The error is WHILE touchPad = TRUE, it should be FALSE. This is so the timer is updated constantly until a person touches the touchpad which is makes sense. In it's current state, swimmer_time is only gotten when the gun goes off, the program would end as soon as the swimmer left the blocks as TouchPad = FALSE.

(ii) If a person false started, the swimmer_start_time is never gotten because startBlockOccupied would be FALSE and therefore the variable never gets the value of the timer as it is located in the conditional loop. So a false start is detected if swimmer_start_time = -1.000 as it would be greater than 0 if they didn't false start.

(iii) Not sure why this is worth 2 marks but I think you just need to have some sort of output (PRINT) of the swimmer_time between lines 140 and 150. Maybe 1 mark for the correct variable output and 1 mark for the correct placement of the code.

23 c)

(i) Array of Records.

(ii) - First error is that WHILE i < 100 should be i <= 100. Currently, the 100th record would miss out as when i is 100, the loop would end so only the first 99 records would be printed.
- Second error is that i is never incremented and so there would be an endless (infinite) loop as the value of i never changes and only the first record would be printed. Add i = i + 1 between lines 60-70.

(iii) There's obviously numerous ways to do this, here's mine:
Code:
BEGIN maxKingdom

set i to 1
set count(1) to 0
set count(2) to 0
set count(3) to 0

WHILE i <= 100
IF organismDB(i).kingdom = "Animalia" THEN
count(1) = count(1) + 1
ELSEIF organismDB(i).kingdom = "Plantae" THEN
count(2) = count(2) + 1
ELSE
count(3) = count(3) + 1
END IF
i = i + 1
ENDWHILE

IF count(1) > count(2) THEN
max = 1
ELSE
max = 2
END IF

IF count(max) > count(3) THEN
ELSE
max = 3
END IF

SELECT CASE max
CASE 1: Print "Animalia"
CASE 2: Print "Plantae"
CASE 3: Print "Fungi"
END SELECT

Print count(max)

END maxKingdom

WTF..... I DID SOMETHING SOOOOO DIFFERENT FOR THE LAST ONE LOL


Like .... 8 lines of code maybe
 

hrtyoung

New Member
Joined
Mar 29, 2009
Messages
23
Gender
Male
HSC
2009
lol, yours was a lot neater than mine... I spent about 15 more lines comparing the different cases to print.

either way it still works fine.
 

Nivek5

Member
Joined
Dec 2, 2008
Messages
48
Gender
Male
HSC
2009
I'm pretty sure you had to print all 3 counters..

Then print the maximum one...

I used a subprogram for finding the maximum .. Adds maintainability or some crap lol :p who cares now. its over.


EDIT: nope my mistake, lol Mine printed all3 along with the maximum >___< fail

Oh and with your Select case
isnt a characteristic that it must have an "Otherwise" statement... if not my teacher taught us wrong
 
Last edited:

Aeriff

Member
Joined
Aug 25, 2008
Messages
332
Location
whore island
Gender
Male
HSC
2009
My answers were exactly the same except my max algorithm took up like 2.5 pages lol. I had to get a new writing booklet just for the last 4 lines of it. I felt so stupid.
 

Fluorescent

Banned
Joined
Apr 5, 2009
Messages
266
Location
Sydney
Gender
Female
HSC
2009
Um for the last algorithm I just added an array called
organismDB(i). Population

I didn't know how else you were supposed to know the pop?

I then did a linear search

fuck me, i failed.
 

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

Top