Computer Science GCSE and A Level Courses and Resources Basket

The Cambridge International AS and A Level Computer Science Coursebook is generally a fantastic resource, covering the entire 9608 syllabus in depth.

However, there are a few frustrating issues with the book, including some errors. For example, on page 245 as part of an exam-style question, the following pseudocode is given for an algorithm to convert a denary number to binary:

FUNCTION Binary(Number : INTEGER) : STRING
    DECLARE BinaryString : STRING
    DECLARE PlaceValue : INTEGER
    BinaryString <- '' // '' empty string
    PlaceValue <- 8
    REPEAT
    IF Number>= PlaceValue
        THEN
            BinaryString <- BinaryString & '1' // concatenates two strings
            Number <- Number - PlaceValue
        ELSE
            BinaryString <- BinaryString & '0'
            PlaceValue <- PlaceValue DIV 2
    UNTIL Number = 0
    RETURN BinaryString
END FUNCTION 

If students attempt this question, they are likely to get confused when told the return value when n = 10 is 101.

This is because of an indentation error and a missing ENDIF. The IF block should be closed, and PlaceValue <- PlaceValue DIV 2 should be moved back one level of indentation. It will then be executed regardless of the outcome of the conditional branching, as required.

Below is a python implementation of the algorithm.

def Binary(Number: int) -> str:
    BinaryString: str = ''
    PlaceValue: int = 8

    print("Number\tBinaryString\tPlaceValue\tNumber >= PlaceValue")
    # Python has no REPEAT....UNTIL so we use while True and break
    while True:
        print("{}\t\t{}\t\t{}\t\t{}".format(Number, BinaryString, PlaceValue, Number >= PlaceValue))
        if Number >= PlaceValue:
            BinaryString = BinaryString + "1"
            Number = Number - PlaceValue
        else:
            BinaryString = BinaryString + "0"
        PlaceValue = PlaceValue // 2 
        if PlaceValue == 0:
            break
    return BinaryString

print(Binary(10))

A couple of points to notice:

  • I’ve used Python 3 type annotations as discussed in this article.

  • I’ve solved the second part of the question where it asks what is needed to make the algorithm correct: if PlaceValue == 0 instead of if Number == 0 (perhaps not obvious until you’ve seen it!).

  • The table in the textbook has the Boolean test Number >= PlaceValue offset by one row (vertically). Programming this into the output here would make the code overly complex and hard to follow.

Spend some time making sure you understand how this algorithm works. The more you do this, the easier it gets.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python