Introduction To Problem Solving Handout/Worksheet
Introduction To Problem Solving Handout/Worksheet
Pseudocode
A pseudocode is an imitation computer program written using mathematical notations and English-like statements
to describe the logics to solve a problem or carry out a procedure.
Output Statements
Input Statements
Assignment statements
When writing pseudocodes we use two key words BEGIN and END or START and STOP to state that we are
starting our process and stopping the process, however, please note that these words does not affect the process.
The word BEGIN is placed before all the statements in the pseudocode and END is placed at the end of all the
statements within the pseudocode. See example below:
BEGIN
Statement1
Statement2
Statement3
END
1
OUTPUT STATEMENTS
The output statement is a statement used to get information to the programmer or computer user. The key
words used for output statements are PRINT or OUTPUT; however we will be using PRINT.
NB. The information that we want to print to the screen must be placed between quotation marks “ “.
E.g. If I want to print “Campion students are the best” to my screen, my out put statement will be written
as:
BEGIN
PRINT “Campion students are the best” Computer Screen
END
Print statements are used to either request information from the user (prompt) or to give results of certain
processes in the pseudocode to the user.
Exercise:
1. Write a pseudocode algorithm to print to the screen “Hello. My name is (your name)”
3. Write a pseudocode algorithm to print to the screen “If you’re happy and you know it say amen”
2
INPUT STATEMENTS
The input statement is used to get data from outside the computer via some input device into a variable for
manipulation by the pseudocode. The two key words used for input statements are READ and INPUT,
however we will be using READ.
Variable: In math a variable is a symbol, word or letter used to represent an unknown value. When we
write programs we don’t know the value of which the user will enter into the program. E.g. I could ask a
user to enter two numbers and that user enters 20 and 10 another could enter 5 and 15 and so on. These
values are unknown to me (the programmer). So it is therefore necessary to use variables for unknown
values. A variable is a named location in memory, the value of which may change during execution of a
program.
Memory
Observe the diagram above depicting variables in memory. The small boxes are the actual space (variable)
in memory where the data will be stored. Of which the data can be changed during the execution of the
program based on the definition of a variable. However these spaces must be labeled or be identified.
Programmers use what are called identifiers/variable names (Num1, Num2, Fname, Lname) to identify
what is being stored in a variable.
1. They must be ONE word in other words they should not contain any space. (If you have two or more
words simple join them together or join them using and underscore)
• Constant: This provides locations for storing data which do not change value during execution of a
• Identifier: This is the name invented by a programmer for a data item. An identifier can be the name of a
3
Let us look at an example of how we use input statements. Ready?
BEGIN
PRINT “Please enter two numbers”
READ A
READ B
END
Based on the pseudocode “please enter two numbers” will be printed to the screen. Then the user can enter
the two ‘unknown’ numbers which will be stored in variables A and B displayed in memory.
We use the print statement before the read statement to ask the user to enter the two numbers; in this case
the print statement is acting as a ‘prompt’. We could also use the print statement where it is used to give
back results of a process. See example below
2. Write a pseudocode algorithm to read two numbers into variables A and B and print back the numbers
to the user.
BEGIN
PRINT “Please enter two numbers”
READ A
READ B
PRINT “The first number you’ve entered is”, A
PRINT “The second number you’ve entered is”, B
END
Please enter two numbers
COMPUTER SCREEN _
The first number you’ve entered is ____
The second number you’ve entered is ___
Exercise:
1. Write a pseudocode algorithm to input the colour and price of a car.
4
ASSIGNMENT STATEMENT
Assignment statements are used to give initial value to variables and to change the value assigned
to a variable.
The assignment statement has two parts, the Lvalue and the Rvalue. The Lvalue refers to the
variable as the storage location where the Rvalue will be stored. The Rvalue refers to a value,
which may be the result of an expression or the content of another variable. The arrow (←) or = is
used as the assignment operator.
Days ← 28
Rate ← 500
This can be interpreted as follows, store the value 28 in the variable days and 500 in the variable
rate.
The Lvalue is “salary” on the left and the Rvalue is the expression on the right, “days*rate”. The
assignment can be interpreted as, multiply the value in the variable days by the value in the variable
rate and store the result in variable salary.
Write a pseudocode algorithm to accept three numbers and find and print their sum.
We can analyze this question by finding out the Input, process and output, creating what is called an IPO
(Input, process, Output) table.
IPO Table has three columns and is used to analyse a problem. The three columns are:
Input – What is need from the user (what is needed from the person using the algorithm)
Process – What is done by the program (ask yourself the following question. “What do I have to do
with the inputs in order to produce the desired output)
Output – What is given to the user from the computer (the end result that is stated in the problem)
Display Sum
BEGIN
PRINT “Please enter three numbers”
READ Num1 Please enter three numbers
READ Num2 _
READ Num3 The 1st number you’ve entered is ____
PRINT “The 1st number you’ve entered is”, Num1 The 2nd number you’ve entered is ____
PRINT “The 2nd number you’ve entered is”, Num2 The 3rd number you’ve entered is____
PRINT “The 3rd number you’ve entered is”, Num3
END
5
Exercise:
1. Write a pseudocode algorithm to read three numbers and find their products and average and print
the sum, average and product. Also create the IPO table
2. Write a pseudocode algorithm to calculate the cost of a product given the quantity and the unit
price. The cost, quantity and unit price must be printed. Also
3. Write a pseudocode algorithm to accept the current year and the year a person was born. Calculate
and print the age of the person.
Trace Table - This is a technique used to test algorithms to make sure that no logical errors occur during
the processing of the algorithm.
Write a pseudocode algorithm to accept three numbers and find and print their sum.
BEGIN
END
6
Flow Chart –
- The oval is used as a terminal point which shows where the algorithm starts and
stops.
- The square or rectangle is used to represent processing. This symbol is for all
calculations or assignments.
- The diamond represents decision. This symbol is used to allow the user to make a
choice between one process and another
- The circle is used as a connector. This symbol allows two flow lines to connect
back to the main process after a decision has been made
- The arrow signifies a flow line. The flow line connects the different symbols and
shows the direction in which the algorithm should flow