Question 1 Report
An ATM (Automated Teller Machine) withdrawal process works as follows:
(a) Write pseudocode for this ATM withdrawal algorithm. Assume the correct PIN is stored in a variable StoredPIN and the account balance is in a variable Balance. [8]
(b) Complete the trace table for the following scenario: StoredPIN = "1234", Balance = 500. User enters PIN "0000", then "1234", then amount 80. [4]
| Step | PIN entered | Attempts | PIN correct? | Amount | Valid amount? | Balance | OUTPUT |
|---|---|---|---|---|---|---|---|
| 1 | |||||||
| 2 | |||||||
| 3 |
(a) The pseudocode implements an ATM withdrawal with PIN verification (up to 3 attempts) and amount validation (multiple of 10, within balance). [8]
DECLARE StoredPIN : STRING
DECLARE EnteredPIN : STRING
DECLARE Balance : REAL
DECLARE Amount : INTEGER
DECLARE Attempts : INTEGER
DECLARE Authenticated : BOOLEAN
Attempts ← 0
Authenticated ← FALSE
REPEAT
OUTPUT "Enter PIN: "
INPUT EnteredPIN
Attempts ← Attempts + 1
IF EnteredPIN = StoredPIN THEN
Authenticated ← TRUE
ELSE
OUTPUT "Incorrect PIN. ", 3 - Attempts, " attempts remaining."
ENDIF
UNTIL Authenticated = TRUE OR Attempts >= 3
IF Authenticated = FALSE THEN
OUTPUT "Card locked. Contact your bank."
ELSE
REPEAT
OUTPUT "Enter withdrawal amount (multiple of 10): "
INPUT Amount
IF Amount MOD 10 <> 0 THEN
OUTPUT "Amount must be a multiple of 10"
ELSE
IF Amount > Balance THEN
OUTPUT "Insufficient funds. Balance: ", Balance
ELSE
Balance ← Balance - Amount
OUTPUT "Dispensing ", Amount
OUTPUT "Remaining balance: ", Balance
ENDIF
ENDIF
UNTIL Amount MOD 10 = 0 AND Amount <= Balance
ENDIFThe PIN loop uses a REPEAT...UNTIL that exits when the PIN matches or 3 attempts are exhausted [1]. The attempt counter increments before the check so the limit is enforced correctly [1]. After authentication, a second loop handles the amount, rejecting non-multiples of 10 and amounts exceeding the balance [1].
(b) Trace for StoredPIN = "1234", Balance = 500, inputs: "0000", "1234", amount 80: [4]
| Step | PIN entered | Attempts | PIN correct? | Amount | Valid amount? | Balance | OUTPUT |
|---|---|---|---|---|---|---|---|
| 1 | 0000 | 1 | No | - | - | 500 | Incorrect PIN. 2 attempts remaining. |
| 2 | 1234 | 2 | Yes | - | - | 500 | (proceeds to amount entry) |
| 3 | - | 2 | Yes | 80 | Yes (80 MOD 10 = 0, 80 ≤ 500) | 420 | Dispensing 80, Remaining balance: 420 |
The first attempt fails, consuming one of three tries [1]. The second attempt succeeds, so the loop exits with Authenticated = TRUE [1]. The amount 80 passes both checks (divisible by 10, within balance), so 80 is subtracted from 500 [1].
Everything you need to excel in your exams