CSC207 CA 2024
Every question on the CSC207 CA 2024 paper, solved: reverseList, arrays and files, exact-output tracing with continue and break, and the auto teller algorithm with its flowchart. Straight to the answers.
A 9-page PDF with every question and solution. Print it, or read it offline on a phone.
What is inside
- A1areverseList()
- A1bDisadvantage of an array
- A1cFiles over arrays
- A2aExact output: switch
- A2bExact output: for loop
- A2cExact output: while with %
- A2dExact output: continue and break
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(...) and printf(...) stay on the same line.
Also solved: CSC207 CA 2025, last year’s paper.
reverseList()
CA 2024 · Section A · Question 1a
Section A: Answer on a separate sheet. Answer all Questions for a Maximum of 30 marks.
1. Arrays are a relevant data structure in manipulating data. Consider an integer list data structure constructed with an array data type with the form:
struct ArrayList {
int elements[MAX];
int size;
};a. Propose an algorithm called reverseList() which displays the values in an array called ArrayList[ ] in the reversed order. For example: the list 2, 3, 10, 23, 50 will be reversed to display: 50, 23, 10, 3, 2.
Solution
void reverseList(ArrayList *list) { for (int i = list->size - 1; i >= 0; i--) { print(list->elements[i]); // last element first if (i > 0) print(", "); // no comma after the last value } println();}
Start at the last position, size - 1, and walk back to 0. For 2, 3, 10, 23, 50 it displays 50, 23, 10, 3, 2. The list itself is not changed: it is only displayed backwards.
Disadvantage of an array
CA 2024 · Section A · Question 1b
b. What is the disadvantage of using an array in general?
Solution
An array has a fixed size: MAX is chosen before the program runs. It cannot grow when more values arrive, and the unused places still take memory. Inserting or deleting in the middle also means shifting every element after it.
Files over arrays
CA 2024 · Section A · Question 1c
c. What is the advantage of using file data structure over arrays?
(5 + 1 + 1 = 7 marks)
Solution
A file is permanent: its data is stored on disk and is still there after the program ends or the computer is switched off. An array lives in memory and is lost when the program stops. A file can also hold far more data than an array, since it is not limited to a fixed MAX.
Exact output: switch
CA 2024 · Section A · Question 2a
2. Carefully study the algorithms below and write down the “exact” output. Put your final output for each subquestion in a box.
a).
today=3;
switch(today)
{
case 1: println("stay home");break;
case 2: println("programing");break;
case 3: println("mathematics");break;
default: println(today);
}Solution
today is 3, so case 3 runs, prints, and break leaves the switch.
mathematics
Exact output: for loop
CA 2024 · Section A · Question 2b
b).
i = 1;
for(j=1; j<10; j=j+1){
println(i);
}
println();Solution
j takes 1, 2, … 9: nine rounds, each prints i, which stays 1. The last println() has nothing to print, so it only ends the line: an empty line.
1 1 1 1 1 1 1 1 1
Nine lines of 1, then one empty line.
Exact output: while with %
CA 2024 · Section A · Question 2c
c).
n=8;
i=1;
while(i<=n){
if(i%2 ==0){
printf(i);
}
i++;
}Solution
i runs from 1 to 8 and only the even numbers are printed: 2, 4, 6, 8. printf does not end the line, so they come out side by side with nothing between them.
2468
Exact output: continue and break
CA 2024 · Section A · Question 2d
d).
for(k=1; k<20; k+=4){
if(k%4==0){
continue;
}
if(k==17){
break;
}
println(k);
k++;
}
println(k);(2 * 4 = 8 marks)
Solution
Careful: k goes up by 1 inside the loop (k++) and then by 4 in the for.
| k at the test | k%4 == 0? | k == 17? | Prints | Next k |
|---|---|---|---|---|
| 1 | no | no | 1 | 1 + 1 + 4 = 6 |
| 6 | no | no | 6 | 6 + 1 + 4 = 11 |
| 11 | no | no | 11 | 11 + 1 + 4 = 16 |
| 16 | yes: continue | skipped | nothing | 16 + 4 = 20 |
| 20 | 20 < 20 is false: the loop ends | |||
k never equals 17, so break never runs. After the loop, println(k) prints 20.
1 6 11 20
The auto teller machine
CA 2024 · Section B · Question 1
Section B: Answer on a separate sheet (16 marks)
An automatic teller machine has a console as shown in the diagram below. The teller machine follows this sequence to assist a customer:
- The customer will insert their card and enter a PIN. A customer is allowed at most three tries at their PIN. If they get it wrong three times the whole process ends without ejecting the card.
- If the PIN is correct they will then select an action button (withdraw, deposit or balance).
- Next they will select the account type (savings or cheque).
- Finally, they will enter the amount in whole dollars (if appropriate), and press OK to confirm it, and the transaction will be processed.
The auto teller will eject the customer’s card and stop the process if the customer presses the ‘Cancel’ button.
| 1 | 2 | 3 | 4 | Withdrawal | Savings | ||
| 5 | 6 | 7 | 8 | Deposit | Cheque | ||
| 9 | 0 | Balance | OK | Cancel |
Using the different flow construct and subprogram, describe an algorithm which the auto teller could use to accept the details from the customer and act on them. Draw the corresponding flow chart.
Solution
Algorithm
It uses all three flow constructs: sequence (the steps in order), selection (if and switch), iteration (the while loop for the three PIN tries), and subprograms for each job the machine does.
// Subprograms (one job each)int readPin(); // 4 digits from the keypad, or -1 if Cancel is pressedbool checkPin(int pin); // true if the PIN matches the cardint readAction(); // 1 Withdrawal, 2 Deposit, 3 Balance, -1 Cancelint readAccount(); // 1 Savings, 2 Cheque, -1 Cancelint readAmount(); // whole dollars from the keypadbool pressedOK(); // true for OK, false for Cancelvoid ejectCard();void keepCard();void autoTeller() { int pin, action, account; int amount = 0; int tries = 0; bool correct = false; // 1. Card inserted: at most three tries at the PIN while (tries < 3 && correct == false) { pin = readPin(); if (pin == -1) { // Cancel ejectCard(); return; } correct = checkPin(pin); if (correct == false) tries++; } if (correct == false) { // wrong three times keepCard(); // end without ejecting the card return; } // 2. Action button action = readAction(); if (action == -1) { ejectCard(); return; } // 3. Account type account = readAccount(); if (account == -1) { ejectCard(); return; } // 4. Amount (not needed for Balance), then OK if (action != 3) amount = readAmount(); if (pressedOK() == false) { // Cancel ejectCard(); return; } processTransaction(action, account, amount); ejectCard();}void processTransaction(int action, int account, int amount) { switch (action) { case 1: withdraw(account, amount); break; case 2: deposit(account, amount); break; case 3: println("Balance: ", balance(account)); break; }}
Flowchart
In exact-output questions, watch every change to the loop variable: inside the loop and in the for header.