Sie sind auf Seite 1von 36

Algorithm - Pseudo Code Flowchart

Introduction
Algorithms are the building blocks of any computer
program
They are a set of rules to be followed in order to
solve problems using computers
What is an Algorithm?
An algorithm is a part of the plan for the computer
program;
An algorithm is an effective procedure for solving a
problem in a finite number of steps.

Programs = Algorithm + Data


It is effective, which means that an answer is found
and it has a finite number of steps and also
guaranteed to terminate

Characteristics of an
Algorithm
Well-ordered: The steps are in a clear order
Unambiguous: The operations described are
understood by a computing agent without further
simplification
Effectively computable: The computing agent can
actually carry out the operation

Method for Developing an Algorithm


1. Define the problem: State the problem you are
trying to solve in clear and concise terms.
2. List the inputs (information needed to solve the
problem) and the outputs (what the algorithm
will produce as a result)
3. Describe the steps needed to convert or
manipulate the inputs to produce the outputs.
Start at a high level first, and keep refining the
steps until they are effectively computable
operations.

What is Algorithm?

How to express an
Algorithm?

Pseudocode (or Program Design


Language)
Consists of natural language-like statements that precisely
describe the steps of an algorithm or program.
Statements describe actions.
Focuses on the logic of the algorithm or program
Avoids language-specific elements .
Written at a level so that the desired programming code can
be generated almost automatically from each statement.
Steps are numbered. Subordinate numbers and/or
indentation are used for dependent statements in selection
and repetition structures.

Pseudocode Language Constructs


Computation/Assignment
Compute var1 as the sum of x and y
Assign expression to var2
Increment counter1

Input/Output
Input: Get var1, var2,
Output: Display var1, var2,

Selection
Single-Selection IF
1. IF condition THEN (IF condition is true, then do
subordinate statement 1, etc. If condition is false, then
skip statements)
1.1 statement 1
1.2 etc.

Pseudocode Language
Constructs
Double-Selection IF
2. IF condition THEN (IF condition is true, then do
subordinate statement 1, etc. If condition is false, then
skip statements and execute statements under ELSE)
2.1 statement 1
2.2 etc.

3. ELSE (else if condition is not true, then do subordinate


statement 2, etc.)
3.1 statement 2
3.2 statement 3

4. SWITCH expression TO

4.1
4.2
4.3
4.4

case 1: action1
case 2: action2
etc.
default: actionx

Pseudocode Language
Constructs
Repetition
5. WHILE condition (while condition is true, then do
subordinate statements)
5.1 statement 1
5.2 etc.

DO WHILE structure (like WHILE, but tests


condition at the end of the loop. Thus, statements
in the structure will always be executed at least
once.)
6. DO
6.1 statement 1
6.2 etc.

7. WHILE condition

FOR structure (a specialized version of WHILE for


repeating execution of statements a specific
number of times)
8. FOR bounds on repetition

Pseudocode Example
Express an algorithm to get two numbers from the
user (dividend and divisor), testing to make sure that
the divisor number is not zero, and displaying their
quotient using pseudocode
1.
2.
3.
4.

Declare variables: dividend, divisor, quotient


Prompt user to enter dividend and divisor
Get dividend and divisor
IF divisor is equal to zero, THEN
4.1. DO
4.1.1. Display error message, divisor must be non-zero
4.1.2. Prompt user to enter divisor
4.1.3. Get divisor
4.2. WHILE divisor is equal to zero

5. ENDIF
6. Display dividend and divisor
7. Calculate quotient as dividend/divisor

Rules for Pseudocode

Example Pseudocode

Start
Read A, B
Start - Terminal
Calculate C = A*B Read A, B Input
Display C
Calculate C = A*B Action
Stop
Display C - Output
Stop - Terminal

User Friendly Pseudo code

Start
Use variables A,B and C
Display write two numbers
Read A, B
Calculate C = A*B
Display multiplication of numbers ,
C
Stop

Flowcharts
A graphical tool that diagrammatically depicts the
steps and structure of an algorithm or program
General rules for flowcharts
All symbols of the flowchart are connected by flow lines
(note arrows, not lines)
Flowlines enter the top of the symbol and exit out the
bottom, except for the Decision symbol, which can have
flow lines exiting from the bottom or the sides
Flowcharts are drawn so flow generally goes from top to
bottom
The beginning and the end of the flowchart is indicated
using the Terminal symbol

Flowchart Symbols

Example Flowchart

Structured Programming
Any computer program can be written with just three
structures: sequences, decisions, and loops.
Sequences: one command is executed after previous one.
Decisions (selections): statement(s) is (are) executed if
certain condition gives TRUE or FALSE value.
Loops (repetition): statement(s) is (are) executed repeatedly
until certain condition gives TRUE or FALSE value.

There are three selection structures in C:


1. IF
2. IF ELSE
3. SWITCH

There are three repetition structures in C:


1. WHILE
2. DO WHILE
3. FOR

Sequence

Decisions (selections)

Three selection structure in C programming:


1.
2.
3.

If
If else
Switch

Decisions (selections)

Nested If
Simply, if structure in if structure
Example - 3: Both final and average grades must be
greater than or equals to 35 for curve calculation in
BEU.
if (final >= 35) then
{
if (average >= 35) then
execute curve calculation commands
endif
}
else
Print FF grade
endif

Pseudo code and Flowchart for


a Decision Structure

Loops (repetition)

Three repetition structure in C programming:


1.
2.
3.

While
Do while
For

Loops (repetition)

Pseudocode and Flowchart for


a Loop

Example
Write an algorithm to determine a students
grade and indicate whether he is successful or
not.
The average grade is calculated as the average of
internal marks and endsem marks.
Student will be successful if his average grade is
greater or equals to 50.

Pseudo Code
Start
Use variables internal, endsem and average
Input internal and endsem
Calculate the average by summing internal and
ensem and dividing by 2
if average is below 50
Print FAIL
else
Print SUCCESS
Stop

Algorithm
Step 1: Input internal and endsem
Step 2: average = (internal + endsem)/2
Step 3: if (average < 50) then
Print FAIL
else
Print SUCCESS
endif

Flowchart

Pseudocode vs Flowcharts
Pseudocode
Artificial and Informal
language
Helps programmers to
plan an algorithm
Similar to everyday
English
Not an actual
programming
language

Flowcharts
A graphical way of
writing pseudo code
Rounded rectangle
terminal
Parallelogram input /
output
Rectangle actions
Diamonds decision /
conditional
Circles connector

END

Performing a Task on The


Computer
The first step in writing instructions to carry out a
task is to determine what the output should be
that is, exactly what the task should produce.
The second step is to identify the data, or input,
necessary to obtain the output.
The last step is to determine how to process the
input to obtain the desired output, that is, to
determine what formulas or ways of doing things
can be used to obtain the output.

Program Development Cycle

Analyze: Define the problem.


Be sure you understand what the program should do, that
is, what the output should be. Have a clear idea of what
data (or input) are given and the relationship between the
input and the desired output.

Design: Plan the solution to the problem.


Find a logical sequence of precise steps that solve the
problem. Such a sequence of steps is called an algorithm.
Every detail, including obvious steps, should appear in the
algorithm.

Program Development Cycle


Choose the interface: Select the objects.
Determine how the input will be obtained and how the output will
be displayed. Then create appropriate commands to allow the user
to control the program.

Code: Translate the algorithm into a programming


language.
Coding is the technical word for writing the program.

Test and debug: Locate and remove any errors in the


program.
Testing is the process of finding errors in a program, and debugging
is the process of correcting errors that are found. (An error in a
program is called a bug.)

Program Development Cycle


Complete the documentation: Organize all the material
that describes the program.

Documentation is intended to allow another person, or the


programmer at a later date, to understand the program. Internal
documentation consists of statements in the program that are
not executed, but point out the purposes of various parts of the
program.
Documentation might also consist of a detailed description of
what the program does and how to use the program (for instance,
what type of input is expected).
For commercial programs, documentation includes an instruction
manual.
Other types of documentation are the flowchart and pseudo code.

Das könnte Ihnen auch gefallen