Вы находитесь на странице: 1из 3

1.

Daniel wrote the program below to print the larger of two numbers, assuming that the two numbers
are not equal. When Daniel compiled the program there were SEVEN errors.
1
Program Largest
2
Var X; Y: char;
3
Begin
4
Input(X, Y);
5
If X < Y
6
Then Writeln (X is the larger number);
7
Else Writeln (Y is the larger number)
8
End;
(a)

Identify the line number of EACH error and rewrite the correct statement. (7 marks)

Customers can withdraw cash from an Automatic Teller Machine (ATM).


withdrawal is refused if amount entered > current balance
withdrawal is refused if amount entered > daily limit
if current balance < $100, then a charge of 2% is made
if current balance $100, no charge is made
Write Pascal code to prompt a user to input the sum of money requested, then decide if a withdrawal can
be made and calculates any charges. Appropriate output messages should be included.
[15 Marks]

3.

4. If A and B are Boolean variables, draw a single


truth table to determine:
a)
b)
c)
d)

NOT (A) OR NOT (B)


NOT (A) AND NOT (B)
NOT (A AND B)
NOT (A OR B)
[8 marks]

Comment on your results

[2 marks]

5. Draw a Flow Chart only for an algorithm to find


the square root of any number. The error between
your answer and the exact square root should be no
greater than 0.0001. You are not allowed to use
mathematical functions.
Here is one method you may try.
1.
2.

Complete the following table showing the expected output from the
flowchart for the three, independent, sets of input data. [5 Marks]

3.
4.
5.

To find root(12)
Pick a number that when squared, comes close to (but is less
than) the number whose square root youre finding: 3 3 = 9.
This is a better choice than 4: 4 4 = 16
Divide the number youre finding the square root of (12) by
the number you squared (3) in step 2: 12 3 = 4
Average the closest square root (3) and the answer of step 3
(4): 3 + 4 = 7. 7 2 = 3.5
Square the average to see how close the number is to 12:

3.5 3.5 = 12.25Close, but not close enough!


Repeat steps 2 and 3 until the number squared is very close to 12:

[20 Marks]

Solutions
1.
1
2
3
4
5
6
7
8

Program Largest; Missing semi-colon


Var X, Y: char; (Comma should separate X and Y, not semicolon)
Incorrect type char will not cause a compile error!
Begin
Input(X, Y); Should be readln(X, Y);
If X < Y
Then Writeln (X is the larger number) No semicolon
Else Writeln (Y is the larger number); semicolon missing
Single quote missing at end of string literal
End; should be period .

Note: The program itself has a logic error in the output statements. This will not cause compilation errors.
2.
Possible solution:

Mark Scheme

program ATM;

At least 2 constants [2 Marks]

CONST
dailyLimit: real = 2500;

Real data type used [2 marks]

chargeRate: real = 0.02;

Messages [2 marks]

VAR

Field width specification used [1 mark]


currentBalance: real;
amtRequest, extraCharge: real;

Rejection logic [2 marks]

BEGIN

Calculate currentBalance [1 mark]

currentBalance:=10000;

Calculate extraCharge [1 mark]

extraCharge: =0;

No syntax errors [2 marks]

writeln(Please enter the amount requested);

No logic errors [2 marks]

readln(amtRequest);
IF (amtRequest > currentBalance) THEN
writeln(The amount requested exceeds your current balance. Program terminated.)
ELSE
IF (amtRequest > dailyLimit ) THEN
writeln(Daily limit exceeded. Program terminated)
ELSE (* we can process a valid transaction *)
Begin
IF ( currentBalance < 100 ) THEN extraCharge : = amtRequest * chargeRate;
currentBalance:= currentBalance (amtRequest + extraCharge);
writeln(Withdrawal amount

Extra charges

Current Balance);

writeln(amtRequest: 17:2, extraCharges:20:2,currentBalance:24:2);


writeln(Thank you for Banking with us. Goodbye!);
END;
END.

3.
INPUT X
48
9170
-800

OUTPUT S
2
4
1

4.
A

A OR B

A AND B

NOT(A OR B)

NOT(A AND B)

NOT(A)

NOT(B)

NOT(A) OR
NOT(B)

NOT(A) AND
NOT(B)

0
0
1
1

0
1
0
1

0
1
1
1

0
0
0
1

1
0
0
0

1
1
1
0

1
1
0
0

1
0
1
0

1
1
1
0

1
0
0
0

From the table we see the following results

NOT(A OR B) NOT(A) AND NOT(B)


NOT(A AND B) NOT(A) OR NOT(B)

5.
INPUT
userNum

PROCESSING

OUTPUT

absError = 0.0001

Enter a number to find the sq root

initialPick = 0
REPEAT
initialPick = initialPick + 1
pickSq = initialPick* initialPick
UNTIL (pickSq >= userNum)
If(pickSq=userNum) then avgPick=initialPick

Your square root is + avgPick

Else
Repeat
nextPick = usernum/initialPick
avgPick = (initialPick + nextPick) / 2
initialPick = avgPick
pickSq= avgPick * avgPick
error = userNum pickSq
if( error < 0) error:=error* (-1)
Until (userNum pickSq < absError)

STORAGE
absError: real
userNum: real

initialPick: real
nextPick: real
avgPick: real
pickSq: real
error: real

Вам также может понравиться