CSC 111
LECTURE 6
Program Development tools: Flow
charts and algorithms.
DR. ZUBAIR ADAM
LAGOS STATE UNIVERSITY, OJO LAGOS
1
Computers as a problem solving
tool
Problem Solving is the sequential process
of analyzing information related to a given
situation and generating appropriate
response options.
Computer science is all about solving
problems with computers (regardless of the
area of study).
Real-world or abstract world problems can
be tackled with computer science.
2
We need to have a standard systematic
Systematic approach to solving
problems
Understand the Problem
1. Understand the Problem
2. Formulate a Model Formulate a Model
3. Develop an Algorithm Develop an Algorithm
4. Write the Program Write the Program
5. Test the Program Test the Program
6. Evaluate the Solution Evaluate the Solution
3
A problem scenario
Consider a simple example of how the
input/process/output works on a simple
problem which requires calculating the
average grade for all students in a class.
1. Input: Get all the grades … perhaps by
typing them in via the keyboard or by
reading them from a USB flash drive or hard
disk.
2. Process: add them all up and compute the
average grade.
3. Output: Output the answer to either the
monitor, to the printer, to the USB flash 4
drive or hard disk …
Understand the Problem
The first step to solving any problem is to make sure that
you understand the problem that you are trying to solve.
One needs to know:
1. What input data/information is available?
2. What does it represent?
3. What format is it in?
4. Is anything missing?
5. Do I have everything that I need?
6. What output information am I trying to produce?
7. What do I want the result to look like text, a picture, a graph?
8. What am I going to have to compute? 5
Understand the Problem
In our example, we well understand that the input is a bunch of grades. But we need to
understand the format of the grades.
Each grade might be a number from 0 to 100 or it may be a letter grade from A+ to F. If it
is a number, the grade might be a whole integer like 73 or it may be a real number like
73.42.
We need to understand the format of the grades in order to solve the problem.
We also need to consider missing grades. What if we do not have the grade for every
student (e.g., some were away during the test) ? Do we want to be able to include that
person in our average (i.e., they received 0) or ignore them when computing the average ?
We also need to understand what the output should be. Again, there is a formatting issue.
Should we output a whole or real number or a letter grade ? Maybe we want to display a
pie chart with the average grade. It is our choice.
Finally, we should understand the kind of processing that needs to be performed on the
6
data. This leads to the next step
Formulate a model
Now we need to understand the processing part of the problem.
To achieve that we break down the problem into smaller problems that require
some kind of simple mathematical computations to process the data.
In our example, we are going to compute the average of the incoming
grades. So, we need to know the model (or formula) for computing the average
of a bunch of numbers. If there is no such “formula”, we need to develop one
Assuming that the input data is a bunch of integers or real numbers x1,x2,…,xn
representing a grade percentage, we can use the following computational
model: Average = (x1 + x2 + x3 + … + xn) / n where the result will be a
number from 0 to 100.
7
Develop an Algorithm
Now that we understand the problem and have formulated a model, it is
time to come up with a precise plan of what we want the computer to do.
An algorithm is a precise sequence of instructions for solving a problem.
To develop an algorithm, we need to represent the instructions in some way
that is understandable to a person who is trying to figure out the steps
involved. Two commonly used representations for an algorithm is by using
Pseudo code, or
Flow charts.
Pseudocode is a simple and concise sequence of English-like instructions
to solve a problem
Flowchart is simply a graphical representation of steps in sequential order
8
and is widely used in presenting the flow of algorithms.
Flowchart
ANSI/ISO Name Decription
Shape
Shows the process's order of operation. A line coming from
Flowline
one symbol and pointing at another. Arrowheads are added
(Arrowhead)
if the flow is not the standard top-to-bottom, left-to right.
Indicates the beginning and ending of a program or sub-
process. Represented as an oval or a rounded (fillet)
Terminal rectangle. They usually contain the word "Start" or "End",
or another phrase signaling the start or end of a process,
such as "submit inquiry" or "receive product".
Represents a set of operations that changes value, form, or
Process
location of data. Represented as a rectangle.
Shows a conditional operation that determines which one
of the two paths the program will take. The operation is
Decision
commonly a yes/no question or true/false test.
Represented as a diamond (rhombus).
Indicates the process of inputting and outputting data, as
Input/Output
9
in entering data or displaying results. Represented as a
rhomboid.
Flowchart
ANSI/ISO Name Decription
Shape
Indicating additional information about a step in the program.
Annotation
Represented as an open rectangle with a dashed or solid line
(Comment)
connecting it to the corresponding symbol in the flowchart.
Predefined Shows named process which is defined elsewhere.
Process Represented as a rectangle with double-struck vertical edges.
Pairs of labeled connectors replace long or confusing lines on
On-page
a flowchart page. Represented by a small circle with a letter
Connector
inside.
Off-page A labeled connector for use when the target is on another
Connector page. Represented as a home plate-shaped pentagon.
10
Pseudocode Vs Flowchart
Consider the following example (from Wikipedia) of solving the
problem of a broken lamp. To the right is an example of a flow
chart, while to the left is an example of pseudocode for solving
the same problem:
Pseudocode
1. IF lamp works, go to step 7.
2. Check if lamp is plugged in.
3. IF not plugged in, plug in lamp.
4. Check if bulb is burnt out.
5. IF blub is burnt, replace bulb.
6. IF lamp doesn’t work buy new lamp.
11
7. Quit ... problem is solved. Figure 1: Flowchart
Write the Program
Writing a program is often called "writing code" or “implementing an
algorithm”.
The code (or source code) is the program itself.
The code is written by using a programming language that is best suited
for the job.
A programming language is a vocabulary and set of grammatical rules
for instructing a computer or computing device to perform specific tasks.
Some examples of programming language are BASIC, C, C++, COBOL,
Java, FORTRAN, Ada, Python (etc). 12
Algorithm (Pseudocode) to Program
S/ Pseudocode Processing code (i.e., program)
N
1. set the sum of the grade values to 0. int sum int sum = 0;
= 0;
2. load all grades x1 … xn from file. byte[] x = loadBytes("numbers");
3. repeat n times{ for (int i=0; i <x.length; i++)
4. get grade xi sum = sum + x[i];
add xi to the sum
}
5. compute the average to be sum / n. int avg = sum / x.length
6. print the average. print(avg);
13
Test the Program
Once you have a program written that compiles, you need to make sure that it
solves the problem that it was intended to solve and that the solutions are correct.
Therefore the program is run in order to evaluate the compiled instructions.
After running the program, if all is well, then a correct output displayed.
It is possible however, that the program works correctly for some set of data input
but not for all.
If the output of the program is incorrect,
it is possible that the algorithm was not converted properly into a proper program.
It is also possible that the algorithm itself is flawed.
Or some instructions where written and performed out of sequence.
Whatever happened, such problems with your program are known as bugs. 14
Test the Program
Bugs are problems/errors with a program that cause it to stop working or
produce incorrect or undesirable results.
One should fix as many bugs in ones program as possible.
To find bugs effectively, the program must be evaluated with many test cases
(called a test suite).
It is also a good idea to have a third-party test ones program because they may
think up situations or input data that the writer may never have thought of.
The process of finding and fixing errors in your code is called debugging and it
is often a very time-consuming “chore” when it comes to being a programmer.
If one takes time to carefully follow problem solving steps 1 through 3, this
should greatly reduce the amount of bugs in ones programs and it should make
15
debugging much easier.
Assignment
Look up all abbreviations used in the slides.
Look up the features/rules of pseudocode &
flowchart
Look up Evaluation Phase
Draw a flowchart of the Pseudocode in Slide 13.
16