PROBLEM SOLVING
Pseudo Code is expressing the solution to a task chronological order using English like terms.
Algorithm is using a series of steps in sequence stating how to solve a problem or carry out a task.
The characteristics of a good algorithm are:
Precision – the steps are precisely stated (defined).
Uniqueness – results of each step are uniquely defined and only depend on the input and the
result of the preceding steps.
Finiteness – the algorithm stops after a finite number of instructions are executed.
Input – the algorithm receives input.
Output – the algorithm produces output.
Generality – the algorithm applies to a set of inputs.
STEPS TO PROBLEM SOLVING
1. Define the problem
2. Analyze the problem
3. Develop solutions to solve the problem
4. Evaluate and select the best solution
5. Implement the best solution
6. Evaluate the solution
ALGORITHM INPUT/ OUTPUT STATEMENTS
The words in the table below are a few of the words used for algorithm purposes. When writing
algorithms or programs they are called reserved words.
Input or assignment statements are words used in an algorithm with assign constants, variables,
characters, numbers or strings to a variable.
Output statements are those statements which will display a message to the screen or display the contents
of a variable.
INPUT STATEMENTS OUTPUT STATEMENTS
Algorithm
Let, Read, Input, = Print, Display, Write, Display, Show
Pascal
Readln, Read Writeln, Write
It should also be noted that input statements also store. Meaning when a constant or variable is assigned
to a memory area that variable becomes storage for that data.
IMPORTANT POINTS FOR WRITING ALGORITHMS:
1. Start your algorithms with Start/Begin
2. At the beginning write a brief notation of what the algorithm is supposed to accomplish.
3. Note the author of the algorithm or program.
4. Ensure to declare all the variables you intend to use in the algorithm or the program.
5. It is also good practice to initialize all the variables being used especially if you’re going to use it
to increment of accumulate.
6. It is a good idea to start with a verb.
7. Programs/Algorithm should flow in a logical easy to read construct, indent where necessary.
8. Programs/Algorithms should be as user friendly as possible.
9. All Loops and conditional statement (IF, ElseIF, Else, Then) should be indented.
10. Always end with Stop/End.
CONSTANTS & VARIABLES
Constants are elements, data areas or memory allocations in a program than never changes value data
that is assigned.
Variables however do change value throughout a program and have specific data types indicating that
kind of data the program is expecting to store.
Data Types:
String-these are variables to contain words or characters, values which will not be used in
calculations
Integers-these are whole numbers whether negative or positive.
Real-are numbers with decimal (floating point).
Character-single letters, number, symbol etc.
Boolean-indicates a value of yes or no/ true or false.
IPO CHARTS (Input & Store, Process and Output)
An IPO chart is a process of determining the processes or construct of writing an algorithm. It helps in
assisting an algorithm developer to determine how to develop an algorithm, pseudo code by examining
the necessary constructs and how they relate.
Let’s examine a question: Develop an algorithm to calculate and display the area of any rectangular
designed room.
INPUT PROCESS OUTPUT
The length of the room, The width of the room Area = length times width Area
Example
PRINT "Enter the length of a room" Area = L * W PRINT "The area of the room is: "; area
INPUT L
PRINT "Enter the width of same room"
INPUT W
Declare L, W, area as integer
Start
PRINT "Enter the length of a room"
INPUT L
PRINT "Enter the width of same room"
INPUT W
Area = L * W
PRINT "The area of the room is: "; area
Stop
LOOPS IN ALGORITHM
FOR NEXT DO WHILE REPEAT UNTIL
Start Start Start
FOR P = 1 TO 5 P=1 P=1
PRINT "Enter the length of a room" DO WHILE P < 5 REPEAT
INPUT L PRINT "Enter the length of a room" PRINT "Enter the length of a room"
PRINT "Enter the width of same room" INPUT L INPUT L
INPUT W PRINT "Enter the width of same room" PRINT "Enter the width of same room"
Area = L * W INPUT W INPUT W
PRINT "The area of the room is: "; area area = L * W Area = L * W
END FOR PRINT "The area of the room is: "; area PRINT "The area of the room is: "; area
Stop P=P+1 P=P+1
END WHILE UNTIL P > 5
Stop Stop
A loop construct is used when a sequence of code is to be repeated a number of times.
CSEC focuses on three types of loops For-Next, Do-While, Repeat-Until.
For-Next Loops repeats a specific number of times.
Do-While Loops repeat while a condition is true.
Repeat-Until repeats while a condition is false or becomes true.
FOR NEXT LOOPS
To write a For-Next loop you need only to write 2 lines of construct.
1. Immediately above the lines of code you wish to repeat enter For X = 1 to 5.
a. X is the variable to automatically count the number of loops.
b. 1 indicates which number the loop should start.
c. 5 indicates the amount of times the program should loop.
2. At the end of the lines to be repeated enter Next X.
a. This line instructs the computer to go to the next number in the sequence.
b. E.g. If the value of X is 1 the next number is 2.
DO WHILE LOOP
Do While Loops repeat as long as a condition is true.
To write do while loops you need to do 4 things.
1. You need to initialize the variable. P=1
2. State at the beginning of the loop the conditions to be met/true. DO WHILE P < 5
3. Increment the variable used. P = P + 1
4. End the loop by stating END WHILE.
P=1
DO WHILE P < 5
PRINT "Enter the length of a room"
INPUT L
PRINT "Enter the width of same room"
INPUT W
Area = L * W
PRINT "The area of the room is: "; area
P=P+1
END WHILE
REPEAT UNTIL
Will continue to repeat a line of code as long as a condition is not true (false).
To write a repeat until you initialize the variable just as in the Do While loop. The difference is that the
variable will need to be initialized with a different value to best represent the loop.
P=9
P=P+1
DO UNTIL P > 5
The incrementing process may also be changed so that the value of P becomes more than 5 so the loop
can end.
It should also be noted that a ‘Repeat Until’ is called a posttest loop the construct for assessing the loop
condition comes at the end of the loop.
Accumulating and Incrementing [Loops]
1. Incrementing= is a programming technique that allows a programmer to change the value of a
variable by adding the same number every time construct loops. (Counting/Counter)
2. Accumulating= is another technique where we add numbers, however the numbers usually vary.
Problem:
We will write a construct to add the grades of students in a class. We will use loops, both Incrementing
and Accumulating techniques.
DIM stu AS STRING
DIM grd AS INTEGER
DIM inc AS INTEGER
DIM acc AS INTEGER
Begin
inc = 0
acc=0
DO WHILE inc < 6
PRINT "Enter your names students:”
INPUT stu
PRINT "Enter your grade student: "
INPUT grd
inc = inc + 1 {example of incrementing/counting}
acc = acc + grd {example of accumulating, adding values}
PRINT "Incrementing = "; inc
PRINT "Accumulating = "; acc
End While
End.
The algorithm above is written to accept the names of 6 students, their grades and display the total grades
for all the students as well as the number of students.
If and Else If Statements (Combined with Loops)
You’re opening a variety store for sale of various items. You decide to have customers ranked on 3
tiers; pearls, rubies and diamonds. Based on their ranking they will receive discounts at 5%, 10% and
15% respectively. (Written 2 ways as an example).
Example 1 Example 2
START BEGIN
DIM stat AS STRING DIM stat AS STRING
FOR x = 1 TO 3 FOR x = 1 TO 3
PRINT "Enter the status of the customer" PRINT "Enter the discount qualified for"
INPUT stat INPUT disc
PRINT "Enter the price of the item" PRINT "Enter the price of the item"
INPUT price INPUT price
IF stat = "pearl" THEN IF disc = 0.05 THEN
disc = price * 0.05 PRINT "You're a pearl member"
sprice = price - disc disc = price * 0.05
PRINT "The price you'll pay is: ", sprice sprice = price - disc
ELSE IF stat = "rubies" THEN PRINT "The price you'll pay is: ", sprice
disc = price * 0.10 ELSE IF disc = 0.10 THEN
sprice = price - disc PRINT "You're a ruby member"
PRINT "The price you'll pay is: ", sprice disc = price * 0.10
ELSE IF stat = "diamond" THEN sprice = price - disc
disc = price * 0.15 PRINT "The price you'll pay is: ", sprice
sprice = price - disc ELSE IF disc = 0.15 THEN
PRINT "You pay: ", sprice PRINT "You're a forever member"
END IF disc = price * 0.15
END IF sprice = price - disc
END IF PRINT "You pay: ", sprice
NEXT x END IF
STOP END IF
END IF
NEXT x
END
IF statement example with explanations:
IF, THEN, ELSE, ELSE IF, END IF
This series of programming code is used to have the computer evaluate 1 or more criteria and based on
the result (yes, or no) perform a series of calculations or operations.
Write an algorithm to test if it rains, if it is so the user is to take an umbrella otherwise no
umbrella is needed.
PRINT "INSTRUCTIONS: Please respond using yes, no or maybe" to display a message
instructing the user how to respond to the algorithm.
FOR x = 1 TO 4 to have the next lines repeat
PRINT "You user, is it raining? " display the message in quotation marks for the user.
INPUT rain to accept the response keyed in from the user
IF rain = "yes" THEN to test if the user responded ‘yes’
PRINT "Please take your umbrella to school" stating what should be done based on ‘yes’
ELSE IF rain = "maybe" THEN to test if the user said ‘maybe’
PRINT "Still take your umbrella in case" what should be done based on ‘maybe’
ELSE IF rain = "no" THEN test if the user said ‘no’
PRINT "Do not take your umbrella" the execution based on the ‘no’ response.
ELSE this does not test but states what should be done if the other test responses are not met-
last option
PRINT "Please follow instructions you moron" this is what the computer would execute because
of the other tests not being true.
END IF
END IF
END IF we have 3 ‘end if’ statements because of the 3 ‘if’ statements above. Note there is none
for else.
NEXT x to end the ‘FOR’ loop.
FLOW CHART SYMBOLS & OPERATIONS
Start/stop, beginning/end [Oval] Start, stop, begin
end
Input/output [Parallelogram]
=, Let, input,
print, read, write,
display
Process or calculation [Rectangle]
+-/*
Decision/comparative/relational [Rhombus]
= ><
<>
Connector [Circle]
Important Programming Terms:
1. Source Codes are the written executable instructions of a programmer. These usually obey the
rules of a specific programming language and is the initiation point for a computer to begin
solving a problem or execute a task and software development.
2. Translators are programs such as compilers and interpreters that convert source codes to
machine code for the computer to execute the instructions.
3. Interpreter is a program that converts source code to machine code for execution but does so
one line at a time.
4. Compilers are programs that convert source code to object code for the computer to execute
the instructions all at once, not line by line as interpreters do.
5. Object Code is created by an interpreter or compiler to translate to executable code such as
machine code.
6. Identifiers are variables. Used to identify memory allocations used for storing data as string,
char, integer, real and Boolean.
7. Debug is a systematic method of checking through a program for errors, usually in syntax errors;
may also include logic errors.
8. Logic errors are flaws in a program that produces incorrect results. The program would still run
or execute, however the results would be incorrect.
9. Syntax errors are mistakes in a program that could prevent it from running/executing. These
are flaws that indicate breaches in the programming rules or language/grammar.
10. Trace Table is a method of going through a program or algorithm using the variables/identifiers
so as to ascertain the accuracy of the results the program will generate.
11. Run Time Error these are problems in a program that may not be syntax errors or even logic
errors but mistakes that affect the operation of a program, usually terminations or errors in
program flow, selection etc.
12. Flow Charts is a method of expressing solutions to solving a problem by using specific symbols
to indicate how data flows through program.
13. Pseudo Code is a method of expressing solutions or chronological steps to problem solving by
using English like statements.
14. Algorithm is a method or process of stating how to execute a task using variables and
mathematical operators also in chronological order.
15. Iteration is a loop. It’s the process having a specific section of your program repeated based
specific conditions or a specific number of times.
16. If is a selection statement. It’s a Boolean operator; the program is required to examine for a
condition if the condition is TRUE a process is executed. If the condition is FALSE another
process is executed.
17. Pre-Test speaks to specific loops such as ‘Do While’ which evaluates a condition prior to carrying
out execution of on the statements to be repeated.
18. ‘Const’ = Constant is a memory allocation which will contain an unchangeable value from the
onset of the program.
19. Variables are memory areas whose value will change or may change throughout program
execution.
20. End is used to end subsections of an ‘IF’ statement or block, but the statement is not yet
completed.