A-level Computing/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2011 Exam/Section B
Section B Introduction
This section will ask you questions that you will have to find programming solutions to. There will be more than one way of answering each question, so the answers below are only indicative of what we are looking for.
As long as you get the same answers it doesn't matter how you do it. You may be given pseudocode to help you program the problem.
Batting Averages
Write a program to enter batting scores until the number 0 is input. And then output the total and average score.
Answer: | ||
Dim count As Integer
Dim total As Integer
Dim score As Integer
Dim average As Integer
Do
Console.WriteLine("Enter Score: ")
score = Console.ReadLine
total += score
count += 1
Loop Until score = 0
average = total / count
Console.WriteLine("Your total score is: " & total)
Console.WriteLine("Your average score is: " & average)
Console.ReadLine()
|
Answer alternate (BBC BASIC): | ||
LET count%=0: total=0 score=-1 average=0
WHILE score<>0
INPUT "Enter Score: (Or 0 to terminate entry)";"score
total+= score
count%+= 1
ENDWHILE
LET average = total/count : REM Calculate the average at end.
PRINT "Your total score is :";total
PRINT "Your average score is:"; average.
|
Password Cracking
Write code to keep asking for login, only accepting: username= PSN Password = easy. Otherwise it should say. “Not hacked yet”:
Answer: | ||
Dim username As String
Dim password As String
Do
Console.WriteLine("Enter username: ")
username = Console.ReadLine
Console.WriteLine("Enter password: ")
password = Console.ReadLine
If username = "PSN" And password = "easy" Then
Console.WriteLine("Hacked!")
Else
Console.WriteLine("Not Hacked yet")
End If
Loop Until username = "PSN" And password = "easy"
Console.ReadLine()
|
Alternate Answer (BBC BASIC): | ||
LET username$=""
LET password$=""
login%=FALSE
uservalid%=FALSE
passvalid%=FALSE
WHILE login%=FALSE
REPEAT
REPEAT
username$="":uservalid%=FALSE
INPUT"Enter Username: ";username$
IF username$<>"" THEN uservalid%=TRUE
UNTIL uservalid%
REM Quick and dirty implementation. Most real password fields would be masked input.
REPEAT
password$="":passvalid%=FALSE
INPUT"Enter Password: ";password$
IF password$<>"" THEN passvalid%=TRUE
UNTIL passvalid%
UNTIL uservalid% AND passvalid%
IF NOT(username$="PSN" AND password$="easy") THEN
PRINT"Not Hacked yet!"
ELSE
PRINT"Hacked!":login%=TRUE
ENDIF
ENDWHILE
|
Christmas Trees
Write code to print out a Christmas tree of a user defined size: even rows = *, odd = #. For example for the number 4 it should output:
# ** ### ****
Answer: | ||
Dim number As Integer
Console.WriteLine("Please enter a number: ")
number = Console.ReadLine
For count = 1 To number
If count Mod 2 = 0 Then
For x = 1 To count
Console.Write("*")
Next
Console.WriteLine()
Else
For x = 1 To count
Console.Write("#")
Next
Console.WriteLine()
End If
Next
Console.ReadLine()
|
Alternate Answer (BBC BASIC): | ||
LET number%=0
INPUT "Please enter rows required: "; number%
FOR count%=1 TO number%
IF count% MOD2=0 out$="*" ELSE out$="#"
PRINT'; : PRINT a new line.
FOR x=1 TO count% REM followed by the row
PRINT ;out$;
NEXT:
NEXT
|