0% found this document useful (0 votes)
13 views26 pages

Lecture 04

The document discusses problem solving techniques and algorithm design, introducing pseudo-code and flowcharts as tools to design algorithms by describing the steps in a simple, easy-to-understand way and showing how control structures operate through connected geometric shapes and flow lines; it provides examples of multiplying numbers in pseudo-code and a cylinder volume calculation activity, and demonstrates typical flowchart symbols and how to represent conditions, functions, comments, and multi-page flowcharts using connectors.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Lecture 04

The document discusses problem solving techniques and algorithm design, introducing pseudo-code and flowcharts as tools to design algorithms by describing the steps in a simple, easy-to-understand way and showing how control structures operate through connected geometric shapes and flow lines; it provides examples of multiplying numbers in pseudo-code and a cylinder volume calculation activity, and demonstrates typical flowchart symbols and how to represent conditions, functions, comments, and multi-page flowcharts using connectors.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 4

PROBLEM SOLVING TECHNIQUES

1
Lecture 4 Objectives

• Use the basic problem-solving techniques.

• Develop algorithms using pseudo-code and


flowchart.
Lecture 4 Contents

4.1 Introduction
4.2 Algorithm Design
4.3 Summary
Lecture 4 4.1 Introduction

• Before writing a program to solve a particular problem, we


need to understand the problem and design its solution.

• Hence, we need an algorithm that can facilitate us in


writing a computer program.

• Note: A problem can have more than one solutions i.e.,


algorithms

____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 4 4.1 Introduction
System
Requirements

(Software Development Life Cycle)

•A step-by-step program plan is created during the design stage.


•Two tools in the problem-solving techniques will be discussed:
Algorithm Design
- Pseudo-code Notations for planning
- Flowchart an algorithm

____________________________________________________________________________________________________________________________________
_
I Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
2 http://www.tiem.utk.edu/~gross/c++man/
Lecture 4 Contents

4.1 Introduction
4.2 Algorithm Design
4.3 Summary
Lecture 4 4.2 Algorithm Design
Introduction

• The solution to any problem involves a series of actions to


be performed in a specific order.

• Algorithm is a procedure for solving a problem in terms of


• the actions to be executed, and
• the order in which these actions are to be executed.

____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 4 4.2 Algorithm Design
Pseudo-code

Pseudo-code is written using informal language that helps us


describe and design algorithms.
• It describes an algorithm in a simple, easy-to-understand
language.
• The steps in a pseudo-code are written for humans and hence
cannot be executed on computers.
• A carefully prepared pseudo-code may be easily converted to a
corresponding High level language program e.g., C/C++,
Python etc.

____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 4 4.2 Algorithm Design
Pseudo-code Example
Example 4.1: Write pseudo-code for multiplying two numbers.

1. Get the first number, A


2. Get the second number, B
3. Calculate the result, C= A * B
4. Display the result, C
Lecture 4 4.2 Algorithm Design

Activity 4.1: Write pseudo-code to calculate and display the


volume of a cylinder.
Lecture 4 4.2 Algorithm Design

Solution 4.1:

1. Get the height of cylinder, h


2. Get the radius of base, r
3. Calculate the result, V = π * h * r2
4. Display the result, V
Lecture 4 4.2 Algorithm Design
Flowchart

• An alternative tool to describe algorithms

• A flowchart consists of geometrical shapes, connected by


lines.

• Flowcharts clearly shows how the control structures operate.

Geometrical shapes – Flow lines –


represent the type of show the order in which
statements in the the statements of an
algorithm algorithm are executed.

____________________________________________________________________________________________________________________________________
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 4 4.2 Algorithm Design

Table: Typical flowchart symbols

Terminal: Indicates the start and end of a flowchart. Single flow line. Only one “Start”
and “Stop/End” terminal for each program. The end terminal for function/subroutine
must use “Return” instead of “Stop/End”.
Process: Used whenever data is being manipulated using a calculation. One flow line
enters, and one flowline exits.
Input/Output: Used whenever data is entered (input) or displayed (output). One flow
line enters, and one flow line exits.
Decision: Used to represent operations (conditions) in which there are two possible
selections. One flow line enters and two flow lines, labelled as “Yes” and “No”, exit.
Function / Subroutine: Used to identify an operation in a separate flowchart segment
(module). One flow line enters, and one flow line exits.
On-page Connector: Used to connect remote flowchart portion on the same page.
Either one flow line enters, or one flow line exits.
Off-page Connector: Used to connect remote flowchart portion on different pages.
Either one flow line enters one flow line exits.
Comment: Used to add descriptions or clarification.

Flow line: Used to indicate the direction of flow of control.


Lecture 4 4.2 Algorithm Design

Start Terminal:
Example: Make a Start
Flowchart starts here
flowchart for
multiplying two Get first number,
A Input: Enter values
numbers. Get second for A and B
number, B

Calculate Resut
C=A*B Process

Display the
Result C Output

End Stop Terminal:


Flowchart ends here
Lecture 4 4.2 Algorithm Design

Start
Example: Flowchart

Use of Comments Read N, N = The number of students


M M = The number of subjects
/Description
Yes

No

Stop
Lecture 4 4.2 Algorithm Design

Example: Flowchart – Start

Use the connectors 1


2

for the same page

1
Stop

2
Lecture 4 4.2 Algorithm Design

Example: Flowchart - Use the connectors for different pages.


Page 1 Page 2
Start

1 2

Yes 1

No End

2
Lecture 4 4.2 Algorithm Design
The details (how the function works)
Example: we put in another flowchart.
This also known as Function-Definition
Using Function-Call
Page 1 Start terminal for a Page 2
Start Function is different.
Do not use “Start”
AVRG ( result,n1, n2,n3)
Note:
Module = Function = Read
Subroutine . n1, n2 , n3

sum = n1+ n2+n3

Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart

result = sum/3

At this part, Print


we only know what result
we want to do. But we
don’t know how to do it Return

This part also known as


Function-Call End
End terminal
must be “Return”
Lecture 4 4.2 Algorithm Design

Activity 4.2: Draw the flowchart design to calculate and display


the volume of a cylinder.
Lecture 4 4.2 Algorithm Design

Solution 4.2:
Start

Get first number,


Get the Aheight, h
Get
Get thesecond
radius, r
number, B

Calculate Resut
Calculate volume,
C=A*B2
V= π*h*r

Display the
Display the
Result C
result V

End
Lecture 4 4.2 Algorithm Design

Activity 4.3: Convert the temperature entered by the user in the


unit of Fahrenheit to the unit of Celsius. The
conversion formula is as follows:

Celsius = (Fahrenheit-32)*5/9)

a) Identify the input, process and output.


b) Write the pseudo-code design of the problem.
c) Draw the flowchart design of the problem.

____________________________________________________________________________________________________________________________________
_
Lecture 4 4.2 Algorithm Design

Solution 4.3:

start

1. Get temp. in
Get, F
Fahrenheit, F
2. Calculate the
Calculate
temp. in Celsius, C=(F-32)*5/9

C = (F-32)*5/9
3. Display the Display C

result, C
end
Lecture 4 4.2 Algorithm Design

Solution 4.3:

start

1. Get temp. in
Get, F
Fahrenheit, F
2. Calculate the
Calculate
temp. in Celsius, C=(F-32)*5/9

C = (F-32)*5/9
3. Display the Display C

result, C
end
Lecture 4 4.2 Algorithm Design

Solution 4.3:

start

1. Get temp. in
Get F
Fahrenheit, F
2. Calculate the
Calculate
temp. in Celsius, C=(F-32)*5/9

C = (F-32)*5/9
3. Display the Display C

result, C
end
Lecture 4 Contents

4.1 Introduction
4.2 Algorithm Design
4.3 Summary
Lecture 4 4.4 Summary

• Before writing a program to solve a particular problem, we


must have a thorough understanding of the problem and
carefully plan an approach to solve it.

• A solution to any problem involves executing a series of


actions in specific order called algorithm.

• Algorithms can be designed using two tools:


• Pseudo-code or Flowchart

____________________________________________________________________________________________________________________________________
_
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education

You might also like