A-level Computing/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2013 Exam/Section B

Section B Introduction

edit

This section will ask you questions that you will have to find programming solutions to. The past exam papers have always provided a strict program specification in the form of variable names and pseudocode. It is very likely that a problem will be set around the theme of cryptography but different to the skeleton code in some way. You should be able to interpret pseudocode, structured english, structure charts, stepwise refinement tables and decision tables. Any of these techniques could be used to communicate an algorithm to be coded.

Practice questions

edit

Obviously all the past papers from AQA have good section B questions that you should attempt. Here are some around this year's theme...

Numeric Caesar Cipher

edit

(a) Write a program that encrypts a number sequence using the same principle as the Caesar Cipher. (b) Test your program using a key= 6 and the number sequence 0123456789

OUTPUT "enter a number to encode"
INPUT plainNum
OUTPUT "enter a key"
INPUT Key
For i = 0 to (length of plainNum -1)
      codedNum <-- codedNum & (VALUE(PlainNum(i)) + Key) Mod 10
Next
OUTPUT codedNum

Answer:

    Sub Main()
        Dim plainNum, codedNum As String
        Dim Key As Integer
        Console.WriteLine("enter a number to encode")
        plainNum = Console.ReadLine()
        Console.WriteLine("enter a key")
        Key = Console.ReadLine()
        For i = 0 To (plainNum.Length - 1)
            codedNum = codedNum & ((Val(plainNum(i)) + Key) Mod 10)
            'Val is a bit of a cheat... subtracting 48 from the ascii code converts a character in the range 48-58 to its integer value
            'codedNum = codedNum & ((Asc(plainNum(i)) - 48 + Key) Mod 10) ' this line is a nicer way to solve it, althrough both only cope with number method specified
            'Obviously it is irrelevant which way is nicer, you MUST use the specified algorithm
        Next
        Console.WriteLine(codedNum)
        Console.ReadLine()
    End Sub


Character Frequency

edit

Using the average frequency that characters appear is one way to "crack" many simple ciphers. You can learn more about this here: http://en.wikipedia.org/wiki/Letter_frequency.

Write a program that counts the number of times each character appears in some inputted text. It should output a list of characters with the number of times each appears.

(pseudo-code to follow)

Answer:

'solution to follow 
    Sub Main()
        Console.WriteLine("enter a string")
        Dim text As String = Console.ReadLine()
        Dim textarr(text.Length - 1, 1) As Object
        Dim search As Boolean
        Dim searchele, newele As Integer
        For i = 0 To text.Length - 1
            search = False
            For j = 0 To text.Length - 1
                If text(i) = textarr(j, 0) Then
                    search = True
                    searchele = j
                End If
            Next
            If search Then
                textarr(searchele, 1) += 1
            Else
                textarr(newele, 0) = text(i)
                textarr(newele, 1) = 1
                newele += 1
            End If
        Next
        For i = 0 To newele - 1
            Console.WriteLine(textarr(i, 0) & " appears " & textarr(i, 1) & " times")
        Next
        Console.ReadLine()
    End Sub


Another question....

Answer:

'Another solution