Anda di halaman 1dari 10

# Lecture 06:Algo.

## 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
– 3 test scores for each student
• output
– Average of 3 tests for each student
• Process
1. Get three scores
3. Divide by three to get the average
4. Print the average
5. Repeat step 1 to 4 for next student
6. 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 Function

## Terminal Represents the beginning or end of a

program.
Flow-line Represents the flow of logic.

manipulation.

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

Flowcharts – An Example
Find the solution of a quadratic equation
Ax2+Bx+C=0, given A, B and C.
A
START

INPUT X1 = (-B+R)/(2A)
A, B, C X2 = (-B-R)/(2A)

Calculate
PRINT
R = SQRT(B2-4AC) A, B, C, X1, X2

A
END

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

INPUT
Step 1: Begin the calculations A, B
Step 2: Input two values A and B Input A and B
C = A + B
Step 3: Add the values PRINT C
and store in C
Step 4: Display the result
OUTPUT
Step 5: End the calculation
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

Algorithm Representation
(Natural Languages)

## • English or some other natural language.

• Are not particularly good:
– too verbose
– unstructured
– too rich in interpretation (ambiguous)
– imprecise

## Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode CS 101: Introduction to Computing

Algorithm Representation
(Using Programming Language)
{
int I, m, Carry;
int a[100], b[100], c[100];
cin >> m;
for ( int j = 0 ; k <= m-1 ; j++ ) {
cin >> a[j];
cin >> b[j];
}
Carry = 0;
i = 0;
while ( i < m ) { …

## Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 06:Algo. Flowcharts Pseudocode CS 101: Introduction to Computing

Pseudo-code

•  Pseudo-code
• Computer scientists use pseudo-code to express algorithms:
– English like constructs (or other natural language), but
– modeled to look like statements in typical programming languages.

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