PROGRAMMING_-_QUESTIONS
PROGRAMMING_-_QUESTIONS
GETTING STARTED
Work in pairs to discuss how you tackle a programming problem. What do
you do first? How do you check that you have met all the requirements? What
features do you add to improve the readability of your program?
PLANNING A PROGR AM
Programmers have to tackle programming
problems every day. They have to
analyse the requirements and work out
how to write a program to meet those
requirements, in the same way that you
will need to. The only difference is that
programmers have been doing this process
for a long time, they have practised and
practised. This is the best way to tackle
how to write programs; practice, practice
and more practice. Most programs follow a
pattern, they will have inputs in the system,
they will include processes (actions), and
then output results. The challenge is
working out which order to put these in.
Consider a 3D computer game, where the Figure 11.1: A person playing a computer game
player moves an online character around a
virtual world, interacting with objects and other characters. This system has inputs, processes and outputs.
For example:
• Input: an arrow key from the user (up, down, left, right).
• Process: change the characters position depending on the key pressed.
• Output: the character in the new position.
The larger the problem, the more likely a design will need to be created first . This design will identify all
of the requirements of the problem, and begin to identify the inputs, processes and outputs. For some
programs there could be thousands (or more) inputs, processes and outputs. They all need to be identified
to make sure the design covers all the requirements.
The only difference between the process with a large problem, and the problem you will be tackling is the
size. You are only going to need to identify a small number of inputs, processes and outputs and you are
working on your own. A large program could have dozens of programmers, each working on their own part
of the problem.
Discussion questions
1 What might happen if you did not identify all of the requirements before starting to tackle the problem?
2 How do you tackle a problem? Do you just start creating a solution, or do you need to identify the
requirements and components first? Or does this depend on the problem?
11 Programming scenarios practice
ACTIVITY 11.1
Write down some of the inputs, processes and outputs for your favourite
computer game.
Peer Assessment
Compare your game with a partner's. Can you think of any more inputs,
processes or outputs for their game?
Example - calculator:
A program needs writing to act as a calculator. The program needs to:
• Take one integer value from the user.
• Take an operator as input; + for addition, - for subtraction,/ for
division, * for multiplication or /\ for power of.
• Take a second integer value from the user.
• Output the result.
Write a program using pseudocode or program code to solve the problem.
355 )
> CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
Once you have read it twice, start to identify the key features; the inputs, processes and
outputs. You could do this using a table, e.g.
Operator.
Then you need to work out how to get from the input to the output. This could be a
description, program statements, or anything that helps you to write down what happens.
If you get stuck here, try doing what the program needs. In the case of the calculator,
write down the first integer value (e.g. 3), write down the operator (e.g. +), write
down the second integer value (1). Then what do you do with this values? Because the
operator is + you will do 3 + 1. That is your process.
356 >
11 Programming scenarios practice
The _pseudocode conventions used in this chapter appear exactly as you will
l see 1n your exams.
Inputs
It is always best to include an output before the input, to tell the user what to enter.
This can be done in two statements, e.g.
OUTPUT "Enter a number"
INPUT number
or as one statement, e.g.
number ._ INPUT "Enter a number"
Whichever way you choose, make sure you are inputting a value into a variable.
In the examples above the variable is number. If you use this statement:
INPUT "Enter a number"
then the number will be input and it won't be stored anywhere, so you can't use it.
Example - calculator:
Write code for the inputs.
Numberl ._ INPUT "Enter a number"
Operator ._ INPUT "Enter an operator"
Number2 ._ INPUT "Enter a number"
Validation
Now you have your inputs, consider if there are limits on what should be entered.
Look at each input in turn and write down what is, and isn't allowed.
357 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
Example - calculator:
• First integer value - must be a whole number.
• Operator - must be+ - / * or A_
• Second integer operator - must be a whole number.
There are different ways that you could validate the inputs, e.g.
• Use an if statement and only allow the rest of the program to run if the
inputs it valid.
• Use a loop to continue asking for the input until it is valid.
Example - calculator:
REPEAT
Numberl <--- INPUT "Enter a number"
UNTIL Numberl.isinteger()
REPEAT
Operator<--- INPUT "Enter an operator"
UNTIL Operator "+" OR Operator = " " OR Operator "*" OR Operator II / 11
OR Operator 11 A 11
REPEAT
Number2 <--- INPUT "Enter a number"
UNTIL Number2.isinteger()
NB the statement . is Integer is not language specific, but as a pseudocode statement
it shows that you are looking to check if the value is an integer and only allow the input
if it is. You might have one for the language you choose, but it is better to use this than
a statement such as: until the number is an integer.
Processes
Revisit your design table and take the processes one at a time and work out whether
they need sequence, selection and/or iteration.
Example - calculator:
Process Construct
1 If+ is entered add the values. selection
2 If - is entered subtract the values. selection
3 If* is entered multiply the values. selection
4 If/ is entered divide the values. selection
5 If A is entered work out the power of. selection
358)
11 Programming scenarios practic
e
Outputs
Once you have written your processes, work out where your outputs should be. Make
sure you output using an appropriate message, for example, what the value is showing.
Example - calculator:
First integer value. If+ is entered add the values. Result from the calculation.
Operator. If - is entered subtract the values.
Second integer operator. If* is entered multiply the values.
If/ is entered divide the values.
if A is entered work out the power of.
The output is:
OUTPUT "The result is: " , Result
or
OUTPUT Numberl , Operator , Number2 , " " , Result
Example - calculator, full code: When outputting
REPEAT data, it is good
Numberl � INPUT "Enter a number" practice to include a
UNTIL Numberl.isinteger() message to say what
the output is. This is
REPEAT the same in a small
Operator� INPUT "Enter an operator" pseudocode problem
UNTIL Operator "+" OR Operator " " OR Operator rr * 11
as in a large program
OR Operator = "/" OR Operator = nAn being created by
many people. If you
REPEAT run a program and
Numberl� INPUT "Enter a number" it outputs a number
UNTIL Numberl.isinteger() without a message -
how do you know
IF Operator = "+" what that number
THEN means?
Result� Numberl + Number2
359)
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
Adding comments
You should always add comments to your code to explain what you have done.
Comments are statements in your program that will not be run. A comment will
always start with a specific symbol to state that this is where the comment starts, for
example, */,#or// .
Comments are used to explain sections of code, not every single statement.
Example - calculator:
// input 2 numbers and operator
REPEAT
Numberl� INPUT "Enter a number"
UNTIL Numberl.Isinteger()
REPEAT
Operator� INPUT "Enter an operator"
UNTIL Operator = "+" OR Operator = " " OR Operator "*" OR Operator .. I II
OR Operator = nAn
REPEAT
Numberl� INPUT "Enter a number"
UNTIL Numberl.Isinteger()
//check operator and perform calculation
IF Operator = "+"
THEN
Result� Numberl + Number2
ELSEIF Operator = "-"
THEN
Result� Numberl - Number2
ELSEIF Operator = "*"
THEN
Result� Numberl * Number2
ELSEIF Operator = "/"
THEN
Result� Numberl / Number2
ELSE
Result� Numberl A Number2
360)
11 Programming scenarios practice
ENDIF
//output the result
OUTPUT "The result is: Result
Final checks
Once you have finished your algorithm perform the following checks:
1 Have you stored all of your inputs in variables?
2 Have you validated inputs where possible?
3 Have you performed all of the processes?
4 Have you outputted the required values?
5 Have you included appropriate messages in your outputs?
6 Have you added comments?
7 Do all your variables have appropriate identifiers?
8 Return to the scenario and tick every statement that your program performs.
If you don't tick a statement then you need to add that to your program.
ACTIVITY 11.3
Read the calculator pseudocode solution above and complete the final checks
on the algorithm. Tick each on in turn to make sure it has been covered in the
solution.
TACKLING A PROBLEM
When you are given a problem to write a computer program for, you need to know
how to approach it to create a solution. In this Skills Focus you will be walked
through the production of a solution by analysing the problem, identifying the
components and writing a solution that covers all of the requirements. You can then
apply the same methodology to other problems to write a computer program to
solve the problem.
A computer game asks the user to enter a 5-letter word and then stores each letter in
a different index in the array, for example:
I I
'house' is input:
Index
Letter• h
0
A second user then has 10 guesses to try and work out what the word is. They have
to enter one letter at a time, and the program either outputs the position that letter
is in (e.g. 'h' is in 0), or that the letter is not in the word.
After guessing a letter the user gets a free guess at the word. If they get it correct
they win and the program ends. If the player has entered 10 letters and not guessed
correctly then they lose.
Write a program using pseudocode or program code for the computer game.
361 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
CONTINUED
Validation
The word that is input needs to be 5 characters long so that can be validated. The
character entered can be validated as to whether it is a character; there is no specific
way to do this but you could use a function such as IsCharacter(). Validating
the word is not as straightforward because most things could be accepted.
REPEAT
WordToGuess <--- INPUT "Enter a 5 character word"
UNTIL LENGTH(WordToGuess) = 5
REPEAT
UserLetter <--- INPUT "Enter a letter to guess"
UNTIL UserLetter.IsCharacter() = TRUE
Processes
Take these one at a time.
First: Store each letter in a different index in an array. To split the string you can use
either a function in your language, or a pseudocode function such as substring.
//INPUT word to guess
REPEAT
WordToGuess <--- INPUT "Enter a 5 character word"
362 )
11 Programming scenarios practi ce
CONTINUED
UNTIL LENGTH(WordToGuess) = 5
//store characters in array
LetterArray � SUBSTRING(WordToGuess, 0, 1), SUBSTRING(WordToGuess, 1, 1),
SUBSTRING(WordToGuess, 2, 1), SUBSTRING(WordToGuess, 3, 1)]
363 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
CONTINUED
Count = 0
364)
11 Programming scenarios practice
CONTINUED
IF UserWord = WordToGuess
THEN
OUTPUT "You win"
Count+- 11
ENDIF
Count+- Count + 1
ENDWHILE
Fifth: Perform the final checks:
1 Have you stored all of your inputs in variables?
2 Have you validated inputs where possible?
3 Have you performed all of the processes?
4 Have you outputted the required values?
5 Have you included appropriate messages in your outputs?
6 Have you added comments?
7 Return to the scenario and tick every statement that your program performs.
If you don't tick a statement then you need to add that to your program.
Questions
1 Which elements of a program do you first need to identify?
2 What is a process in a program?
3 What do you need to include in your final checks?
Example - calculator:
A program needs writing to act as a calculator. T he program needs to:
• Take one integer value from the user.
• Take an operator as input; + for addition, - for subtraction, / for
division, * for multiplication or /\ for power of.
• Take a second integer value from the user.
• Output the result.
Write a program using pseudocode or program code to solve the problem.
365 )
> CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
Adding extras
It is best practice to add the following to your algorithms:
• Messages to tell people what to input.
• Validation of inputs.
• Messages to say what is being output.
• Comments to explain the code.
1 Messages to tell people what to input:
Numberl +- INPUT "Enter the first number"
Operator +- INPUT "Enter an operator2
Number2 +- INPUT "Enter the second number"
366 >
11 Programming scenarios pra
ctice
IF Operator = "+"
THEN
OUTPUT Numberl + Number2
ELSEIF Operator = "-"
THEN
OUTPUT Numberl - Number2
ELSEIF Operator = "*"
THEN
OUTPUT Numberl * Number2
ELSEIF Operator = "/"
THEN
OUTPUT Numberl / Number2
ELSE
OUTPUT Numberl A Number2
ENDIF
2 Validate inputs:
REPEAT
Numberl <- INPUT "Enter the first number"
UNTIL Nwnberl.Isinteger = TRUE
REPEAT
Operator <- INPUT "Enter an operator"
UNTIL Operator = 11 + 11 OR Operator = 11 - 11
OR Operator = 11 * 11
OR Operator = 11 /11
OR Operator = 11" 11
REPEAT
Number2 <- INPUT "Enter the second number"
UNTIL Nwnber2.Isinteger = TRUE
IF Operator = "+"
THEN
OUTPUT Numberl + Number2
ELSEIF Operator = "-"
THEN
OUTPUT Numberl - Number2
ELSEIF Operator = "*"
THEN
OUTPUT Numberl * Number2
ELSEIF Operator = "/"
THEN
OUTPUT Numberl / Number2
ELSE
OUTPUT Numberl A Number2
ENDIF
3 Messages with outputs:
REPEAT
Numberl <- INPUT "Enter the first number"
UNTIL Numberl.Isinteger = TRUE
367 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
REPEAT
Operator r INPUT "Enter an operator"
UNTIL Operator = "+" OR Operator = "-" OR Operator "*" OR Operator .. I 11
OR Operator = nAn
REPEAT
Number2 r INPUT "Enter the second number"
UNTIL Number2.Isinteger = TRUE
IF Operator = "+"
THEN
OUTPUT "The addition = 11
REPEAT
Operator r INPUT "Enter an operator"
UNTIL Operator "+" OR Operator = " " OR Operator "*" OR Operator II/ II
OR Operator = HAn
REPEAT
Number2 r INPUT "Enter the second number"
UNTIL Number2.Isinteger = TRUE
368 )
11 Programm ing scenarios practice
IF Operator = "+"
THEN
OUTPUT "The addition = "
OUTPUT Numberl + Number2
ELSEIF Operator = "-"
THEN
OUTPUT "The subtraction
OUTPUT Numberl - Number2
ELSEIF Operator = "*"
THEN
OUTPUT "The multiplication =
OUTPUT Numberl * Number2
ELSEIF Operator = "/"
THEN
OUTPUT "The division - "
OUTPUT Numberl / Number2
ELSE
OUTPUT "The power of = "
OUTPUT Numberl Number2A
ENDIF
TACKLING A PROBLEM
In this problem an array is needed to store data within the problem. This Skills Focus
will take you through the production of a solution to a problem that makes use of an
array. You can then use this same methodology when solving your own problems.
A 2D array stores the cost of items and the quantity sold of 100 items. For example,
part of the array could store the data in the table.
Cost 10.00 15.99 20.22 13.78 8.99 6.20 4.30 10.00 12.00
Quantity 5 6 8 10 2 15 3 16 10
A program is needed to:
• Ask the user whether they want to see the:
• total number of items sold
• total cost of all items sold
• quantity of items that sold less than 10
• quantity of items that sold more than or equal to 10.
• Calculate the answer to the item selected.
• Output the total with an appropriate message.
369)
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
CONTINUED
Write an algorithm in pseudocode or program code to perform these actions.
You do not need to initialise the values in the array.
Walk through the problem
Enter 1. Enter 2.
Calculate 1 was chosen, so add up 2 was chosen so calculate If 1 was chosen total items.
answer. total number of items = 5 total cost
+6+8+10+2+15+3 If 2 was chosen, multiply cost by
cost* quantity for each quantity and total values.
+16+10
item
If 3 was chosen check if each
10 * 5 +15.99+ 6, etc. value is less than 10.
Important note: this program uses an array. To check, count, or calculate using each
item in an array you will need to use a loop. A count-controlled loop, e.g. for loop, is
most appropriate.
Take each step and turn it into pseudocode.
Step 1 - output and input of choice.
OUTPUT "Enter 1 for total number of items sold, 2 for total cost of all items
sold, 3 for number of items less than $10, 4 for number of items $10 or more".
INPUT Choice
Step 2 - totalling number of items sold.
IF Choice = 1
THEN
Total .- 0
FOR Count .- 0 TO 100
Total .- Total + Array[Count, 1)
NEXT Count
OUTPUT "Total number of items sold is " , Total
ENDIF
370)
11 Programming scenarios practice
CONTINUED
371 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
CONTINUED
IF Choice = 1
THEN
Total <- 0
FOR Count <- 0 TO 100
Total <- Total + Array[Count, 1]
NEXT Count
OUTPUT "Total number of items sold is " , Total
ELSEIF Choice = 2
THEN
Total <- 0
FOR Count <- 0 TO 100
Total<- Total + (Array[Count, OJ * Array[Count, 1])
NEXT Count
OUTPUT "Total cost of items sold is ", Total
ELSEIF Choice = 3
THEN
Numberitems <- 0
FOR Count <- 0 TO 100
IF Array[Count, 1] < 10
THEN
Numberitems <- Numberitems + 1
ENDIF
NEXT Count
OUTPUT "Number of items that sold less than 10 " Numberitems
ELSEIF Choice = 4
THEN
Numberitems <- 0
FOR Count <- 0 to 100
IF Array[Count, 1] >= 10
THEN
Numberitems <- Numberitems + 1
ENDIF
NEXT Count
OUTPUT "Number of items that sold 10 or more " Numberitems
ENDIF
Final checks
• Messages to tell people what to input. (✓)
• Validation of inputs - needs to be done.
• Messages to say what is being output. (✓)
• Comments to explain the code.
-----
11 Programming scenarios practice
CONTINUED
//Take user choice from menu
REPEAT
OUTPUT "Enter 1 for total number of items sold, 2 for total cost of all
items sold, 3 for number of items less than $10, 4 for number of items
$10 or more."
INPUT Choice
UNTIL Choice = 1 or Choice = 2 or Choice = 3 or Choice = 4
IF Choice = 1
THEN //if choice is 1 total number of items sold
Total <--- 0
FOR Count <--- 0 TO 100
Total <--- Total + Array[Count, 1]
NEXT Count
OUTPUT "Total number of items sold is " , Total
ELSEIF Choice = 2
THEN //if choice is 2 total cost of all items sold
Total <--- 0
FOR Count <--- 0 TO 100
Total<--- Total + (Array[Count, o J * Array[Count, 1])
NEXT Count
OUTPUT "Total cost of items sold is " , Total
ELSEIF Choice = 3
THEN //if choice is 3 count items less than 10
Numberitems <--- 0
FOR Count <--- 0 TO 100
IF Array[Count, 1] < 10
THEN
Numberitems <--- Numberitems + 1
ENDIF
NEXT Count
OUTPUT "Number of items that sold less than 10 " Numberitems
ELSEIF Choice = 4
THEN //if choice is 4 total items 10 or more
Numberitems<--- 0
FOR Count <--- 0 TO 100
IF Array[Count, 1] >= 10
THEN
Numberitems <--- Numberitems + 1
ENDIF
NEXT Count
OUTPUT "Number of items that sold 10 or more " Numberitems
ENDIF
373 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
The user can then enter a command to move right, move left, move up
or move down.
Getting started
1 Identify the inputs into the system.
Practice
Write a program using pseudocode or program code to solve the problem.
Challenge
1 The player's x coordinate can only be between 0 and 200.
The player's y coordinate can only be between 0 and 150.
If the user's guess is too low the program outputs "Too low".
If the user guesses correctly the program outputs the number of guesses
they had and the game ends.
If the user has 10 guesses that are all incorrect then the game outputs the
number and tells the user they have lost.
374)
11 Programming scenarios practice
CONTINUED
Getting started
1 Identify the data that needs to be input into the system.
Practice
1 Write the program using pseudocode or program code.
Challenge
Programs can use random number generators. Find out how to use a random
number generator and use this to declare the number at the start of the game,
instead of hard coding a value into the program. Amend your program to use
the random number generator.
Practice
Write a program using pseudocode or program code to solve the problem.
Challenge
Extend your program to ask the user what type of data they are entering, and
what type of data they want output. For example, they could enter the denary
number 1 and want the 8-bit binary output 00000001.
375 >
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
Getting started
1 Identify the inputs to the system.
2 Identify the outputs to the system.
3 Identify the processes that need to take place.
Practice
Write a program using pseudocode or program code to solve the problem.
Challenge
Extend the program to store the weight and cost of each item entered in an
array. When the last item is entered store the weights and costs in a text file.
Practice
Write a program using program code or pseudocode to allow a user to enter
a username and password and store this in the array Logins.
376)
11 Programming scenarios practice
CONTINUED
Challenge
The usernames and passwords can be stored in a text file instead of the array.
Each item can be on a different line, e.g.
username1
password1
username2
password2
Find out how to read and write multiple lines from a text file. At the start of
the program read in all the usernames and passwords to an array. At the end
of the program write all the usernames and passwords back into the file.
Getting started
1 Play the game with another student.
Practice
1 Write a program using pseudocode or program code for the game
to be played by two players.
2 Amend your program to use a function to determine which player wins. This
should take the two moves as parameters and return which player won.
377 )
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
CONTINUED
Challenge
SUMMARY
EXAM-STYLE QUESTIONS
1 A program takes a number from the user. It then counts from 1 to the number
that the user has entered (inclusive). If the number being input is divisible by
5 it outputs a message. If the number being input is divisible by 7 it outputs
a message.
Write a program using pseudocode or program code to solve the problem. [15)
2 A program stores the name (e.g. Dahlia), maximum height in cm (e.g. 100)
and if they can live in shade (e.g. TRUE) of plants in one or more arrays.
The program asks the user to enter their requirement for a plant including:
• The minimum height required.
• If it needs to live in shade or not.
The program should then output all of the plants that meet the requirements.
Write a program using pseudocode or program code to solve the problem.
You can assume the array(s) have already been initialised with 50 plants. [15)
378)
11 Programming scenarios practice
CONTINUED
3 A computer game for two players uses a set of cards. Each card has an
animal; horse, elephant, cat or lion.
Each player starts the game with 15 cards stored in an array; Playerl ,
Player2.
The first card for each player is compared. The player who wins the round
follows these rules:
• Lion always beats horse.
• Elephant always beats lion .
• Horse always beats cat.
• Cat always beats elephant.
The program compares each card in turn until it has compared all 16 sets of cards.
The player who wins each comparison gets 1 point. If there is a winner at
the end of the game, the program should store the winner's score in a text file,
and output the player's name.
Write a program using pseudocode or program code to solve the problem.
You can assume the cards have already been divided between the two players. [15]
4 A program asks the user to enter the cost of 100 items. Each cost should
be stored in an array, Costs . The program should calculate and output:
• The total cost of the items.
• The average cost of the item.
• The cost of the most expensive item.
• The cost of the least expensive item.
Write a program using pseudocode or program code to solve the program. [15]
5 A program stores words in a ID array, Words , with 1000 elements.
The program needs to ask the user to select one of the three actions for
the program to perform.
1 Identify and output how many words have 5 or more letters.
2 Identify and output the longest word in the array.
3 Identify and output the shortest word in the array.
The program needs to use a function for each of the three actions that are
called from the main program.
Write a program using pseudocode or program code to solve the problem.
You can assume the array is already initialised with 1000 words. [15]
379)
) CAMBRIDGE IGCSE™ & 0 LEVEL COMPUTER SCIENCE: COURSEBOOK
After studying this chapter, think about how confident you are with the different topics.
This will help you to see any gaps in your knowledge and help you to learn more effectively.
You might find it helpful to rate how confident you are for each of these statements when you are revising.
You should revisit any topics that you rated 'Needs more work' or 'Getting there'.
- - -- - - - - +- - - - -- -
- -- - --
See Needs Getting Confident
' I can ...
topic more work there to move on
. ---- - __,_ -- --- _...,_______ _
_ -- -� - -- - - -- -- - - --- - - - -