0% found this document useful (0 votes)
3 views51 pages

Cs Workshop Final

The document outlines key concepts in algorithms, programming, and logic, including the Program Development Life Cycle (PDLC), pseudocode examples, data validation, and error handling. It discusses various programming structures such as arrays, procedures, and functions, along with database concepts and logic gates. Additionally, it provides pseudocode for common operations and examples of test data for validation checks.

Uploaded by

ayeshafahadddd
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)
3 views51 pages

Cs Workshop Final

The document outlines key concepts in algorithms, programming, and logic, including the Program Development Life Cycle (PDLC), pseudocode examples, data validation, and error handling. It discusses various programming structures such as arrays, procedures, and functions, along with database concepts and logic gates. Additionally, it provides pseudocode for common operations and examples of test data for validation checks.

Uploaded by

ayeshafahadddd
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

Paper IIII

Paper
Algorithms,Programming and
Algorithms,Programming and Logic
Logic
2

Algorithm , Programming and Logic

Paper II
3
4
5
6

Exam Summary notes

Program Development Life Cycle (PDLC)


 Analysis: Understand problem, inputs, outputs.
 Design: Plan solution using pseudocode/flowchart.
 Coding: Write program in a language.
 Testing: Find and fix errors.
Pseudocode:
 INPUT number
 IF number > 0 THEN
 OUTPUT 'Positive'
 ELSE
 OUTPUT 'Negative'
 ENDIF

Decomposition & Sub-systems


 System divided into smaller parts (modules).
 Simplifies complex problems.
 Development of subprograms
 Methods used to design and construct a solution using Flowcharts , Pseudocodes
and Structure Diagrams
 Decomposing a problem into its components
1. Input
2. Process
3. Output
4. Storage

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

Standard Methods of Solution


 Use of
1. Totalling
2. Counting
3. Max,Min and Average
4. Linear Search
5. Bubble sort
 Reusable logic patterns.
7

Pseudocode:
 FOR i ← 1 TO 10
 OUTPUT i
 NEXT i

Validation & Verification


 Validation: Check input is sensible, reasonable and accurate
 Types: Length, Range, Format, Presence, Type, Check digit
 Verification: Check input matches original.
 Types: Double Entry, Visual Check.

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

Writing Algorithms (Pseudocode)


 Use clear structure and keywords.
Pseudocode:
 INPUT a, b
 Sum ← a + b
 OUTPUT sum

Variables & Data Types


 Variables store data, constants don’t change.
 Types: Integer, Real, String, Char, Boolean.
 Variables
1. Must start with a letter, not a number
2. Can contain letters, numbers, and underscores
3. No spaces allowed
4. Should be meaningful (e.g. StudentAge, TotalMarks)
5. Must not use reserved keywords like IF, WHILE, INPUT, OUTPUT
6. Be consistent in naming style

Pseudocode:
 DECLARE age : INTEGER
 age ← 15

Input & Output


 Input: user data, Output: display result.
Pseudocode:
 INPUT name
 OUTPUT name
9

Sequence, Selection, Iteration


 Sequence: step-by-step execution.
 Selection/Condition: IF/CASE.
 Iteration: loops.
 Count Controlled ….FOR..TO…NEXT
 Condition Controlled
 Pre Condition…..WHILE…DO…ENDWHILE (executes untill condition is true)
 Post Condition ….REPEAT….UNTIL (executes until condition id false. Executes at least once)

Pseudocode:

DECLARE count : INTEGER

count ← 1

WHILE count <= 5 DO


OUTPUT count
count ← count + 1
ENDWHILE

String Handling

1. Length of String

Find number of characters in a string.

DECLARE name : STRING


DECLARE lngth : INTEGER

name ← "Ahmed"
lngth ← LENGTH(name)

OUTPUT lngth // 5

2. Substring

Extract part of a string.

DECLARE text, part : STRING

text ← "Computer"
part ← SUBSTRING(text, 1, 4)

OUTPUT part // Comp


10

3. Upper Case (UCASE)

Convert string to uppercase.

DECLARE word : STRING


word ← "hello"
word ← UCASE(word)
OUTPUT word // HELLO

4. Lower Case (LCASE)

Convert string to lowercase.

DECLARE word : STRING


word ← "WORLD"
word ← LCASE(word)
OUTPUT word // world

Library Routines

1. MOD (Modulus)

Gives the remainder after division.

DECLARE result : INTEGER


result ← 10 MOD 3
OUTPUT result // returns 1

2. DIV (Integer Division)

Gives the whole number part of division (no remainder).

DECLARE result : INTEGER


result ← 10 DIV 3
OUTPUT result // returns 3

3. ROUND

Rounds a number to nearest whole number (or given decimal places).

DECLARE num : REAL


num ← 3.2342
OUTPUT ROUND(num , 2) // returns 3.23
11

4. RANDOM

Generates a random number (usually between 0 and 1 or a given range).

DECLARE num : INTEGER


num ← RANDOM(1, 10)
OUTPUT num // returns any number between 1 and 10

Procedures & Functions


 Procedure: performs [Link] not return value
 Function: returns value.
 Local vs Global variables.
Pseudocode:
 FUNCTION Add(x, y) RETURNS REAL
 RETURN x + y
 ENDFUNCTION

Arrays (1D & 2D)


 Store multiple values of same data types
Pseudocode 1D:

 DECLARE arr[5]
 FOR i ← 1 TO 5
 INPUT arr[i]
 NEXT i

Pseudocode 2D:

 DECLARE marks : ARRAY[1:3, 1:2] OF INTEGER


 DECLARE i, j : INTEGER
 FOR i ← 1 TO 3
 FOR j ← 1 TO 2
 OUTPUT "Enter values”
 INPUT marks[i, j]
 NEXT j
NEXT i
12

Linear Search & Bubble Sort in 1D Arrays


 Linear Search: check each element to find specific data
 Bubble Sort: swap adjacent values for sorting
Pseudocode (Linear Search):

 OUTPUT “ENTER NAME TO FIND”


 INPUT Name
 Found ← False
 Counter ← 1
 REPEAT
 IF Name =StdName[Counter]
 THEN
 Found ←TRUE
 ELSE
 Counter ← Counter +1
 ENDIF
 UNTIL Found = TRUE OR Counter > ClassSize
 IF Found =TRUE
 THEN
 OUTPUT “NAME FOUND AT POSITION”,Counter
 ELSE
 OUTPUT “NOT FOUND”
 ENDIF

Pseudocode (Bubble Sort):

 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.

 Pseudocode: Write File (to save data)

OPENFILE "[Link]" FOR WRITE

WRITEFILE "[Link]", "Ali, 16, 85"


WRITEFILE "[Link]", "Sara, 15, 90"
WRITEFILE "[Link]", "Ahmed, 17, 78"

CLOSEFILE "[Link]"

 Pseudocode: Read File (to retrieve data)

OPENFILE "[Link]" FOR READ

READFILE "[Link]", StudentRecord

WHILE StudentRecord <> ""


OUTPUT StudentRecord
READFILE "[Link]", StudentRecord
ENDWHILE

CLOSEFILE "[Link]"

Database Concepts
 Table: rows (records), columns (fields).
 Primary key uniquely identifies record.
 SQL Format:

SELECT column_name, column_name, column_name


FROM table_name
WHERE condition
ORDER BY column_name [ASC | DESC];
14

• 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.

Draw one line to link each description

Not all program development life cycle stages will be used.

Program development life cycle description Program development life cycle stage

develop an algorithm to solve the problem analysis


by using structure diagrams, flowcharts or
pseudocode

coding

detect and fix the errors in the program

design

identify the problem and its requirements


evaluation

write and implement the instructions to


solve the problem testing

[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

3 (a) Describe what is meant by data validation.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

. ................................................................................................................................................ [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.

Give a reason for each of your choices of test data.

Normal test data .......................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................

Abnormal test data ...................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................

Extreme test data .....................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................
[6]

4 Explain the purpose of the library routines DIVand ROUND


DIV ...................................................................................................................................................

..........................................................................................................................................................
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.

You do not need to rewrite the algorithm.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

. ................................................................................................................................................ [4]

6 State two features that should be included to create a maintainable program.

Give a reason why each feature should be used.

1 .......................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

2 .......................................................................................................................................................

..........................................................................................................................................................

...........................................................................................................................................................
[4]
36

7 The flowchart represents an algorithm.

START

Pointer 1

INPUT
Letter

IS No
Word[Pointer, 1] Pointer Pointer + 1
= Letter ?

Yes

OUTPUT "Letter ", Letter, " is


represented by ", Word[Pointer, 2]

OUTPUT "Another Letter? (Y or N)"

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

Pointer Letter Choice OUTPUT

[4]
38

(b) Identify the type of algorithm used.

...................................................................................................................................................

. ................................................................................................................................................ [1]

(c) Describe one problem that could occur with this algorithm if an invalid character was input.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

. ................................................................................................................................................ [2]
39

BLANK PAGE
40

8 The function LENGTH(Phrase)calculates the length of a string Phrase

(a) Write the pseudocode statements to:


• store the string "The beginning is the most important part"in Phrase
• calculate and output the length of the string
• output the string in upper case.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

. ................................................................................................................................................ [3]

(b) Write the output your pseudocode should produce.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

. ................................................................................................................................................ [2]
41

9 Consider this logic expression.

Z = (NOT A OR B) AND (B XOR C)

(a) Draw a logic circuit for this logic expression.

Each logic gate must have a maximum of two inputs.

Do not simplify this logic expression.

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.

TVCode ScreenSize Satellite SmartTV SoundBar Price$


TV90SaSmSd 90 YES YES YES 9750.00
TV75SaSmSd 75 YES YES YES 8500.00
TV75SaSd 75 YES NO YES 8000.00
TV65SaSmSd 65 YES YES YES 6000.00
TV65SmSd 65 NO YES YES 5000.00
TV65SaSd 65 YES NO YES 5000.00
TV55SaSmSd 55 YES YES YES 4000.00
TV55SaSd 55 YES NO YES 3500.00
TV55SmSd 55 NO YES YES 3500.00
TV50SaSmSd 50 YES YES YES 2500.00
TV50Sa 50 YES NO NO 1750.00
TV50Sm 50 NO YES NO 1750.00
TV40Sa 40 YES NO NO 1200.00
TV40 40 NO NO NO 950.00
TV32 32 NO NO NO 650.00

(a) Give the name of the field that is most suitable to be the primary key.

State the reason for this choice.

Field
..........................................................................................................................................

Reason
.....................................................................................................................................

...................................................................................................................................................

[2]

[Link] Fida
43

(b) The database uses the data types:


• text
• character
• Boolean
• integer
• real
• date/time.

Complete the table to show the most appropriate data type for each
field. Each data type must be different.

Field Data type


TVCode
ScreenSize
SmartTV
Price$
[2]

(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.

SELECT TVCode, ......................................................., .......................................................

. ........................................................ TVRange

WHERE SmartTV =............................................................. ;


[4]

[Link] Fida
44

Question 11

[Link] Fida
45

What the Question is About

 A number game between 2 players


 Each player gets 100 random numbers (1–6)
 Numbers are compared one by one
 Points are given based on comparison
 Final winner is decided based on total points

Key Concepts

 2D Array → NumberGenerated[player][round]
o Row = player (Player 1, Player 2)
o Column = each random number (1 to 100)

Rules of the Game

 Generate 100 numbers (1–6) for each player


 Compare numbers:
o Higher number → 2 points
o Same number → both get 1 point
 Repeat for all 100 numbers
 Add total points

If totals are equal:

 Keep generating new numbers


 Compare until one wins
 Winner gets +2 points

[Link] Fida
46

What You Are REQUIRED to Do

Your program must:

1. Input player names


o Validate (not empty, valid text)
2. Generate random numbers
o 100 for Player 1
o 100 for Player 2
o Store in NumberGenerated
3. Compare numbers
o Apply scoring rules
4. Calculate total points
o Keep separate totals for both players
5. Handle tie situation
o Keep generating numbers until winner found
6. Output results
o Show:
 Player names
 Total points
o Display in descending order (highest first)

[Link] Fida
47

Break it into SMALL TASKS (Very Important)


Task 1: Input
 Ask for Player 1 name
 Ask for Player 2 name
 Validate input

Task 2: Generate Numbers


 Loop 100 times
 Generate random number (1–6)
 Store for both players

Task 3: Initialize Scores


 score1 ← 0
 score2 ← 0

Task 4: Compare Numbers


 Loop from 1 to 100:
o If Player1 > Player2 → score1 += 2
o If Player2 > Player1 → score2 += 2
o Else → both +1

Task 5: Check Winner


 If score1 > score2 → Player 1 wins
 If score2 > score1 → Player 2 wins

Task 6: Handle Tie


 While scores are equal:
o Generate new numbers
o Compare once
o Winner gets +2

Task 7: Output
 Display:
o Player names
o Scores
 Show highest score first

[Link] Fida
48

Solution

// INPUT AND VALIDATE PLAYER NAMES

OUTPUT "Enter Player 1 Name: "


INPUT Player1
WHILE Player1 = ""
OUTPUT "Invalid input. Enter Player 1 Name again: "
INPUT Player1
ENDWHILE
OUTPUT "Enter Player 2 Name: "
INPUT Player2
WHILE Player2 = ""
OUTPUT "Invalid input. Enter Player 2 Name again: "
INPUT Player2
ENDWHILE

// INITIALISE SCORES
Score1 ← 0
Score2 ← 0

// GENERATE 100 RANDOM NUMBERS FOR EACH PLAYER


FOR i ← 1 TO 100
NumberGenerated[1 , i] ← RANDOM(1,6)
NumberGenerated[2 , i] ← RANDOM(1,6)
NEXT i

// COMPARE NUMBERS AND ASSIGN POINTS

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

OUTPUT "Tie detected. Generating extra numbers..."

Num1 ← RANDOM(1,6)
Num2 ← RANDOM(1,6)

IF Num1 > Num2 THEN


Score1 ← Score1 + 2

ELSE IF Num2 > Num1 THEN


Score2 ← Score2 + 2

ENDIF

ENDWHILE

// OUTPUT RESULTS IN DESCENDING ORDER

IF Score1 > Score2 THEN


OUTPUT "Winner: ", Player1
OUTPUT Player1, " Score: ", Score1
OUTPUT Player2, " Score: ", Score2

ELSE
OUTPUT "Winner: ", Player2
OUTPUT Player2, " Score: ", Score2
OUTPUT Player1, " Score: ", Score1

ENDIF

END

[Link] Fida
50

Exam Practice Questions

Chapter 7

March 2019 Paper 22, question 2, question 3 and question 5

June 2019 Paper 21, question 4

June 2019 Paper 22, question 2, question 3 and question 5

June 2019 Paper 23, question 2, question 3 and question 4

Nov 2019 Paper 21, question 1(e), question 2 and question 4

Nov 2019 Paper 22, question 2, question 3 and question 4

Nov 2019 Paper 23, question 5 and question 6

March 2020 Paper 22 question 2, question 3 and question 4

June 2020 Paper 21, question 2, question 3, question 4 and question 5

June 2020 Paper 22, question 1(b), question 3 and question 4

June 2020 Paper 23, question 3, question 4, question 5 and question 6

2023 Specimen Paper 2, question 2, question 4, question 5 and question 7

Chapter 8

March 2019 Paper 22, question 1 and question 4

June 2019 Paper 21, question 1, question 2 and question 3

June 2019 Paper 22, question 1 and question 4

June 2019 Paper 23, question 1 and question 5

Nov 2019 Paper 21, question 1

Nov 2019 Paper 22, question 1, question 5 and question 6

Nov 2019 Paper 23, question 1 and question 4

March 2020 Paper 22, question 1

June 2020 Paper 21, question 1 and question 4

June 2020 Paper 22, question 1, question 2 and question 5

June 2020 Paper 23, question 1, question 2, question 3 and question 4

2023 Specimen Paper 2, question 1, question 3, question 8, question 9, question 10, question 12 and question 13

[Link] Fida
Chapter 9 51

June 2019 Paper 21 Question 5(a) and 5(b)

June 2019 Paper 22 Question 5(a)

June 2019 Paper 23 Question 6(a)

Nov 2019 Paper 21 Question 5(a) and 5(b)

Nov 2019 Paper 22 Question 7(a)

Nov 2019 Paper 23 Question 7(a) and 7(b)

March 2020 Paper 22 Question 5(a)

June 2020 Paper 21 Question 6(a)

June 2020 Paper 22 Question 6(a)

June 2020 Paper 23 Question7 (a)

2023 Specimen Paper 2 Question 11

Chapter 10

March 2019 Paper 12, question 5

June 2019 Paper 11, question 2

June 2019 Paper 12, question 8

June 2019 Paper 13, question 4

Nov 2019 Paper 11, question 2

Nov 2019 Paper 12, question 4

Nov 2019 Paper 13, question 6

March 2020 Paper 12, question 6

June 2020 Paper 11, question 2

June 2020 Paper 12, question 4

June 2020 Paper 13, question 6

2023 Specimen Paper 2, question 6

Note: from 2023 Boolean logic questions will be set in paper 2.

[Link] Fida

You might also like