Sie sind auf Seite 1von 22

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

Algorithm
A concept that pervades all areas of computer science.

Algorithm is a process that a computer could carry out to


complete a well defined task within finite time and
resources.

The objective of computer science is to solve problems


by developing, analyzing, and implementing
algorithmic solutions.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


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

Al-Khwarizmi Principle
• All complex problems can be broken into simpler
sub-problems.
• Solve a complex problem by breaking it down into
smaller sub-problems and then solve them (in a
specified order), one at a time.
• When all the steps are solved, the original problem
itself has also been solved.
• This process is called Algorithm.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


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

Computer Programming
• Computer is a powerful tool
• It is not intelligent!
• In order to use computer to solve our problems, we
must tell it what we want done and the order in which
we want it done.
• These instructions are called computer program.
• This process is called computer programming.
• The person giving these instructions is called a
computer programmer.

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
1. Call the workshop
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 student’s 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


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

Conversion from Fahrenheit to Celsius

• Output
– Temperature in Celsius (C)

• Inputs
– Temperature in Fahrenheit (F)

• Process

5
C  (F  32)
9

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
2. Add them together
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.

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.
Flow
Input and Processing of
Output Steps Steps data

Decision Terminator
Connectors

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


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

Begin
Flow chart for
Converting
Get temp. in ‘F’
Fahrenheit
into Celsius
Calculate 5
C  ( F  32)
9

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


Get three scores
calculating
Add them together average of
three scores
Divide the result by three

Print the average

Yes No
More students? 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

INPUT
Step 1: Begin the calculations A, B
BEGIN Adder
Step 2: Input two values A and B Input A and B
C = A + B
Add A to B
Step 3: Add the values PRINT C
and store in C
END Adder
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

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

Your First C++ Program


1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << "Welcome ";
8 std::cout << "to C++!\n";
9
10 return 0; // indicate that program ended
successfully
11}

Welcome to C++!

Unless new line '\n' is specified, the text


continues on the same line.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Das könnte Ihnen auch gefallen