Class 3 - Algorithms
Class 3 - Algorithms
Class 3 - Algorithms
There are no well-defined standards for writing algorithms. Rather, it is problem and resource-
dependent. Algorithms are never written to support a particular programming code.
As we know that all programming languages share basic code constructs like loops (do, for,
while), flow-control (if-else), etc. These common constructs can be used to write an algorithm.
We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing
is a process and is executed after the problem domain is well-defined. That is, we should know
the problem domain, for which we are designing a solution.
Example
Step 1 − START
Step 4 – c= a+b
Step 5 − print c
Step 6 – STOP
3.5 Pseudo-code
EXAMPLES:
1. Begin
3. Output("input number1:")
4. INPUT s1
5. Output("input number2:")
6. INPUT s2
7. Sum=s1+s2
8. OUTPUT sum
9. End
Example 2: Write the Pseudocode to Check Whether the Number is Odd or Even.
1. Start
3. Read input of a X
4. If X mod = 0
5. Print “X is Even"
6. Else
7. Print “X is Odd"
8. End
3.6 Flowchart
Flowcharts are nothing but the graphical representation of the data or the algorithm for a better
understanding of the code visually. It displays step-by-step solutions to a problem, algorithm,
or process. It is a pictorial way of representing steps that are preferred by most beginner-level
programmers to understand algorithms of computer science, thus it contributes to
troubleshooting the issues in the algorithm. A flowchart is a picture of boxes that indicates the
process flow in a sequential manner. Since a flowchart is a pictorial representation of a process
or algorithm, it’s easy to interpret and understand the process. To draw a flowchart, certain
rules need to be followed which are followed by all professionals to draw a flowchart and is
widely accepted all over the countries.
It is most importantly used when programmers make projects. As a flowchart is a basic step to
make the design of projects pictorially, it is preferred by many.
When the flowcharts of a process are drawn, the programmer understands the non-useful parts
of the process. So, flowcharts are used to separate useful logic from unwanted parts.
Since the rules and procedures of drawing a flowchart are universal, a flowchart serves as a
communication channel to the people who are working on the same project for better
understanding.
Optimizing a process becomes easier with flowcharts. The efficiency of the code is improved
with the flowchart drawing.
A. Process flowchart: This type of flowchart shows all the activities that are involved in
making a product. It basically provides a pathway to analyze the product to be built. A
process flowchart is most commonly used in process engineering to illustrate the
relation between the major as well as minor components present in the product. It is
used in business product modeling to help understand employees about the project
requirements and gain some insight into the project.
B. Data flowchart: As the name suggests, the data flowchart is used to analyze the data,
specifically it helps in analyzing the structural details related to the project. Using this
flowchart, one can easily understand the data inflow and outflow from the system. It is
most commonly used to manage data or to analyze information to and fro from the
system.
C. Business Process Modeling Diagram: Using this flowchart or diagram, one can
analytically represent the business process and help simplify the concepts needed to
understand business activities and the flow of information. This flowchart illustrates
the business process and models graphically which paves a way for process
improvement.
There are different types of boxes that are used to make flowcharts. All the different kinds of
boxes are connected to one another by arrow lines. Arrow lines is used to display the flow of
control. Let’s learn about each box in detail.
Terminal
This box is oval in shape which is used to indicate the start or end of the program. Every
flowchart diagram has an oval shape that depicts the start of an algorithm and another oval
shape that depicts the end of an algorithm. For example:
Data
This is a parallelogram-shaped box inside which the inputs or outputs are written. This
basically depicts the information that is entering the system or algorithm and the information
that is leaving the system or algorithm. For example: if the user wants to input a from the
user and display it, the flowchart for this would be:
Process
This is a rectangular box inside which a programmer writes the main course of action of the
algorithm or the main logic of the program. This is the crux of the flowchart as the main
processing codes is written inside this box. For example: if the programmer wants to add 1
to the input given by the user, he/she would make the following flowchart:
Decision
This is a rhombus-shaped box, control statements like if, or condition like a > 0, etc are
written inside this box. There are 2 paths from this one which is “yes” and the other one is
“no”. Like every decision has either yes or no as an option, similarly, this box to have these
as options. For example: if the user wants to add 1 to an even number and subtract 1 if the
number is odd, the flowchart would be:
Flow
This arrow line represents the flow of the algorithm or process. It represents the direction of
the process flow. in all the previous examples, we included arrows in every step to display
the flow of the program. arrow increases the readability of the program.
On-Page Reference
This circular figure is used to depict that the flowchart is in continuation with the further
steps. This figure comes into use when the space is less and the flowchart is long. Any
numerical symbol is present inside this circle and that same numerical symbol will be
depicted before the continuation to make the user understand the continuation. Below is a
simple example depicting the use of On-Page Reference
Example 1: Design a flowchart for the multiplication of two numbers and print it.
Example 2: Design a flowchart to Print the largest among two numbers entered by the user.