Introduction to problem solving Handout/Worksheet
Objectives - Students should be able to:
• Define a program - This is a sequence of instructions, which instructs the computer as to what
tasks are to be performed, how to perform them and in what order.
• Algorithm - An algorithm is the blueprint for building a program and can be represented by using
pseudocodes and flowcharts.
• Define flowcharts - This is a diagram that illustrates how events are sequenced by time or order
using specialized symbols and flow lines
• Define 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.
• Define variables - This is a named location in memory, the value of which may change during
execution of a program
• Write pseudocode algorithm
• Draw flowcharts using basic structures such as assignment, conditional statements
• Use simple trace tablets
Solving a problem on a computer involves the following activities:
Definition of the problem – State what the problem is and what needs to be done
Analyze the problem. Understand the problem and determine the requirements.
Propose and evaluate possible solutions
Choose and develop the most appropriate algorithm (a method) for solving the problem.
Test and validate the algorithm. Check for errors in the algorithm
Implement the algorithm( ie Writing the program for the algorithm)
Test and Debug (check for errors in the program)
Document and maintain the program. (Write an explanation on how the program works)
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.
Pseudocode is made up of several statements:
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
Campion students are the best
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.
Can you write print statements of your own?
Exercise:
1. Write a pseudocode algorithm to print to the screen “Hello. My name is (your name)”
2. Write a pseudocode algorithm to prompt the user to enter two numbers
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.
Before we explore input statements further we must understand a few terms.
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
Num1 Num2 Fname Lname
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.
Rules in writing variable names:
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)
2. They should reflect what is being stored in them
3. They should NOT begin with a number
4. They should not be more than 64 characters
5. They should not be a reserved word. E.g. PRINT
• Constant: This provides locations for storing data which do not change value during execution of a
program. E.g. Pi.
• Identifier: This is the name invented by a programmer for a data item. An identifier can be the name of a
variable or a constant. E.g. Num or Pi.
3
Let us look at an example of how we use input statements. Ready?
1. Write a pseudocode algorithm to read two numbers into variables A and B.
BEGIN
PRINT “Please enter two numbers”
READ A
READ B
END
COMPUTER SCREEN MEMORY
Please enter two numbers
_
A B
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 ___
Now can you write input statements on your own?
Exercise:
1. Write a pseudocode algorithm to input the colour and price of a car.
2. Write a pseudocode algorithm to enter three values into variable A, B and C.
3. Write a pseudocode algorithm to read the name and sex of a student.
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.
Consider the following Assignment statement:
Days ← 28
Rate ← 500
This can be interpreted as follows, store the value 28 in the variable days and 500 in the variable
rate.
Consider the following assignment statement:
Salary ← Days * 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.
Use all three statements and complete the following questions:
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)
Steps in creating the IPO Table
Write the input/inputs
Write the process/processes
Write the output/outputs
INPUT PROCESS OUTPUT
Num1 Prompt to enter numbers. Sum
Num2
Num3 Calculate Sum OR
Sum=Num1+Num2+Num3
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
PRINT “Please Enter First Number”
READ Num1
PRINT “Please Enter Second Number”
READ Num2
PRINT “Please Enter Third Number”
READ Num3
SUM ← Num1 + Num2 + Num3
PRINT “The Sum is, Sum”
END
Steps to create a trace table
Create columns for the number of lines in the algorithm and variables (Input and Output)
Record each line and the code/statement, input and/or output that correspond to it.
Line Num1 Num2 Num3 Sum
1 - - - -
2 20 - - -
3 20 - - -
4 20 40 - -
5 20 40 - -
6 20 40 60 -
7 20 40 60 120
8 20 40 60 120
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 parallelogram represents Input or Output of data. This symbol is used to
allow the user to enter information or to allow the program to send information to
the user
- 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