0% found this document useful (0 votes)
7 views12 pages

algorithmandflow-chart (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views12 pages

algorithmandflow-chart (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

ALGORITHM AND FLOW CHART

• Introduction

• Problem Solving

• Algorithm

• Examples of Algorithm

• Properties of an Algorithm

• Flow Chart

• Flow Chart Symbols

• Some Flowchart Examples

• Advantages of Flowcharts

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
INTRODUCTION
Intelligence is one of the fundamental features that distinguishes humans from other living species
on the planet. Basic intelligence includes problem solving and developing solutions to deal with the
various situations that arise in daily life. Someone goes to the bank to withdraw money. He/she
decides to withdraw the entire money from his/her account after learning the balance in his/her
account, but he/she must leave a minimum balance in his/her account. Making a decision about how
much money to withdraw from the account is an example of basic intelligence. During the process of
solving any problem, one tries to find the necessary steps to be taken in a sequence. In this
Unit you will develop your understanding about problem solving and approaches.

PROBLEM SOLVING
Can you recall a day in your life when you did not solve a problem? Of obviously, the answer to this
question is no. We are obligated to solve challenges in our lives. In our daily activities, such as
purchasing something from a general store and paying for it, depositing school fees, or withdrawing
money from a bank account. All of these tasks require some sort of problem solving. Problem
solving can be defined as any activity performed by a human or computer to achieve a specific goal.
Let us look at some more examples to make it apparent.
Example1: If you are watching a news channel on your TV and you want to
change it to a sports channel, you need to do something i.e. move to that
channel by pressing that channel number on your remote. This is a kind of problem solving.
Example 2: One Monday morning, a student is ready to go to school but yet he/she has not picked
up those books and copies which are required as per timetable. So here picking up books and
copies as per timetable is a kind of problem solving.
Example 3: If someone asks to you, what is time now? So seeing time in your watch and telling him
is also a kind of problem solving.

Example 4: 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 an individual have to give for picnic is also a
kind of problem solving.
Now, broadly we can say that problem is a kind of barrier to achieve something and problem solving is
a process to get that barrier removed by performing some sequence of activities

Here it is necessary to mention that all the problems in the world cannot be solved. There are some
problems which have no solution and these problems are called Open Problems.
If you can solve a given problem then you can also write an algorithm for it. In next section we will
learn what is an algorithm.

ALGORITHM
Algorithm can be 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 point”. There may
be more than one way to solve a problem, so there may be more than one algorithm for a problem.
Now, if we take definition of algorithm as: “A sequence of activities to be processed for getting
desired output from a given input.” Then we can say that:
Getting specified output is essential after algorithm is executed.
One will get output only if algorithm stops after finite time.
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
and what is/are expected output after running the algorithm. Now let us take some exercises to
develop an algorithm for some simple problems: While writing algorithms we will use following
Prepared By: Programming 1
Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
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.

Example 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:
Step1: Read\input the Radius r of the Circle Step
2: Area PI*r*r // calculation of area
Step3: Print Area

Problem2: 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: Step1: Start


Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum num1+num2 // calculation of sum Step5: Print Sum
Step6: End

Problem 3: Convert temperature Fahrenheit to Celsius Inputs to the algorithm:


Temperature in Fahrenheit Expected output:
Temperature in Celsius

Algorithm:

Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 5/9*(F32)
Step 4: Print Temperature in Celsius: C Step5: End

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
Type of Algorithms
The algorithm and flowchart, classification to the three types of control structures. They are:
• Sequence
• Branching (Selection)
• 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.
The branch 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. This structure is also known as the
selection structure.

Problem1: write algorithm to find the greater number between two numbers Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A Step4: if B greater than A then C=B Step5: Print C
Step6: End
−𝑥, 𝑥 < 0
{
Problem2: write algorithm to find the result of equation:𝑓(𝑥) = 𝑥, 𝑥 ≥ 0

Step1: Start
Step2: Read/input x
Step3: If X Less than zero then F=-X
Step4: if X greater than or equal zero then F=X Step5: Print F
Step6: End

Problem3: A algorithm to find the largest value of any three numbers.

Step1: Start
Step2: Read/input A,B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End

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.) In the flowcharts, a back
arrow hints the presence of a loop. A trip around the loop is known as iteration. 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, a common mistake made by inexperienced
programmers. The loop is also known as the repetition structure.

Examples:
Problem1: An algorithm to calculate even numbers between 0 and 99

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
Start
I←0
Write I in standard output
I ← I+2
If (I <=98) then go to line 3
End

Problem2: Design an algorithm which gets a natural value, n, as its input and calculates odd numbers
equal or less than n. Then write them in the standard output:
Start
Read n
I←1
Write I
I←I+2
If ( I <= n) then go to line 4
End
Problem3: Design an algorithm which generates even numbers between 1000 and 2000 and then
prints them in the standard output. It should also print total sum:
Start
I ← 1000 and S ← 0
Write I
S←S+I
I←I+2
If (I <= 2000) then go to line 3 else go to line 7
Write S
End
Problem4: 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
Start
Read n
I ← 2 and S ← 0
S= S + 1/I
I←I+2
If (I <= n) then go to line 4 else write S in standard output
End
Combining the use of these control structures, for example, a loop within a loop (nested loops), a
branch within another branch (nested if), a branch within a loop, a loop within a branch,
and so forth, is not uncommon. Complex algorithms may have more complicated logic structure
and deep level of nesting, in which case it is best to demarcate parts of the algorithm as separate
smaller modules. Beginners must train themselves to be proficient in using and combining control
structures appropriately, and go through the trouble of tracing through the algorithm before they
convert it into code.

Properties of algorithm
Donald Ervin Knuth has given a list of five properties for an algorithm, these properties are:
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.
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

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
for each activity in the algorithm.

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.

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. If some result is from the
intermediate stage of the operation then it is known as intermediate result and r esult obtained at
the end of algorithm is known as end result. The output is expected value/quantities always have a
specified relation to the inputs

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.

FLOWCHART

A flowchart is a diagram that visually presents the flow of data through processing systems. This
means that by looking at a flow chart, one can learn about the processes done and the order in
which they occur in a system. Algorithms are simply a series of procedures for solving issues. As a
result, a flow chart can be used to represent an algorithm. A flowchart will describe the processes
that must be performed (and in what order) to solve a particular problem. A flow chart can be
thought of as a blueprint for a design you created to solve a problem.

For example suppose you are going for a picnic with your friends then you plan for the activities you
will do there. If you have a plan of activities then you know clearly when you will do what activity.
Similarly when you have a problem to solve using computer or in other word you need to write a
computer program for a problem then it will be good to draw a flowchart prior to writing a computer
program. Flowchart is drawn according to defined rules.

Flowchart Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language Programs:
Terminal, Process, input/output, Decision, Connector and 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.

Symbol Name Function


Indicates any type of internal
Process operation inside the Processor or
Memory

Used for any Input / Output (I/O)


input/output operation. Indicates that the
computer is to obtain data
or output results

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
Used to ask a question that can be
Decision answered in a binary format
(Yes/No, True/False)

Allows the flowchart to be drawn


Connector without intersecting lines or without
a reverse
flow.

Used to invoke a subroutine or an


Predefined Process Interrupt program.

Indicates the starting or ending of


Terminal the program, process, or interrupt
program

Flow Lines Shows direction of flow.

General Rules for flowcharting


All boxes of the flowchart are connected with Arrows. (Not lines)

Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit
point for all flowchart symbols is on the bottom except for the Decision symbol.
The Decision symbol has two exit points; these can be on the sides or the bottom and one side.
Generally a flowchart will flow from top to bottom. However, an upward flow can be shown as long
as it does not exceed 3 symbols.
Connectors are used to connect breaks in the flowchart. Examples are:

1. From one page to another page.

2. From the bottom of the page to the top of the same page.

3. An upward flow of more then 3 symbols

Subroutines and Interrupt programs have their own and independent flowcharts.
All flow charts start with a Terminal or Predefined Process (for interrupt programs or subroutines)
symbol.
All flowcharts end with a terminal or a contentious loop.

Flowcharting uses symbols that have been in use for a number of years to represent the type of
operations and/or processes being performed. The standardized format provides a common
method for people to visualize problems together in the same manner. The use of standardized
symbols makes the flow charts easier to interpret, however, standardizing symbols is not as
important as the sequence of activities that make up the process.

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
1.4.4 Some examples of Flowcharts
Now, we will discuss some examples on flowcharting. These examples will help in proper
understanding of flowcharting technique. This will help you in program development process in
next unit of this block.
Problem1: Find the area of a circle of radius r.

Problem 2: Convert temperature Fahrenheit to Celsius.

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
Problem3: Flowchart for an algorithm which gets two numbers and prints sum of their value

Problem5: Algorithm for find the greater number between two numbers.

Start

Read A,B

True False
If A>B

Print A Print B

END

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
Problem6: Flowchart for the problem of printing even numbers between 9 and 100:

Problem7: Flowchart for the problem of printing odd numbers less than a given number. It should
also calculate their sum and count.

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
problem8: Flowchart for the calculate the average from 25 exam scores.

Start

Sum=0, C=0

Enter Exam
Scores, S

Sum=Sum+S

C=C+1

No
Is C=
25?

Yes

Av=Sum/25

Print Av

END

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty
1.4.5 Advantages of using Flowcharts
As we discussed 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:
Communication: A Flowchart can be used as a better way of communication of the logic of a system
and steps involve in the solution, to all concerned particularly to the client of system.
Effective analysis: A flowchart of a problem can be used for effective analysis of the problem.
Documentation of Program/System: Program flowcharts are a vital part of a good program
documentation. Program document is used for various purposes like knowing the components in the
program, complexity of the program etc.
Efficient Program Maintenance: Once a program is developed and becomes operational it needs
time to time maintenance. With help of flowchart maintenance become easier.
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.

Prepared By: Programming 1


Armie Q. Valencia AY 2023-2024
E-CoAST Faculty

You might also like