Sie sind auf Seite 1von 20

Lecture 06:Algo.

Flowcharts Pseudocode

CS 101: Introduction to Computing

Logical Flow of a Program


algorithms, flowcharts, pseudocode

Zawar Hussain

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Fundamental Building Blocks of Programs


THERE ARE TWO BASIC ASPECTS of programming:
data and instructions.

To work with data


you need to understand variables and types

To work with instructions


you need to understand control structures and subroutines.
You'll spend a large part of the course becoming familiar with these
concepts
Ref: http://math.hws.edu/javanotes/c1/s4.html

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Algorithms
All computing problems
can be solved by executing a series of actions in a specific order

Algorithm
A procedure determining the

Actions to be executed
Order in which these actions are to be executed
Program control
Specifies the order in which statements are to executed

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Algorithm to Start a Car


1.Insert the key
2.Make sure car is in neutral gear
3.Press the gas pedal/ (Accelerator)
4.Turn the key to the start position
5.If the engine starts in 6 seconds
1.Release the key to the ignition position

6.Else if the engine does not start in 6


seconds
1.Release the key and gas pedal
2.Wait for 10 seconds , and repeat the steps 3 6, but no more
than 5 times

7. If the
car does not start
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Pseudocode
Pseudocode

Artificial, informal language used to develop algorithms


Similar to everyday English
Not actually executed on computers
Allows us to think out a program before writing the code for it
Easy to convert into a corresponding C++ program
Consists only of executable statements
Example:
Begin
Input: Marks
If students Marks to 60
Print "Passed
Else
Print Failed
End
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Pseudo-code Primitives
Three basic kind of operations:
Sequential
Computation ( Set )
Input/Output ( Get ... / Print ... )

Conditional
If Else
If

Iterative / looping
Repeat ...
While ...
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Control Structures
Sequential execution
Statements executed one after the other in the order written

Transfer of control
When the next statement executed is not the next one in sequence

Bohm and Jacopini: all programs written in terms of 3 control


structures
Sequence structure

Built into C++. Programs executed sequentially by


default.
Selection structures

C++ has three types - if, if/else, and switch


Repetition structures

C++ has three types - while, do/while, and for


Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Divide and Conquer


Hard Problem

Easy Sub-problem

Hard Sub-problem

Easy Sub-problem

Easy Sub-problem

Easy Sub-problem

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Problem Solving Techniques


What is the unknown?
What is required?

What are the data?


What is given?

What is the condition?


By what condition the unknown is linked to the data?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

5
C
9(F
32)

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Conversion from Fahrenheit to Celsius

Output

Inputs

Temperature in Celsius (C)

Temperature in Fahrenheit (F)

Process

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Calculate and print the average grade of 3


tests for the entire class

Input

output

3 test scores for each student


Average of 3 tests for each student

Process
1.
2.
3.
4.
5.
6.

Get three scores


Add them together
Divide by three to get the average
Print the average
Repeat step 1 to 4 for next student
Stop if there are no more students

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Flow Charts

A flowchart is a visual or graphical representation of an


algorithm.
The flowchart employs a series of blocks and arrows,
each of which represents a particular operation or step
in the algorithm.
The arrows represent the sequence in which the
operations are implemented.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Flowcharts Most Common Symbols


Symbol

Name
Terminal
Flow-line

Function
Represents the beginning or end of a
program.
Represents the flow of logic.

Process

Represents calculations or data


manipulation.

Input/Output

Represents inputs or outputs of data


and information.

Decision

Represents a comparison, question,


or decision that determines
alternative paths to be followed.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Flow Charting

Expresses the flow of processing in a


structured pictorial format.
Input and
Output Steps

Decision

Processing
Steps

Terminator

Flow
of
data

Connectors

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

C
5
(9
F
3
2
)

Lecture 06:Algo. Flowcharts Pseudocode

Begin

Get temp. in F

Calculate

CS 101: Introduction to Computing

Flow chart for


Converting
Fahrenheit
into Celsius

Print C

Stop

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Flow chart for


calculating
average of
three scores

Get three scores


Add them together
Divide the result by three
Print the average

Yes

More students?

No

Stop

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Comparison of Algorithm representations in Natural language, flowchart and Pseudo-code

START

Step 1: Begin the calculations

INPUT
A, B

Step 2: Input two values A and B


Step 3: Add the values

Add A to B
and store in C

Step 4: Display the result


Step 5: End the calculation

BEGIN Adder
Input A and B
C = A + B
PRINT C
END Adder

OUTPUT
C

END

Natural language

Flowchart

Pseudo-code

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Questions?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode

CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi