0% found this document useful (0 votes)
51 views10 pages

CS 9618 Pseudocode Problems Fancy

AS Level Computer Science Guide

Uploaded by

debbiemuler1523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views10 pages

CS 9618 Pseudocode Problems Fancy

AS Level Computer Science Guide

Uploaded by

debbiemuler1523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cambridge AS & A Level Computer Science (9618)

Pseudocode Problems & Solutions

Prepared to match the Cambridge pseudocode specification


Problem 1 — Simple Selection and Arithmetic
Problem: Write pseudocode to read an integer 'n' and output whether it is 'Positive', 'Negative'
or 'Zero'.
Solution:
READ n
IF n > 0 THEN
OUTPUT "Positive"
ELSE IF n < 0 THEN
OUTPUT "Negative"
ELSE
OUTPUT "Zero"
END IF

Explanation:
Use chained selection to test the sign of n; comparisons follow Cambridge
pseudocode style.
Problem 2 — Iteration and Accumulators
Problem: Read integers until a sentinel value -1 is read. Output the count of numbers entered
(excluding sentinel) and their mean (average) as a real number with two decimal places.
Solution:
count ← 0
sum ← 0
WHILE TRUE DO
READ x
IF x = -1 THEN
EXIT WHILE
END IF
count ← count + 1
sum ← sum + x
END WHILE
IF count = 0 THEN
OUTPUT "No numbers entered"
ELSE
mean ← sum / count
OUTPUT FORMAT(mean, 2) // show 2 decimal places
END IF

Explanation:
Use an infinite loop with sentinel. FORMAT(...) indicates real formatting
per pseudocode guide.
Problem 3 — Linear Search in an Array
Problem: Given an array A of n integers and an integer target, write pseudocode to return the
index of target in A, or -1 if not found (1-based indexing).
Solution:
FUNCTION LinearSearch(A, n, target)
FOR i ← 1 TO n DO
IF A[i] = target THEN
RETURN i
END IF
END FOR
RETURN -1
END FUNCTION

Explanation:
Simple sequential scan; matches Cambridge conventions for arrays and FOR
loops.
Problem 4 — Bubble Sort (Ascending)
Problem: Write pseudocode to sort an array A of n integers into ascending order using bubble
sort (in-place).
Solution:
PROCEDURE BubbleSort(A, n)
FOR pass ← 1 TO n-1 DO
FOR i ← 1 TO n-pass DO
IF A[i] > A[i+1] THEN
temp ← A[i]
A[i] ← A[i+1]
A[i+1] ← temp
END IF
END FOR
END FOR
END PROCEDURE

Explanation:
Classic bubble sort. Use in-place swaps; indexing per 1-based array notation
used in Cambridge materials.
Problem 5 — 2D Array Traversal (Matrix Sum)
Problem: Given a 2D array M with r rows and c columns, compute the sum of all elements and
return it.
Solution:
FUNCTION MatrixSum(M, r, c)
total ← 0
FOR i ← 1 TO r DO
FOR j ← 1 TO c DO
total ← total + M[i][j]
END FOR
END FOR
RETURN total
END FUNCTION

Explanation:
Nested loops to visit each cell; use M[i][j] for 2D access.
Problem 6 — File I/O: Count Words
Problem: Write pseudocode to read a text file 'input.txt' and count how many words appear
(assume words separated by whitespace).
Solution:
count ← 0
OPEN file 'input.txt' FOR READING
WHILE NOT EOF(file) DO
line ← READLINE(file)
IF line ≠ "" THEN
words ← SPLIT(line)
count ← count + LENGTH(words)
END IF
END WHILE
CLOSE file
OUTPUT count

Explanation:
SPLIT without arguments splits on whitespace. EOF and file operations follow
Cambridge-style pseudocode.
Problem 7 — Procedure with Parameters (Statistics)
Problem: Write a procedure that takes an array A and its length n and returns both the
minimum and maximum values found.
Solution:
PROCEDURE MinMax(A, n, OUT minVal, OUT maxVal)
minVal ← A[1]
maxVal ← A[1]
FOR i ← 2 TO n DO
IF A[i] < minVal THEN
minVal ← A[i]
END IF
IF A[i] > maxVal THEN
maxVal ← A[i]
END IF
END FOR
END PROCEDURE

Explanation:
Procedure uses OUT parameters to return two results, matching Cambridge
guidance on headings.
Problem 8 — Recursion: Factorial
Problem: Write pseudocode for a recursive function Factorial(n) that returns n! for n ≥ 0 (with
0! = 1).
Solution:
FUNCTION Factorial(n)
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * Factorial(n - 1)
END IF
END FUNCTION

Explanation:
Simple recursion with base case n = 0.
Notes & References
Pseudocode style follows the Cambridge 'Pseudocode Guide for Teachers' and the 9618
syllabus conventions: use of ← for assignment, 1-based array indexing in many exam
resources, FOR and WHILE loops, PROCEDURE/FUNCTION headings, and file operations.

This booklet is an educational resource and examples are simplified for clarity. For official spec
and exact pseudocode syntax, refer to the Cambridge pseudocode guide and the official 9618
syllabus.

Prepared by: AI assistant (aligned to Cambridge 9618 pseudocode guidance).

You might also like