A program processes currency values and needs to display them in a standard format with exactly 2 decimal places. (a) Write pseudocode for a function Format...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

A program processes currency values and needs to display them in a standard format with exactly 2 decimal places.

(a) Write pseudocode for a function FormatMoney that takes a REAL amount and returns a string in the format "X.YY" (always showing 2 decimal places). For example, 5.0 should return "5.00" and 12.5 should return "12.50". [4]

(b) State the output for each input. [2]

InputOutput
7.0
15.999

(c) Explain why displaying currency values with consistent formatting is important. [2]

Answer Details

(a) The FormatMoney function converts a REAL amount into a string with exactly two decimal places.

FUNCTION FormatMoney(Amount : REAL) RETURNS STRING
    DECLARE Whole : INTEGER
    DECLARE Frac : INTEGER
    DECLARE Result : STRING
    Whole ← INT(Amount)
    Frac ← INT((Amount - Whole) * 100 + 0.5)
    IF Frac >= 100 THEN
        Whole ← Whole + 1
        Frac ← Frac - 100
    ENDIF
    Result ← NUM_TO_STR(Whole) & "."
    IF Frac < 10 THEN
        Result ← Result & "0" & NUM_TO_STR(Frac)
    ELSE
        Result ← Result & NUM_TO_STR(Frac)
    ENDIF
    RETURN Result
ENDFUNCTION

INT(Amount) extracts the whole part. [1] The fractional part is multiplied by 100 and rounded (adding 0.5 before truncation). [1] If rounding pushes Frac to 100 (e.g., 15.999 rounds to 16.00), the overflow is carried into Whole. [1] A leading zero is prepended for single-digit pence values (e.g., Frac = 0 becomes "00"). [1]

(b)

InputOutput
7.0"7.00"
15.999"16.00"

For 7.0: Whole = 7, Frac = INT(0 * 100 + 0.5) = 0. Since 0 < 10, a leading zero is added: "7.00". [1]

For 15.999: Whole = 15, Frac = INT(0.999 * 100 + 0.5) = INT(100.4) = 100. Since Frac >= 100, Whole becomes 16 and Frac becomes 0: "16.00". [1]

(c) Consistent formatting prevents confusion about the value displayed. [1] Without fixed decimal places, "5" and "5.00" represent the same value but look different. A value like "5.1" could be misread as 51 pence rather than 5 pounds 10 pence if the context is ambiguous. Uniform formatting makes financial data clearer for users and reduces the chance of misinterpretation. [1]

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning