Algorithm Question - Everyone Welcome (1 Viewer)

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
((e) Write an algorithm to calculate the cost of the tickets that the train traveller
wants to buy.
Part of the algorithm is to conduct a search of an array of records containing the
single full fare and return full fare for each destination. The search needs to
extract fare information for the destination station selected by the train traveller.
The structure of the array of records is:
A portion of the array of records is shown below.
Use the variable names:
UserDest to stand for the destination station selected by the
train traveller;
NumSingle to stand for the number of one-way tickets the
traveller wishes to purchase;
NumReturn to stand for the number of return tickets the traveller
wishes to purchase; and
TotalFare to stand for the total cost of all tickets purchased.
For this algorithm you may assume that:
there are 100 records in the array of records;
no concession, child or student fares are available;
the variables NumSingle and NumReturn could be zero. (If both variables were
zero then TotalFare would be zero.)
Scone
Singleton
Springwood
$23.80
$15.40
$ 5.20
$42.50
$28.40
$ 9.60
destination (index).fullreturn destination (index).fullsingle destination (index).station


This is copied straight from the pdf file for the 2002 hsc for sdd.....
Would anyone be kind enough to answer this question and/or analyse my answer as to whether its worth 5 marks or not?

My Answer (This was a pretty quick job):

Code:
BEGIN CalcTickets
 index=0
 Itemfound = false
 WHILE UserDest <> destionation(index).station
            index = index +1
 UNTIL UserDest = destination(index.station OR index = 100
            IF UserDest = destionation(index).station THEN
            [NumSingle*destionation(index).fullsingle]+[NumReturn*destination(index).fullreturn]=TotalFare
            ENDIF
 ENDWHILE
END CalcTickets
[Edited to allow for indentation etc. - Laz]
 
Last edited:

ezzy85

hmm...yeah.....
Joined
Nov 4, 2002
Messages
556
Gender
Undisclosed
HSC
N/A
i havent read the q or algorithm, but according to my teacher you wont get full marks because you need indentation.
 

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
i know that, i just couldnt do it in this post box, even when i put spaces for indents, it just justified it automatically..
 

Beaky

You can read minds?
Joined
Apr 5, 2003
Messages
1,407
Location
Northen Beaches Pos
Gender
Male
HSC
2003
Umm.. minds quite similar to yours JRasiner...

Cept you should have parameters passing from the main program... that would prob u get u extra marks

Therefore I'll attach an exemplar sample instead :p
 

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
i dont beleive u need to put parameters, because it is the main program, the data your meant to extract, ur extracting from an array, not another program....... (tell meeh if im wrong)
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
Originally posted by JRasnier
.
.
.
My Answer (This was a pretty quick job):

Code:
BEGIN CalcTickets
 index=0
 Itemfound = false
 WHILE UserDest <> destionation(index).station
            index = index +1
 UNTIL UserDest = destination(index.station OR index = 100
            IF UserDest = destionation(index).station THEN
            [NumSingle*destionation(index).fullsingle]+[NumReturn*destination(index).fullreturn]=TotalFare
            ENDIF
 ENDWHILE
END CalcTickets
[Edited to allow for indentation etc. - Laz] [/B]
don't know if you'd get full marks for the loop

it's WHILE - END WHILE or DO - UNTIL

also [NumSingle*destionation(index).fullsingle]+[NumReturn*destination(index).fullreturn]=TotalFare is the wrong way around........
 

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
I see I see, i agree with u, as i said it was a quick job, i knew that WHILE with a UNTIL was wrong, just didnt know how else to do it at the time

but i see now u would do a


WHILE UserDest<> destination(index).station
index = index + 1
IF UserDest = destination(index_.station OR index = 100 THEN
END
ELSEIF
UserDest = destionation(index).station THEN
TotalFare = [NumSingle*destionation(index).fullsingle]+[NumReturn*destination(index).fullreturn]=TotalFare
ENDIF
ENDIF
END WHILE
END CalcTickets
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
Code:
BEGIN calcTickets
   index = 1
   found = False
   WHILE NOT found AND index <= 100
        IF UserDest = destination(index).station THEN
            found = True
        END IF
        index = index + 1
   END WHILE

   IF found THEN
        TotalFare = NumSingle * Destination(index).fullsingle + NumReturn * Destination(index).fullreturn
        Output "The total fare is: " + TotalFare
   ELSE
        Output "Station not found"
   END IF
END calcTickets
 

Beaky

You can read minds?
Joined
Apr 5, 2003
Messages
1,407
Location
Northen Beaches Pos
Gender
Male
HSC
2003
Originally posted by JRasnier
i dont beleive u need to put parameters, because it is the main program, the data your meant to extract, ur extracting from an array, not another program....... (tell meeh if im wrong)
Im assuming this you created this as a subroutine...

Thats how i interprated it...

but its cool if you make it as a program
 

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
i was just basing it on what it says in the question......

sorry if i sounded rude, just gettin a tiny bit stressed, from the prediction where scores better then mine are getting 80's for prediction, seems like i wont be able to get 80's or something, anwyayz yeaha sorriez dude...oh yeh also thanx dude's....
 
Last edited:

TheKey

Member
Joined
Jun 14, 2003
Messages
285
Location
Sydney
Gender
Male
HSC
2003
prolly very stupid question but anyway, if the question in HSC says "write an algorithm.." does that automatically mean you write it as code (like you guys are doing above) or can you draw it in a flowchart form instead?

I know in our trial they marked both as correct and you can get full mark for ether, and if you did both types they only mark the better one.
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
If it says: "Write Pseudocode" then you write pseudocode.
If it says: "Do a flowchart" then you do a flowchart.

If it says: "Write an algorithm" then you can do either. They are both methods of writing algorithms. But you don't need to do 'both'. Just do what they say.
 

-X-

Member
Joined
Aug 10, 2003
Messages
481
Gender
Undisclosed
HSC
N/A
Will this method get full marks you recon? Its not as efficient since it searches the whole array before exiting the loop.

Code:
Begin CalcPrice()

           UserDest = Get destination from user;
           NumSingle = Get no of 1 way tickets;
           NumReturn = Get no of return tickets;
           TotalCost = 0;

           If NumSingle & NumReturn are <> '0' THEN
                   For index = 1 to 100
                            If Destination(index).station = UserDest THEN
                                  TotalCost = NumSingle x (Destination.index).FullSingle) + NumReturn x (Destination(index).FullReturn);
                            End IF
                    next index;
           Else
                    TotalCost = '0';
           End IF
end CalcPrice()
Basically, if the code does the same thing with out being effiecient, do u get marks deducted?

Damn indentation is a killer to type using Space Bar. heheh :D
 

Infinite

New Member
Joined
Oct 2, 2003
Messages
11
It looks fine.

Maybe you want to declare the variables first?

or intalise the algorithms at the start and turn it int oa sub so you can asusme the values are already given to you.

i.e

Begin CalcPrice(UserDest, NumSingle, NumReturn)

But usually a few minor errors wouldnt cost you a mark.

Also use '*' for multiplicaiton not 'x'
 

-X-

Member
Joined
Aug 10, 2003
Messages
481
Gender
Undisclosed
HSC
N/A
Originally posted by Infinite
It looks fine.

Maybe you want to declare the variables first?

or intalise the algorithms at the start and turn it int oa sub so you can asusme the values are already given to you.

i.e

Begin CalcPrice(UserDest, NumSingle, NumReturn)

But usually a few minor errors wouldnt cost you a mark.

Also use '*' for multiplicaiton not 'x'
ok thanks allot then. I used 'x' to make it easier to read, alteast easeir for me. :p
 

felix_js

lost
Joined
Oct 14, 2002
Messages
341
Gender
Undisclosed
HSC
N/A
Just a question, are we allowed to use FOR and NEXT in our algorithms or do we need to REPEAT/WHILE and increment?
FOR loops arent in the Methods of Algorithm Description
 

-X-

Member
Joined
Aug 10, 2003
Messages
481
Gender
Undisclosed
HSC
N/A
Our teacher says that its acceptable to use VB syntax in psedoucode since its very similar to english.
 

Infinite

New Member
Joined
Oct 2, 2003
Messages
11
Ive written mini sentences sometimes. Just do whatever is easier for you.

i.e

for i = 0 to end of array

while i is not x

then

blah blah blah
 

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

Top