CHP 8 Programming
CHP 8 Programming
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.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
36
8 Programming
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.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
37
8 Programming
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
b In your chosen programming language write a statement to only accept a menu choice
that is 1, 2, 3, 4 or 5.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
38
8 Programming
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
39
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
40
8 Programming
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
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
41
8 Programming
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
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
Photocopying prohibited Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
42
8 Programming
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.
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
.................................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
43
8 Programming
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
44
8 Programming
A ← ((B + C – D * E) ^ F) / G
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
45
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
46
8 Programming
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 ......................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
47
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
48
8 Programming
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'.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
49
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
50
8 Programming
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
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.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
51
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
52
8 Programming
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.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook Photocopying prohibited
53
8 Programming
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 Cambridge IGCSE and O Level Computer Science Algorithms, Programming and Logic Workbook
54