Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Computer Program Engineering

Three types

Problem Solving Techniques (pseudo code, flowchart),


Qualities of Good Program,
Program Life Cycle
Problem Solving Techniques
Pseudocode
Algorithm
Flow charts
How People Solve Problems
A Problem exists when what we have (Data) is not the same
as what we want (information)
People create a solution (called an Algorithm) which
manipulates Data into Information
People do this quickly and often in a complex way
How Computers Solve Problems
Computers also use Algorithms to solve problems, and
change data into information
Computers can only perform one simple step at a time
Complex “Human” Algorithms must be broken down into
simple step-by-step instructions BEFORE they can be
translated into computer code
ALGORITHMS AND FLOWCHARTS

A typical programming task can be divided into two


phases:
Problem solving phase
produce an ordered sequence of steps that describe solution
of problem
this sequence of steps is called an algorithm
Implementation phase
implement the program in some programming language
Algorithms (source : wikipedia)
 In mathematics, computing, linguistics and related disciplines, an
algorithm is a procedure (a finite set of well-defined instructions) for
accomplishing some task which, given an initial state, will terminate in
a defined end-state.
 Algorithms are essential to the way computers process information,
because a computer program is essentially an algorithm that tells the
computer what specific steps to perform (in what specific order) in
order to carry out a specified task, such as calculating employees’
paychecks or printing students’ report cards.
Problem Solving
Problem Solving is the ability to understand what you have,
what you want, and creating a set of instructions to change
what you have into what you want
Good Problem Solving Skills are based on knowledge,
experience and logic
Good Programmers NEVER make assumptions
Expressing the Algorithms
A “Standard” way of describing an algorithm must exist if
we expect our solution to be understood by others easily
There are standards in programming:
PSEUDOCODE
FLOWCHARTS
PROGRAMMING LANGUAGE
Pseudo Code
“Pseudo” means “pretend” or “false”
Pseudo Code is pretend or false computer code; generic
English-like terms that are somewhat like computer code
Pseudo Code is not as standardized as flowcharts, and does
not facilitate the breaking down of problems as well as a
flowchart does
Pseudocode (wikipedia)
Pseudocode (derived from pseudo and code) is a compact
and informal high-level description of a
computer programming algorithm that uses the structural
conventions of programming languages, but omits detailed
subroutines, variable declarations or language-specific
syntax. The programming language is augmented with
natural language descriptions of the details, where
convenient.
Flowcharts
A Flowchart is a Visual Representation of an algorithm
A Flowchart uses easy-to-understand symbols to represent
actions on data and the flow of data
Flowcharts aid in breaking down a problem into simple
steps
Flowchart Symbols
Name Symbol Use in Flowchart

Basic
Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example
Example 1: Write an algorithm to determine a student’s
final grade and indicate whether it is passing or failing. The
final grade is calculated as the average of four marks.
Pseudocode
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Algorithm
Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
Exercise
Write an algorithm and draw a flowchart to convert the
length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with
30
Print length in cm (LCM)
Example 2
Algorithm Flowchart
Step 1: Input Lft
Step 2: Lcm  Lft x 30 START

Step 3: Print Lcm Input


Lft

Lcm  Lft x 30

Print
Lcm

STOP
Qualities of Good Program

Main goal in creating programs is and should always


be to satisfy the requirements of your clients
1) It should be free from bugs.
2) It must run in an accurate and efficient manner.
3) The program's interface must be accessible and user-
friendly.
4) It should be easy to maintain and doesn't control
system resources.
5) The source code is well-organized and optimized for
the best performance.
The Program Life Cycle.

1. Phases of the life cycle.


2. Example of the Design Phase.
3. Implementing the algorithm.
Phases of the program life-cycle.
A. The Design Phase.
Creation of an algorithm.
Analogy: drawing up a blueprint for a house.
B. The Implementation Phase.
Creation of a program.
Analogy: building a house.
C. The Maintenance Phase.
Use and modification of a program.
Analogy: adding an extension, repair of house.
The Design Phase.
1. Receive the problem specification.
2. Understand / analyze the problem.
3. Create an algorithm.
4. Test and debug the algorithm
Why?
Where may errors arise?
1. Failure to understand the problem.
May solve a different problem instead!
2. Incorrect algorithm.
Either the solution fails to be an algorithm (fails one or
more of four criteria) or it fails to give the correct answer
in all cases.
How to avoid errors in design.
We can reduce the number and severity of errors
by:
1. Taking time to fully understand the problem.
2. Ensuring that we have a true algorithm and that it
works for both “normal” and “unusual” data—
meaning?
Importance of Design Phase.
Studies show that time spent in the design phase is
always a good investment.
Analogy:
which is most cost-effective and causes least problems
---
preventative care or treatment of illness?
The Implementation Phase.
After the design phase:
1. Code the program: translate the algorithm into a
programming language.
A program = an algorithm coded in a
programming language.
2. Compile the program.
3. Test and debug the program. Again?
Where may errors arise?
1. Bad translation. The C# source code does not say
the same thing as the algorithm. (Monty Python
Danish-English phrase book: “Your hovercraft is full
of eels”!)
2. Syntax errors. The code has errors in grammar,
spelling or punctuation.
How to avoid errors in implementation.
1. Master the programming language.In this way,
you will know what C# statement corresponds to a
statement of English.
2. Learn what the compiler’s syntax error messages
mean.
This will enable you to edit the code until it is free of
syntax errors.
The Maintenance Phase.
1. Use the program.
2. Discover and fix errors missed in testing.
Danger: fixing one problem may introduce others.
3. Enhance the program by adding additional
capabilities.
Example of Design Phase.
1. Receive problem.
Determine the change due to a customer. Assume that
the sales tax may vary.
2. Understand the problem.
Analyze the problem into the 3 main activities of a
program:
(1) INPUT (2) PROCESS (3) OUTPUT.
Example of Design Phase (cont.).
3. Create algorithm.
Figure out the instructions required under the 3
headings. These can be written as pseudocode
(English imperatives which look like a program) e.g.
Input Price,
Calculate Change,
Display Change.
Example of Design Phase (cont.).
I. INPUT.
1. Input Amount Tendered.
2. Input Price.
3. Input Sales Tax.
How is this ambiguous?
How can we make it unambiguous?
Example of Design Phase (cont.).
II. PROCESS.
1. Calculate TaxAmount =
 SalesTaxRate * Price.
2. Calculate TotalPrice =
 Price + TaxAmount.
3. Calculate Change =
 AmountTendered - TotalPrice
Example of Design Phase (cont.).
III. OUTPUT.
1. “Echo print” the user’s input.
2. Display SalesTaxAmount
3. Display TotalPrice.
4. Display Change.
Test and debug the algorithm.
How?
“Play computer.” Try some data values. Calculate
what the change should be, then follow the instructions
of the algorithm exactly as a computer would (i.e.
blindly), and see if you get the same result.
Also, consider usual and unusual data.
Test and debug the algorithm (cont).
You may think that our algorithm is obviously correct.
But, can it handle unusual data?
No, if the AmountTendered is less than the TotalPrice
it will give negative change instead of recording an
error!
A validation check and loop would be needed to fix
this.
Implementing an algorithm.
The algorithm is just an idea in our head / on paper. It
has no power to make a computer do anything.
So: how can we make the computer execute the
algorithm?
We must translate the algorithm into a programming
language.
Implementing an algorithm (cont.)
Why? Why is English unsuitable?
It is too vague. E.g “several,” “a few,” “many.”
It is ambiguous. E.g. “glasses,” “bat.”
It can express actions which a computer cannot
perform e.g. “vote your conscience.”
Implementing an algorithm (cont.)
Thus we need a programming language.
Definition: A programming language is a formal
(symbolic) language which is:
(1) precise;
(2) unambiguous;
(3) restricted to operations a CPU can perform.
Implementing an algorithm (cont.)
But even the programming language is not in the
CPU’s native tongue.
The CPU only “understands” binary. It cannot directly
obey C#.
Therefore, it can implement our program only if it is
translated into binary form.
This is accomplished by the compiler.
Implementing an algorithm (cont.)
Thus programming requires 2 stages of translation:
(1) the programmer translates the algorithm into a
programming language;
(2) the compiler translates the program (source code)
into binary (object code).
Implementing an algorithm (cont.)
The compiler, like God’s law, is totally unforgiving. It
will only produce object code if there are NO syntax
errors. Since only the object code can be run, a
program cannot run unless all syntax errors are
removed.
Fortunately, the editor covers a multitude of sins!

You might also like