0% found this document useful (0 votes)
18 views31 pages

Problem Solving with Algorithms and Flowcharts

Uploaded by

yonatanethiopia1
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)
18 views31 pages

Problem Solving with Algorithms and Flowcharts

Uploaded by

yonatanethiopia1
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

Problem Solving Using

Computers

Introduction to Software Engineering and Computing


.

1
Content
• Introduction
• Problem solving using computers
• Defining problem in context of software solution
• Basics of Algorithms
• Control Structure
• Flowchart
• Advantages of Flowchart
• Exercise Questions

2
Introduction
• Intelligence is one of the key characteristics which differentiate a
human being from other living creatures on the earth.
• Basic intelligence covers day to day problem solving and making
strategies to handle different situations
Example: One person goes Bank to withdraw money
❖ After knowing the balance in his account
❖ He/she decides to withdraw the entire amount from his account
but he/she has to leave minimum balance in his account
❖ Deciding about how much amount he/she may withdraw from the
account is one of the examples of the basic intelligence.

3
Problem Solving Using Computer (Cont.)
• Whatever activity a human being or machine do for achieving a
specified objective comes under problem solving.
Example 1: If someone asks to you, what time is it, now?
• So seeing time in your watch and telling him is also a kind of problem
solving.
Example 2: Some students in a class plan to go on picnic and decide to
share the expenses among them.
• So calculating total expenses and the amount of money an individual
have to give for picnic is also a kind of problem solving.

4
Problem Solving Using Computer (Cont.)
• Now, broadly we can say that problem is a kind of barrier to achieve
something.
• Problem solving is a process to get that barrier removed by performing
some sequence of activities.
• There are some problems that has not yet been solved and for which a
solution is not yet known and these problems are called Open
Problems.
• If you can solve a given problem then you can also write an algorithm
for it.
So, what is an algorithm?

5
Algorithm
• Defined as: “A sequence of activities to be processed for getting
desired output from a given input.”
• Webopedia defines an algorithm as: “A formula or set of steps for
solving a particular problem. ”
• To be an algorithm, a set of rules must be unambiguous and have a
clear stopping points.
• There may be more than one way to solve a problem, so there may be
more than one algorithm for a problem.
From the above definition of algorithm we can say that:

6
Algorithm (Cont.)
i. Getting specified output is essential after algorithm is executed.
ii. One will get output only if algorithm stops after finite time.
iii. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous.
Before writing an algorithm for a problem, one should find out
• What is/are the inputs to the algorithm
• What is/are expected output after running the algorithm
While writing algorithms we will use the following symbol for different
operations:
‘+’ for Addition, ‘-’ for Subtraction, ‘*’ for Multiplication, ‘/’ for Division and
‘← ’ for assignment.
• For example : A = X*3 means, A will have a value of X*3.

7
Properties of Algorithm (Cont.)
Donald Ervin Knuth has given a list of five properties for an algorithm, these
properties are:
i. Finiteness:
• An algorithm must always terminate after a finite number of steps
• It means after every step one reach closer to solution of the problem
and after a finite number of steps algorithm reaches to an end point.
ii. Definiteness:
• Each step of an algorithm must be precisely defined.
• It is done by well thought actions to be performed at each step of the
algorithm.
• Also the actions are defined unambiguously for each activity in the algorithm.
8
Properties of Algorithm (Cont.)
iii. Input:
• Any operation you perform need some beginning value/quantities
associated with different activities in the operation.
• So the value/quantities are given to the algorithm before it begins.
iv. Output:
• One always expects output/result (expected value/quantities) in terms
of output from an algorithm
• The result may be obtained at different stages of the algorithm
• The output is expected value/quantities always have a specified relation
to the inputs

9
Properties of Algorithm (Cont.)
V. Effectiveness:
• Algorithms to be developed/written using basic operations.
• Actually operations should be basic, so that even they can in principle be
done exactly and in a finite amount of time by a person, by using paper
and pencil only.

10
Algorithm (Cont.)
Examples of Algorithm
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm:
✔ Radius r of the Circle
Expected output:
✔ Area of the Circle
Algorithm:
Step 1: Start
Step 2: Read\input the Radius r of the Circle
Step 3: Area <- PI*r*r // calculation of area
Step 4: Print Area
Step 5: End
11
Algorithm (Cont.)
Problem 2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
✔ First num1.
✔ Second num2.
Expected output:
✔ Sum of the two numbers
Algorithm:
Step 1: Start
Step 2: Read\input the first num1.
Step 3: Read\input the second num2.
Step 4: Sum <- num1+num2 // calculation of sum
Step 5: Print Sum
Step 6: End
12
Algorithm (Cont.)
Problem 3: Converting Temperature Fahrenheit to Celsius
Inputs to the algorithm:
✔ Temperature in Fahrenheit
Expected output:
✔ Temperature in Celsius
Algorithm:
Step 1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C ←5/9*(F-32).
Step 4: Print Temperature in Celsius: C
Step 5: End
13
Types of Control Structure
The algorithm and flowchart, classified into the three types of control
structures. They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
• These three control structures are sufficient for all purposes.
The sequence:
• Is exemplified by sequence of statements place one after the other –
the one above or before another gets executed first.
• In flowcharts, sequence of statements is usually contained in the
rectangular process box
14
Types of Control Structure (Cont.)
The Branching (Selection):
• Refers to a binary decision based on some condition.
• If the condition is true, one of the two branches is explored; if the
condition is false, the other alternative is taken.
• This is usually represented by the ‘if-then’ construct in pseudo-codes
and programs.
• In flowcharts, this is represented by the diamond-shaped decision
box
• The branching is also known as the selection structure
Example: The branching used to ask a question that can be answered
in a binary format (Yes/No, True/False)

15
Examples of Branching (Selection)
Problem 1: write algorithm to find the greater number between two
numbers
Algorithm:
Step 1: Start
Step 2: Read/input A and B
Step 3: If A greater than B then C=A
Step 4: if B greater than A then C=B
Step 5: Print C
Step 6: End

16
Examples of Branching (Selection)
Problem 2: Write algorithm to find the result of equation:
Algorithm:
Step 1: Start
Step 2: Read/input x
Step 3: If X Less than zero then F=-X
Step 4: if X greater than or equal zero then F=X
Step 5: Print F
Step 6: End

17
Examples of Branching (Selection)
Problem 3: A algorithm to find the largest value of any three numbers.
Algorithm:
Step 1: Start
Step 2: Read/input A,B and C
Step 3: If (A>=B) and (A>=C) then Max=A
Step 4: If (B>=A) and (B>=C) then Max=B
Step 5: If (C>=A) and (C>=B) then Max=C
Step 6: Print Max
Step 7: End

18
The Loop (Repetition)
• The loop allows a statement or a sequence of statements to be
repeatedly executed based on some loop condition
• It is represented by the ‘while’ and ‘for’ constructs in most programming
languages, for unbounded loops and bounded loops respectively.
• Unbounded loops refer to those whose number of iterations depends on
the eventuality that the termination condition is satisfied
• Bounded loops refer to those whose number of iterations is known
before-hand
• The loop is also known as the repetition structure
• You must ensure that the condition for the termination of the looping
must be satisfied after some finite number of iterations, otherwise it
ends up as an infinite loop
19
Examples of Loop
Problem 1: An algorithm Printing Numbers From 1 to 5
Algorithm:
Step 1. Start
Step 2. I ← 1
Step 3. Write I in standard output
Step 4. I ← I+1
Step 5. If (I <=5) then go to STEP 3
Step 6. End

20
Examples of Loop
Problem 2: An algorithm to display even numbers from 0 and 99
Algorithm:
Step 1. Start
Step 2. I ← 0
Step 3. Write I in standard output
Step 4. I ← I+2
Step 5. If (I <=98) then go to STEP 3
Step 6. End

21
Examples of Loop cont.
Problem 3: Design an algorithm with a natural number, n, as its input which
calculates the following formula and writes the result in the standard output:
S = ½ + ¼ + … +1/n
Algorithm:
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4
else write S in standard output
7. End

22
Flowchart
• The flowchart is a diagram which visually presents the flow of data
through processing systems.
• This means by seeing a flow chart one can know the operations
performed and the sequence of these operations in a system.
• Algorithms are nothing but sequence of steps for solving problems.
• So a flow chart can be used for representing an algorithm.
• You can see a flow chart as a blueprint of a design you have made
for solving a problem.

23
Flowchart Cont.
• There are 6 basic symbols commonly used in flowcharting of assembly
language Programs:
i. Terminal
ii. Process
iii. Input/output
iv. Decision
v. Connector
vi. Predefined Process
• This is not a complete list of all the possible flowcharting symbols, it is
the ones used most often in the structure of Assembly language
programming.
24
Examples of Branching (Selection)

25
Some Examples of Flowcharts
Problem 1: Find the area of a circle of Problem 2: Convert temperature
radius r. Fahrenheit to Celsius.

26
Some Examples of Flowcharts
Problem 3: Algorithm which gets two Problem 4: Algorithm for find the
numbers and prints sum of their value greater number between two numbers
Start

Read
A,B

True False
If
A>B

Print Print
A B

END

27
Some Examples of Flowcharts
Problem 5: Flowchart for the problem of printing even numbers between 9
and 100:

28
Advantages of Using Flowchart
• Flow chart is used for representing algorithm in pictorial form.
• This pictorial representation of a solution/system is having many
advantages.
These advantages are as follows:
1) Communication: A Flowchart can be used as a better way of
communication of the logic of a system and steps involve in the solution.
2) Effective analysis: A flowchart of a problem can be used for effective
analysis of the problem.

29
Advantages of Using Flowchart (Cont.)
3) Documentation of Program/System: Program document is used for
various purposes like knowing the components in the program,
complexity of the program etc.
4) Efficient Program Maintenance: Once a program is developed and
becomes operational it needs time to time maintenance.
• With help of flowchart maintenance become easier.
5) Coding of the Program: Any design of solution of a problem is finally
converted into computer program.
• Writing code referring the flowchart of the solution become easy.

30
Exercise
1. What is an algorithm?
2. Explain the need of an algorithm?
3. Find factorial of N? Write an algorithm.
4. Draw a flowchart to find the largest of three numbers x, y and z.
5. Draw a flowchart to find the sum of first 100 natural numbers.

31

You might also like