WB Chapter 8 Programming
WB Chapter 8 Programming
Before starting to program, you must choose your programming language (tick when complete).
My choice of programming language is: ..........................................................................
Python
https://www.python.org/downloads/
The download is free. Download and install it. The IDE, called IDLE, will provide an editing window:
Photocopying prohibited
26 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
Java
https://www.bluej.org/
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
27
... and a terminal window to show the output from the program:
Photocopying prohibited
28 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
Visual Basic
https://visualstudio.microsoft.com/vs/express/
Download and install the community version. It is free and will provide an editing window:
Programs written in console mode, once compiled, run at the command prompt:
Action: download and install the IDE for your chosen programming language (tick when complete).
Now follow the instructions for your chosen programming language to write your first program.
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
29
If there are no syntax errors, then the program will run in the Python shell.
If there are syntax errors, then the IDE will show you where and provide an error message.
Action: write and run a Python program to output Hello World (tick when complete).
Photocopying prohibited
30 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
31
These are
comments with hints
for good
programming.
The HelloWorld
class has been set
up for you.
REMEMBER:
There must be matching braces at
the start and end of every class.
There must be a main program.
There must be a semi-colon at
the end of every programming
statement.
If the program has no syntax errors the class icon appears on the screen without any hatching.
32 Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
Right click on the icon then choose void main(String[] args). Then click on OK.
If there are syntax errors, then the IDE will show you where and provide an error message.
... and the class icon appears on the screen with hatching.
Action: write and run a Java program to output Hello World (tick when complete).
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
33
The editing window will appear with a skeleton program ready for you to add your code.
Suggestions will appear to help you write your code. Press Enter to accept a
highlighted suggestion.
Photocopying prohibited
34 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
Your program will run in a command line window. The Console.ReadKey() will ensure that the output
remains on screen.
If there are syntax errors, then the IDE will show you where and provide an error message.
Action: write and run a Visual Basic program to output Hello World (tick when complete).
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
35
Getting started
There is plenty of information about getting started using an IDE. Remember you will be writing
programs in console mode. Here are some examples.
1
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
2
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
3
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
2 a In your chosen programming language write a prompt and input statement(s) to enter
three whole numbers.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
36 Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
b In your chosen programming language write statement(s) to assign the values, 10, 20 and
30 to three variables.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
d In your chosen programming language, using your answers to a, b and c above, write and test a
program that assigns three user-input numbers to three variables and displays the values; then
assigns numbers to the same three variables and displays the values. You may need to declare
the variables used to store your numbers. Write or paste a copy of your program here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
37
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b In your chosen programming language write a statement to only accept a menu choice
that is 1, 2, 3, 4 or 5.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
38 Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
39
d Test your program with the following test data and complete this table.
0.4
5.1
−1
0.5 Option 1
0.5 Option 2
0.5 Option 3
5 Option 1
5 Option 2
5 Option 4
1 Option 5
a Counting:
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b Iteration:
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
c Selection:
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
40 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
d Sequence:
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
e String handling:
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
5 Put the following pseudocode statements in the correct order for this algorithm.
Make sure that you indent each statement correctly.
// Algorithm to input ten positive numbers and total them, output the total and then
// average
A ← 0
A ← A + C
B ← 0
B ← B + 1
D = A / B
DECLARE A, B, C, D : INTEGER
DECLARE D : REAL
INPUT C
OUTPUT A, D
OUTPUT "Please enter a positive number "
REPEAT
REPEAT
UNTIL B = 10
UNTIL C > 0
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
41
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
6 a In your chosen programming language write a program for an improved algorithm for
Question 5. Your program must use:
» meaningful variable names
» appropriate prompts and messages
» relevant comments.
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
4, 3, 7, 6 ,0, −3, 9, 1, 8, 5, 2, 5
42 Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
For each one, write and test a program to output the names of eight students that have
been stored in the array Student[0:7]. Use the variable Counter for the iteration.
b Extend one of your programs to work for any number of students, outputting the
list of students and number of students in the array.
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
43
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b In your chosen programming language write a program to perform the four string
operations from part a. Test your program with the string "Test String" stored in
the variable MyString. Write or paste a copy of your program here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
44 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
A ← ((B + C – D * E) ^ F) / G
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
45
c Write pseudocode statements to use a nested FOR .. NEXT loop to output the
numbers 1 to 10 twenty times.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
d Write and test a program to use a nested FOR .. NEXT loop to output the
numbers 1 to 10 twenty times. Write or paste a copy of your program here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
46 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
10 Use the following list of words and phrases to complete the paragraph below.
Each word can be used once, more than once or not at all.
» called » local
» code » parameters
» constant » procedures
» defined » program
» functions » return
» global » value
Tasks that are repeated many times in an algorithm make use of .............................................. and
Procedures and functions are .............................................. once and .............................................. many times.
They can be written with and without .............................................. . They are the variables that store the
A variable that can be used in any part of a program is called a .............................................. variable.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b Identify four library routines that you need to be able to use for IGCSE.
1
.......................................................................................................................................................................
2
.......................................................................................................................................................................
3
.......................................................................................................................................................................
4
.......................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
47
c Write and test a program that uses library routines to create two random integers with
values between 10 and 20, finds the quotient and remainder of the first integer divided
by the second, and outputs all four values with suitable messages. Write or paste a
copy of your program here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Array: ................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b Write a procedure in pseudocode to display the contents of a three by three array that
stores the current state of a noughts and crosses game. The display should look just like
a noughts and crosses game, using 'X's and 'O's on three separate lines.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
48 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
c Write a program that uses a procedure to display the contents of a three by three array
that stores the current state of a noughts and crosses game. Test the program with
this data. Each row must start on a new line. Write or paste a copy of your program here.
Here is an example of what might be displayed on screen:
O
X
X O
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b i Write pseudocode statements to store the word 'Test' in the file 'MyFile.txt'.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
49
ii Write pseudocode statements to read and display the word stored in the file 'MyFile.txt'.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
c Write a program to store the word 'Test' in the file 'MyFile.txt', then read and display
the word stored in the file 'MyFile.txt'. Write or paste a copy of your program here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
The program should call an appropriately named procedure or function for each option. It
should also include a loop that only stops if option 4 is selected.
Photocopying prohibited
50 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
b Write a procedure or function for option 1 from the program in part a to input a word to use for
a password. It should check the following about the word:
» It is between 10 and 20 characters in length.
» It does not contain any spaces.
» It contains at least one uppercase letter.
» It contains at least one digit between 0 and 9.
The procedure or function should output suitable error messages if the password does not meet
each of these conditions. Write or paste a copy of your procedure/function here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
51
Test your program with the following test data and complete this table.
Password
Password99
password99
Password99!
ExtraLargePassword9999999
21
c Extend your code from part b to store a valid password in the file 'MyPassword.txt'.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
52 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
d Write another procedure or function for option 2 from the program in part a to input a
password and check that it matches the password stored in 'MyPassword.txt'. Write or paste a
copy of your procedure/function here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
e Write another procedure or function for option 3 from the program in part a. It should ask the
user to enter the current password and check that this is correct (i.e. matches the one stored
in 'MyPassword.txt') before allowing them to change their password. The new password should
match all of the requirements of a password listed in part b. (HINT: It is possible to call one
procedure or function from within another procedure or function.) Write or paste a copy of your
procedure/function here.
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
............................................................................................................................................................................
Photocopying prohibited
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
53
15 Write and test a program that uses a two-dimensional array, Game[]to store the moves
in a noughts and crosses game. The program should meet the following requirements:
» Start each game with an empty array.
» Allow two players to input their moves in turn; the contents of the array are displayed
before and after every move.
» One player can input O and the other input X; no other moves are allowed, and no move
can use a space in the array already occupied.
» After every move the program should check for a completed line of three Os or three Xs,
and if found output the winner of the game.
» Use procedures and parameters.
» Include comments to explain how your code works.
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
..................................................................................................................................................................................
Photocopying prohibited
54 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook