Вы находитесь на странице: 1из 20

# Lecture 06:Algo.

Flowcharts Pseudocode

## Logical Flow of a Program

algorithms, flowcharts, pseudocode

Zawar Hussain

## 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

## 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

## 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

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

## 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

## 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

Hard Problem

Easy Sub-problem

Hard Sub-problem

Easy Sub-problem

Easy Sub-problem

Easy Sub-problem

## Problem Solving Techniques

What is the unknown?
What is required?

What is given?

## What is the condition?

By what condition the unknown is linked to the data?

5
C
9(F
32)

Output

Inputs

Process

## 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

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

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.

## Flowcharts Most Common Symbols

Symbol

Name
Terminal
Flow-line

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

Process

manipulation.

Input/Output

and information.

Decision

## Represents a comparison, question,

or decision that determines
alternative paths to be followed.

Flow Charting

## Expresses the flow of processing in a

structured pictorial format.
Input and
Output Steps

Decision

Processing
Steps

Terminator

Flow
of
data

Connectors

C
5
(9
F
3
2
)

Begin

Get temp. in F

Calculate

Converting
Fahrenheit
into Celsius

Print C

Stop

calculating
average of
three scores

## Get three scores

Divide the result by three
Print the average

Yes

More students?

No

Stop

START

INPUT
A, B

and store in C

## Step 4: Display the result

Step 5: End the calculation

Input A and B
C = A + B
PRINT C

OUTPUT
C

END

Natural language

Flowchart

Pseudo-code

Questions?

## Нижнее меню

### Получите наши бесплатные приложения

Авторское право © 2021 Scribd Inc.