You are on page 1of 16

Chapter 2: Algorithm Representation

and Data Structure

1 Tadele M.
Program design
 This is the actual development of a program’s processing logic.
 The program’s processing logic is called its algorithm.
 An algorithm is a sequence of instructions which, if followed,
produces a solution to the given problem.
 Generally, an algorithm is a finite set of well-defined instructions
for accomplishing some task which, given an initial state, will
terminate in a corresponding recognizable end-state.
 The algorithm should be precise and unambiguous, simple,
correct, and efficient.
 An algorithm is written using special rules and statements

2
Program design…
Program design involves three tasks:
(i) grouping the program activity into modules
(ii) develop a solution algorithm for each module.
(iii) test the solution algorithm

Examples of an algorithm:
 Input: Read student name and marks obtained.
 Processing: Calculate total marks and average marks.
 Output: Write student name, total marks, average marks

3
Program design…
 The first task that involves grouping tasks into modules
focuses on what must be done (requirements) where the
Top-bottom design is used.
 This involves breaking down the program into smaller,
manageable components represented graphically on a
hierarchy chart; the top most showing the main module
referred as the main routine, which is then subdivided into
smaller sections also referred to as sub-routines.
 Each module is represented by a rectangle labeled by its
name.

4
Drop-down chart

Process exams

Read input Compute marks Generate output

Read student Calculate Determine Rank the


marks record Calculate average grade student
Total marks Generate Generate Student
marks exam analysis report form
report

5
Design Details
 Once the essential logic of the program has been determined
through the use hierarchy charts, you can now work on the
details.
 The two ways to show program details are
 writing the details using pseudocode or
 drawing the details using flowcharts.

6
Pseudo-code
 A Pseudo-code is a verbal shorthand method that closely
resembles a programming language, but does not have to
follow a rigid syntax structure.
 It can also be defined as a tool for designing a program in a
narrative form using human language statements to describe
the logic and processing flow.
 Example

START
READ student name, mark1, mark2, mark3, mark4
Totalmarks = mark1 + mark2 + mark3 + mark4
Avgmark = Totalmarks / 4
PRINT student name, totalmarks, avgmarks
STOP

7
Pseudo-code commonly used
keywords
 Several keywords are often used to indicate common
input, output, and processing operations.
 Input: READ, OBTAIN, GET
Output: PRINT, DISPLAY, SHOW, writeline
Processing: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP

8
Program flow chart
 A program flowchart is a diagrammatic representation of
a program’s processing logic.
 It uses standard symbols called ANSI symbols (called after the
American National Standards Institute that developed them)
and short statements that describe various activities.
 A flowchart shows how your program works before you
begin actually coding it.

9
Program flowchart symbols

10
AN EXAMPLE OF A FLOW CHART

ENTER TWO
NUMBERS;A,B

CALCULATE SUM=A+B
AVG = SUM/2

AVG >=85? No
GRADE = “A-

Yes
GRADE =“A”

PRINT
SUM,AVERAGE
AND GRADE
END
11
Flowchart (input-output)

12
Lamp flowchart

13
Sample Pseudocode to Flowchart
Conversion
 Find Area of a Square
Pseudocode Flowchart
START
READ len
area = len*len
PRINT area
STOP

14
What skills do we need to be a programmer?
 For someone to be a programmer, in addition to basic skills in
computer, needs to have the following major skills:
 Programming Language Skill: knowing one or more
programming language to talk to the computer and instruct the
machine to perform a task.
 Problem Solving Skill: skills on how to solve real world problem
and represent the solution in understandable format.
 Algorithm Development: skill of coming up with sequence of
simple and human understandable set of instructions showing the
step of solving the problem. Those set of steps should not be
dependent on any programming language or machine.
15
Worksheet 1

For each of the problems below, develop a flow chart


1) Receive a number and determine whether it is odd or even.
2) Receive 3 numbers and display them in ascending order from
smallest to largest
3) Add the numbers from 1 to 100 and display the sum
4) Take an integer from the user and display the factorial of that
number

16

You might also like