Problem Solving and Program Design
Problem Solving and Program Design
If you research the topic of Problem Solving by using either the internet or any reputable text
book, most likely you will see the number of procedures range from probably four (4) to seven
(7) steps. For example, you may come across the following seven (7) main steps when trying to
solve a problem:
The above seven steps above may be merged into four steps as follows:
* Examine related problems, and determine if the same technique can be applied.
* Examine a simpler or special case of the problem to gain insight into the solution of the
original problem.
* Make a table.
* Work backward.
* Work the problem by hand (with a calculator or a using a trace table) for a specific set of data.
Step 4. Test the solution with a variety of data to validate that you are solution correct to the
problem given.
Ensure that you include error messages for data that is not expected on input or in processing
that may cause the program to crash
Here is another set of steps on the phases. Can you place the following three steps into the four
step above?
Problem-Solving Phase
Maintenance Phase
Input
Entering data from an external source into the system can be achieved either:
1. Manually using keyboards which are often used to enter data into a system, or
2. Automatically, where data can be entered into the system using hardware such as OMR, microphone,
or sensors.
Processing
Search
Sort, and
Output
After the data is processed into information, it can be presented in different ways for the user, such as:
Sound (such as the sounds from ATMs when you are making a transaction or the sound from the cash
register)
Light, (flashing lights on an ambulance; a flashing light to take your money or card from the ATM)
Output essentially deals with any data that is leaving the system.
Storage
If data doesnt need to be output immediately after processing it may be saved for later use on a storage
medium such as a hard drive.
Also, stored data may be accessed to be processed with new data to output more information.
Note the unique qualities of storage mediums such as CDs and Disk Drives. They can be input devices
when data is being stored on them, but they become output devices when data is required from these
devices.
3. Distinguish between variables and constants
The ability to manipulate variables is one of the most powerful features of a programming
language.
Let's explain variables and constants by first understanding something that you are hopefully
familiar with.
Suppose each week Janus and Kikiyo go to a particular room in a building for a meeting. The
room is the same location with the same furniture and may even have a name, such as Room
LT4.
Each week when they go to the meeting, Janus may or may not sit in the same seat since Kikiyo
or someone else who got there earlier may have taken that seat, so Janus sits somewhere else.
SO! The room LT4 is a constant. It is located in the same place for the duration of your meeting.
While, where Janus, Kikiyo and the other sit varies. Their seats are variable each week when
they go to the meeting. Sometimes Janus or Kikiyo or someone else may miss the meeting.
Janus, Kikiyo and the other people have names but their seat location in the weekly meeting is
what varies.
a variable is an area of storage whose value can change during processing. In our
example the variable is the seat location
a constant can contain a value that never changes during the processing of the program.
In our example the constant is the room.
Variables
Programmers generally choose names for the variables that are meaningful. That is, the
programmer uses a variable that indicates what the variable is used for. It is good programming
practice to use meaningful variable names that suggest what they represent, since this makes the
program more readable and easier to understand.
For example:
SeatNo := 'Janus';
HoursWorked := 35;
the variable to be assigned a value must appear on the left of the assignment operator
the assignment operator is :=
a legal expression (value to be assigned) must appear on the right
Constants
A constant represents a value that does not change for the duration of the program. So, for
example, in Pascal , a value of 25% or .25 will be assigned to the variable Tax. However, Tax
cannot change to any other percentage while the program is being run.
Note the use of the equal sign for the Constant Declaration.
Problem 1
Write an algorithm and draw a flowchart that reads two values, determines the largest value and
prints the largest value with an identifying message.
ALGORITHM
MAX = VALUE1
else
MAX = VALUE2
endif
Step 3: Print The largest value is, MAX
Flowchart
Problem 2
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Algorithm
Pseudocode:
Step 1: Input Len_FT
Step 2: Len_CM Step 3: Print Len_CM
Flowchart
Problem 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate
its area.
Algorithm
Pseudocode
Step 1: Input W, L
Step 2: Area = W * L
Step 3: Print Area
Flowchart
Using our steps in problem solving, a typical programming task can be divided into two phases:
This phase is where you produce an algorithm ( an ordered sequence of steps) that
describe solution of problem
2. Implementation phase
This phase is where you implement the program in some programming language, in this
case, Pascal.
An algorithm as a set or list of instructions for carrying out some process step by step until it
reaches the end of the algorithm.
A recipe in a cookbook is an excellent example of an algorithm. So, the recipe includes the
ingredients and the method of cooking until you (hopefully) end up with a nice dish!
Serves 4 people
Examples of Algorithms
1. Pseudocode - English like words that specify the sequence of steps in an algorithm
2. Flowcharts - a graphical tool using standard symbols to show the sequence of steps in an
algorithm
So whether you use pseudocode or flowcharts to expand your algorithm, here is why algorithm
are so useful:
Every algorithm must have an end, so there must be a set of steps followed in that
algortihm to reach the end. For example, you follow the steps of the recipe to reach the
end which is your dish that is ready to eat!
Every instruction should therefore be clear, with short instructions and easily understood
The instructions should be in a sequence from top to bottom. Using the example recipe
above, you follow the instructions from step 1 to step 5.
To illustrate how we use algorithms (pseudocode and flowcharts) to a programming problem, let
us use an example:
Example 1:
Write an algorithm to determine a students final grade and indicate whether the student
has passed or failed. The final grade is calculated as the average of four marks.
Pseudocode
List the tasks that need to be performed. You can also use an IPO chart to help you:
if average is below 50
Print FAIL
else
Print PASS
Detailed Pseudocode
Input M1,M2,M3,M4
GRADE = (M1+M2+M3+M4)/4
Then
Print FAIL
Else
Print PASS
Endif
Flowcharts
Flowcharts show how data flows from the input of data through the computer to final output or
storage (note the IPOS diagram in a previous section).
A Flowchart
shows logic of an algorithm emphasizes individual steps and flow of data from one action to the
next
Flowcharting guidelines
Representation of algorithms
Design an algorithm and the corresponding flowchart for adding the test scores as given below:
Pseudocode
1. Start
2. Total = 0
6. Add to Total
8. Add to Total
The example 1, algorithms (pseudocode and flowchart) illustrate the steps for solving the
problem of adding six testscores. Where one testscore is added to Total at a time.
Note that the flowchart should have a Start step at the beginning and at least one stop step at the
end.
Since we want the sum of six testscores, then we should have a variable to store the
resulting sum after each addition. In this example, the container is called Total and we make sure
that Total should start with a zero value by step 2.
LOOPS
For loop
Since we know that there are six testscores, we can can use a FOR loop. Here is the
flowchart that uses a FOR loop:
Pseudocode
1. Start
2. Total = 0
5. Get a testscore
8. Stop
Flowchart
While Loop
Here is another way, we need to add a last number to the list of numbers given. This
number should be special, so unique so that each time we get a testscore, we test it to see
if we have reached that special, unique number. Common numbers are -1, or 99. In this
example, since we are using testscores, it may not be wise to use 99 as the unique number
to stop the program.
Pseudocode
1. Start
2. Total = 0
3. Get a testscore
8. Stop
Flowchart
In this flowchart we use a While loop. While we have not entered that special unique
testscore of -1, the user is prompted to enter another testscore.
TRUTH TABLES
When using the AND Operator, both expressions must be TRUE for the compound
expression to be true.
So if a is 4 and b is 2
Then
(a <= 2) AND (b = 2)
Evaluates to
(4 <= 2) AND (2 = 2)
FALSE
Boolean Operators: OR
When using the OR Operator, either one expression or both expressions can be TRUE for
the compound expression to be true.
If (expression1) OR (expression2)
THEN
Output (One or both expressions are true);
So if a is 4 and b is 2
Then
(a <= 2) OR (b = 2)
(4 <= 2) OR (2 = 2)
FALSE OR TRUE
TRUE
If NOT (expression)
THEN writeln(This expression is FALSE);
So if a is 4 and b is 2
Then
NOT (TRUE)
to FALSE.
1st NOT
2nd AND, *, /
3rd OR, +, -
Truth Tables
Negation
~ p
F T
T F
AND
p AND q p AND q
T T T
T F F
F T F
F F F
Conditional Statement
Relational Operators compare two values. Let us assume that the values A and B contain
two numbers. We can compare the two values as follows:
Making decisions
Conditional Statements are used to help with decisions that can be made based on the
results of comparing two values. For example, the expression A>B is a logical
expression. It describes a condition we want to test.
So:
if A>B is true (if A is greater than B) we take one action, such as print the value of A
if A>B is false (if A is not greater than B) we take another action, such as print the value
of B
The algorithm for conditional statements are shown below:
If condition then
true alternative
else
false alternative
endif
If A>B then
print A
else
print B
endif
IF-THEN Statement
IF-THEN-ELSE Statement
IF (Boolean expression) THEN
STATEMENT1
ELSE
STATEMENT2;
Write an algorithm and draw a flowchart that reads two values, determines the largest
value and prints the largest value with an identifying message.
ALGORITHM
MAX = VALUE1
else
MAX = VALUE2
endif
Step 3: Print The largest value is, MAX
Flowchart
Problem 2
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Algorithm
Flowchart
Problem 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and
calculate its area.
Algorithm
Pseudocode
Step 1: Input W, L
Step 2: Area = W * L
Step 3: Print Area
Flowchart
Trace tables
A trace table is a useful technique used to test an algorithm with data to make sure that program
does what it is supposed to do and no logical errors occur
The best way to use a trace table is through manual tracing or a dry run of the program
statements.
Checking the sequence of steps of the program algorithm or code before you execute the
code
locate errors in your algorithm or program code
Let us use some program code below along with a trace table for all the variables and outputs.
Program Code
y=3
For x =1to4
y=y+x
Loop
Print (y)
Trace Table
x y output
1 3
2 4
3 6
4 9
4 13 13
Many times a question on trace tables will ask you to complete the table which will include
headings and some values to use as input with sometimes a few values as output.
Note the variables in the headings will also be found in the fragment of code given
Fill in all values that are input to the variables in the columns
A problem may seem impossible to solve because at first glance it can be very complex.
Complex problems can be solved using top-down design, also called stepwise refinement, where
Sub-dividing the problem into parts helps clarify what needs to be done.
At each step of refinement, the new sub-tasks become less complicated and, therefore, easier to
program.
Parts of the solution may turn out to be used in other parts of the program
Subdividing the problem into sub-parts allows more than one person to work on the solution.
Problem:
Unfortunately, stating the problem isn't enough to actually make any bakes, we need to break the task down:
1. Organise Kitchen
2. Make bakes
3. Serve
1. Organise Kitchen
1. Clean surfaces
2. Get out mixing bowl, whisk, spoon, sieve
3. Get out flour, sugar, salt, eggs, milk, butter
4. Put on apron
2. Make bakes
1. Sift salt and flour into bowl
2. Add sugar
3. Mix dry ingredients
4. cream butter into dry ingredients until the mixture resembles breadcrumbs
5. Add water (milk is optional)
6. Mix well. Add flour so that the batter is not too sticky
7. Cook
8. Serve
And each of these tasks can be broken down further, let us take a look at the Cook:
Fry Bakes
So starting at a single point, making bakes, we have broken down the task into it individual parts.