• 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

Mark my algorithm (1 Viewer)

rad

New Member
Joined
Sep 7, 2003
Messages
9
This was question 22(a)(iii) in the 2001 paper, can someone please tell me if its correct? (i really hate algorithms btw)

Link to question

Code:
BEGIN Bikes
    OPEN trans_file
    OPEN inv_file
    index = 1
    count = 0
    WHILE more transactions in trans_file
        READ trans_file.stocknumber(index)
        found = false
        WHILE found = false and not end of inv_file
            count = count + 1
            IF inv_file.stocknumber(count) = trans_file.stocknumber(index) THEN
                found = true
            END IF
        END WHILE
        IF found = true THEN
            inv_file.quantity(count) = inv_file.quantity(count) + trans_file.quantity(index)
        ELSE
            Print "could not find stock number"
        END IF
        index = index + 1
    END WHILE
    index = 1
    REPEAT
        IF inv_file.quantity(index) < 3 THEN
            Print "Less than 3 items in stock"
        END IF
        index = index + 1
    UNTIL no more items in inv_file
    CLOSE trans_file
    CLOSE inv_file
END Bikes
 
Last edited:

rad

New Member
Joined
Sep 7, 2003
Messages
9
killed my indenting :mad:

edit - fixed!
 
Last edited:

Glide

Member
Joined
Oct 13, 2003
Messages
103
Dont have the Q infront of me, but it looks good to me :)
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
Logically a great answer...

Just a few points that mean it's not perfect.

1. Your algorithm will process the last transaction record, which is the 999 or sentinel or end of file marker. I think you need to do an initial read before entering the outside loop and then check for the 999 record as the terminating condition for the loop. (I think you'd lose a mark for this).

2. You've initialised Count in the wrong place. Count=0 should be just prior to your loop that iterates through the inventory file. (Probably would lose a mark).

3. The transaction file is a sequential file, hence you can't access records in this file via an index. Rather you just move to the next record and read it. (This is pretty petty, and you may not lose a mark for it).

4. ENDWHILE is one word and so is ENDIF. But who cares, you wouldn't lose a mark for this!!!

5. Bikes is a dumb name for this routine. UpdateInventory or similar would make more sense. Obviously, you wouldn't lose a mark for this either!!!

Certainly your solution would be amongst the best in the state!!!

Sam
 

Glide

Member
Joined
Oct 13, 2003
Messages
103
Wow, I never knew you could not access records with an index in a sequential file D:!

So I guess i'll leave that for random access. How would you (in pseudocode) check the 999th record for a sentenal value, you'd have to loop through the whole record? thats very inefficiant :(
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
Yep, you've gotta iterate through the file from the start. A sequential file, is just as the name implies, a simple sequence of data items.
 

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

Top