Cs Workshop Final
Cs Workshop Final
Paper
Algorithms,Programming and
Algorithms,Programming and Logic
Logic
2
Paper II
3
4
5
6
Purpose of Algorithm
Step-by-step solution to a problem.
Must be clear and logical.
Pseudocode:
SET sum ← 0
FOR i ← 1 TO 5
sum ← sum + i
NEXT i
OUTPUT sum
Pseudocode:
FOR i ← 1 TO 10
OUTPUT i
NEXT i
Pseudocode:
INPUT age
IF age >= 0 AND age <= 120 THEN
OUTPUT “Valid”
ELSE
OUTPUT “Invalid”
ENDIF
Test Data
Normal: Expected [Link] accepted
Abnormal/Erroneous: Invalid [Link] rejected
Extreme:Largest and smallest [Link] accepted
Boundary: +/- [Link] value accepted other rejected
Pseudocode:
INPUT mark
IF mark >= 50 AND mark <=100
THEN
OUTPUT “Pass”
ELSE
OUTPUT “Fail”
ENDIF
Trace Tables
Track variable values step-by-step.
Used in dry run.
Pseudocode:
SET x ← 1
WHILE x <= 3
OUTPUT x
x←x +1
ENDWHILE
8
Errors in Algorithms
Syntax, Logic, Runtime errors.
Fix by testing and debugging.
Pseudocode:
IF num ? 10 THEN // should be = or < or > instead of ?
OUTPUT num
ENDIF
Pseudocode:
DECLARE age : INTEGER
age ← 15
Pseudocode:
count ← 1
String Handling
1. Length of String
name ← "Ahmed"
lngth ← LENGTH(name)
OUTPUT lngth // 5
2. Substring
text ← "Computer"
part ← SUBSTRING(text, 1, 4)
Library Routines
1. MOD (Modulus)
3. ROUND
4. RANDOM
DECLARE arr[5]
FOR i ← 1 TO 5
INPUT arr[i]
NEXT i
Pseudocode 2D:
First ←1
Last ←10
REPEAT
SWAP ←FALSE
FOR Index ← First TO Last-1
IF Num[Index] > Num[Index+1]
THEN
Temp ← Num[Index]
Num[Index] ← Num [ Index+1]
Num[Index+1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ←Last-1
UNTIL SWAP = FALSE OR LAST = 1
13
Files Handling
Store data permanently.
Read/Write operations.
CLOSEFILE "[Link]"
CLOSEFILE "[Link]"
Database Concepts
Table: rows (records), columns (fields).
Primary key uniquely identifies record.
SQL Format:
• SQL Examples
(1)
SELECT Name, Age, Marks
FROM Students
WHERE Marks > 80
ORDER BY Marks DESC;
(2)
SELECT EmployeeName, Department, Salary
FROM Employees
WHERE Department = "IT"
ORDER BY Salary ; // Sort in ascending order
(3)
SELECT COUNT(Name), Marks, Age // Count total number of students with > 80 marks
FROM Students
WHERE Marks > 80
(4)
SELECT Name, SUM( Marks), Age // Sum total marks of students with > 80 marks
FROM Students
WHERE Marks > 80
15
Logic Gates
AND, OR, NOT,NAND,NOR,XOR gates.
Used in circuits.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1 a) Four descriptions of stages in the program development life cycle are shown.
Program development life cycle description Program development life cycle stage
coding
design
[4]
(b) Identify three of the component parts after a problem has been decomposed.
1 ................................................................................................................................................
...................................................................................................................................................
2 ................................................................................................................................................
...................................................................................................................................................
3 ................................................................................................................................................
...................................................................................................................................................
[3]
32
2 Tick one box to show the name of the data structure used to store a collection of data of the
same data type.
A Array
B Constant
C Function
D Variable
[1]
33
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
. ................................................................................................................................................ [2]
(b) A validation check is used to make sure that any value that is input is an integer between 30
and 200 inclusive.
Give one example of each type of test data to check that the validation check is working as
intended. Each example of test data must be different.
Reason .....................................................................................................................................
...................................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
[6]
..........................................................................................................................................................
ROUND ..............................................................................................................................................
..........................................................................................................................................................
[4]
34
5 An algorithm has been written in pseudocode to allow some numbers to be input. All the
positive numbers that are input are totalled and this total is output at the end.
An input of 0stops the algorithm.
01 Exit ←1
02 WHILE Exit <> 0 DO
03 INPUT Number
04 IF Number < 0
05 THEN ←Total + Number
06 Total
07 ELSE
08 IF Number = 0
09 THEN
10 Exit 1 ←
11 ENDIF
12 ENDIF
13 ENDIF
14 OUTPUT "The total value of your numbers is ", Number
(a) Identify the four errors in the pseudocode and suggest a correction for each error.
Error 1 .......................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 2 .......................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 3 .......................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 4 .......................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
[4]
35
(b) Describe how you could change the corrected algorithm to record and output how
many positive numbers have been included in the final total.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
. ................................................................................................................................................ [4]
1 .......................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
...........................................................................................................................................................
[4]
36
START
Pointer 1
INPUT
Letter
IS No
Word[Pointer, 1] Pointer Pointer + 1
= Letter ?
Yes
INPUT Choice
Yes IS No
Choice ='Y' ? STOP
37
The table represents the two-dimensional (2D) array Word[]which stores the first half of the
phonetic alphabet used for radio transmission. For example, Word[10,1]is ‘J’.
Index 1 2
1 A Alpha
2 B Bravo
3 C Charlie
4 D Delta
5 E Echo
6 F Foxtrot
7 G Golf
8 H Hotel
9 I India
10 J Juliet
11 K Kilo
12 L Lima
13 M Mike
(a) Complete the trace table for the algorithm by using the input data: F, Y, D, N
[4]
38
...................................................................................................................................................
. ................................................................................................................................................ [1]
(c) Describe one problem that could occur with this algorithm if an invalid character was input.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
. ................................................................................................................................................ [2]
39
BLANK PAGE
40
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
. ................................................................................................................................................ [3]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
. ................................................................................................................................................ [2]
41
B Z
[4]
(b) Complete the truth table from the given logic expression.
Working space
A B C Z
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
[4]
42
10 A database table called TVRangeshows the main features and prices of a range of
televisions.
(a) Give the name of the field that is most suitable to be the primary key.
Field
..........................................................................................................................................
Reason
.....................................................................................................................................
...................................................................................................................................................
[2]
[Link] Fida
43
Complete the table to show the most appropriate data type for each
field. Each data type must be different.
(c) Complete the structured query language (SQL) query to return the television
(TV) code, screen size and price of all Smart TVs in the database table.
. ........................................................ TVRange
[Link] Fida
44
Question 11
[Link] Fida
45
Key Concepts
2D Array → NumberGenerated[player][round]
o Row = player (Player 1, Player 2)
o Column = each random number (1 to 100)
[Link] Fida
46
[Link] Fida
47
Task 7: Output
Display:
o Player names
o Scores
Show highest score first
[Link] Fida
48
Solution
// INITIALISE SCORES
Score1 ← 0
Score2 ← 0
FOR i ← 1 TO 100
IF NumberGenerated[1, i] > NumberGenerated[2 , i] THEN
Score1 ← Score1 + 2
ELSE IF NumberGenerated[2 , i] > NumberGenerated[1 , i] THEN
Score2 ← Score2 + 2
ELSE // both equal
Score1 ← Score1 + 1
Score2 ← Score2 + 1
ENDIF
NEXT i
[Link] Fida
49
// HANDLE TIE SITUATION
WHILE Score1 = Score2
Num1 ← RANDOM(1,6)
Num2 ← RANDOM(1,6)
ENDIF
ENDWHILE
ELSE
OUTPUT "Winner: ", Player2
OUTPUT Player2, " Score: ", Score2
OUTPUT Player1, " Score: ", Score1
ENDIF
END
[Link] Fida
50
Chapter 7
Chapter 8
2023 Specimen Paper 2, question 1, question 3, question 8, question 9, question 10, question 12 and question 13
[Link] Fida
Chapter 9 51
Chapter 10
[Link] Fida