Introduction to algorithms · CA 2025

CSC207 CA 2025

Every question on the CSC207 CA 2025 paper, solved: ordered set snippets, isPalindrome, exact-output tracing, and the course registration pseudocode with its flowchart. Straight to the answers.

5 min read 6 questions

Download CA 2025 Solutions

An 8-page PDF with every question and solution. Print it, or read it offline on a phone.

How to use this page

The blue box is the question exactly as it is on the paper. The solution follows it directly. Section A is the short questions, Section B the pseudocode and flowchart.

Solutions use the course pseudocode: curly braces, typed functions, * and -> for structs, and // comments. read(x) reads a value, println(...) prints and ends the line, print(...) stays on the same line.

Also solved: CSC207 CA 2024, the paper from the year before.

A1

initSet() and contains()

CA 2025 · Section A · Question 1

Question A1

1. A set is a data structure which helps us to collect items/objects of the same type/domain. An ordered set is one whose position of its elements is relevant. Consider the following structure for an ordered set:

struct OrderedSet{
       int data[MAX_SIZE]; // store elements
       int size;

};

Propose the code snippet(s) ONLY that is at position (a) and (b) for the methods to initialize an ordered set (initSet()) or check if an ordered set contains some element (contains()). Initializing a set means deleting/clearing all its elements/data, while “contains” is to check if a value is an element of the set. (2+3 = 5 marks)

void initSet(OrderedSet *set) {
       ___(a)___

}

bool contains(OrderedSet *set, int value) {
       for (int i = 0; i < set->size; i++) {
               ___(b)___
       }
       return false;

}

Solution

(a)

set->size = 0;    // no elements left: the set is cleared

(b)

if (set->data[i] == value)    return true;    // found: stop and answer yes

In place:

void initSet(OrderedSet *set) {    set->size = 0;                    // (a)}bool contains(OrderedSet *set, int value) {    for (int i = 0; i < set->size; i++) {        if (set->data[i] == value)    // (b)            return true;    }    return false;}
A2

isPalindrome()

CA 2025 · Section A · Question 2

Question A2

2. Consider the following structure for a word:

struct Word{
       char elements[MAX];
       int length;

};

A word is a palindrome if it is exactly the same as the word itself, when written in the reversed order. For example: “dad” and “mom” are palindromes. “grace” is not a palindrome because the reversed word order gives “ecarg”. Assume the characters are in lower case. Propose an algorithm called isPalindrome() with appropriate input parameter(s) and returned value, which can be used to determine if a word is a palindrome or not. (4 marks)

Solution

bool isPalindrome(Word *w) {    int left = 0;    int right = w->length - 1;    while (left < right) {        if (w->elements[left] != w->elements[right])            return false;             // a mismatch: not a palindrome        left++;        right--;    }    return true;                      // every pair matched}

Input: the word (a pointer to Word). Returns true if it is a palindrome, false otherwise. “dad” → true, “grace” → false (g ≠ e).

A3a

Exact output: switch

CA 2025 · Section A · Question 3a

Question A3a

3. Carefully study the algorithms below and write down the “exact” displayed output. Put your final output for each sub-question in a box/frame. (2 x 3 = 6 marks)

(a)

today= 5;
switch(today)
{
       case 1: print("stay home");break;
       case 2: print("programing");break;
       case 3: print("mathematics");break;
       default: print(today);
}

Solution

today is 5: no case matches, so default prints the value of today.

Exact output
5
A3b

Exact output: for loop

CA 2025 · Section A · Question 3b

Question A3b

(b)

i = 1;
for(j=1; j<10; j=j+2){
       println(i);
}
println(j);

Solution

j takes 1, 3, 5, 7, 9: five rounds, each prints i, which stays 1. The loop stops when j = 11, and the last line prints it.

Exact output
1
1
1
1
1
11
A3c

Exact output: while with %

CA 2025 · Section A · Question 3c

Question A3c

(c)

n=10;
i=1;
while(i<=n){
       if(i%3 == 0){
               println(i);
       }
       i++;
}

Solution

i runs from 1 to 10, and only the multiples of 3 are printed.

Exact output
3
6
9
B1

Course registration and ranking

CA 2025 · Section B · Question 1

Question B1

Section B (Answer on a separate sheet)

1. Consider the following scenario: A student is asked for the students’ name and how many courses registered. If the student registered up to 4 courses, grade the student, and above four courses notify the student to drop excess courses and register only for four.

Ranking of students is done as follows: for average greater than 90 is “Excellent”, average greater than 80 “Very Good”, average greater than 60 “Good” and otherwise failed.

(a) Write the Pseudocode for the above problem. (b) Draw the corresponding Flow chart. (8+10 = 18 marks)

Solution

(a) Pseudocode

void gradeStudent() {    char name[50];    int cr;                             // courses registered    float avg;                          // the student's average    read(name);    read(cr);    if (cr > 4) {        println(name, ": drop excess courses and register only 4");    } else {        read(avg);        if (avg > 90)            println("Excellent");        else if (avg > 80)            println("Very Good");        else if (avg > 60)            println("Good");        else            println("Failed");    }}

(b) Flowchart

StartRead name, crcr > 4 ?YesDisplay“Drop excess courses”avg > 90 ?YesDisplay “Excellent”avg > 80 ?YesDisplay “Very Good”avg > 60 ?YesDisplay “Good”NoRead avgNoNoNoDisplay “Failed”End
Flowchart for Section B, Question 1

Trace by hand, line by line, and write what is printed exactly as it would appear. That is most of the marks in Section A.

CSC207 CA 2025

Every question on the CSC207 Introduction to Algorithms CA 2025 paper, solved in the course pseudocode, straight to the answers.

Anonymous reading stats and solution feedback help improve this book. No names or personal details are collected.

Questions are reproduced word for word from the CSC207 CA 2025 paper (University of Buea) so you can match them to your copy. Where the paper leaves a choice open, the solution says what was assumed.