Class 3 - Algorithms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

3 Algorithm

The algorithm is a step-by-step procedure, which defines a set of instructions to be executed in


a certain order to get the desired output. An algorithm is the systematic logical approach which
is a well-defined, step-by-step procedure that allows a computer to solve a problem. The
Algorithm designed are language-independent, i.e., they are just plain instructions that can be
implemented in any language, and yet the output will be the same, as expected.

3.1 Characteristics of an Algorithm.


➢ Finiteness: an algorithm must terminate after a finite number of steps.
➢ Definiteness: the steps of the algorithm must be precisely defined or unambiguously
specified.
➢ Generality: an algorithm must be generic enough to solve all problems of a particular
class.
➢ Effectiveness: the operation of the algorithm must be basic enough to be put down on
pencil and paper. They should not be too complex to warrant writing another algorithm
for the operation.
➢ Input-output: the algorithm must be certain initial and precise inputs, and outputs that
may be generated both at its intermediate and final steps.

3.2 How to Write an Algorithm?

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

Let’s try to learn algorithm writing by using an example.


Problem − Design an algorithm to add two numbers and display the result.

Step 1 − START

Step 2 − declare three integers a, b & c

Step 3 − define values of a & b

Step 4 – c= a+b

Step 5 − print c

Step 6 – STOP

3.3 Advantages of Algorithms


➢ It is easy to understand.
➢ An algorithm is a step-wise representation of a solution to a given problem.
➢ In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier
for the programmer to convert it into an actual program.

3.4 Disadvantages of Algorithms


➢ Writing an algorithm takes a long time so it is time-consuming.
➢ Understanding complex logic through algorithms can be very difficult.
➢ Branching and Looping statements are difficult to show in Algorithms(imp).

3.5 Pseudo-code

Pseudo-code is a term that is often used in programming and algorithm-based fields. It is a


methodology that allows the programmer to represent the implementation of an algorithm.
Simply, we can say that it’s the cooked-up representation of an algorithm. Often at times,
algorithms are represented with the help of pseudocodes as they can be interpreted by
programmers no matter what their programming background or knowledge is. Pseudocode, as
the name suggests, is a false code or a representation of code that can be understood by even a
layman with some school-level programming knowledge.
It’s simply an implementation of an algorithm in the form of annotations and informative text
written in plain English. It has no syntax like any of the programming languages and thus
can’t be compiled or interpreted by the computer.

3.5.1 Advantages of Pseudocode


➢ Improves the readability of any approach. It’s one of the best approaches to start the
implementation of an algorithm.
➢ Acts as a bridge between the program and the algorithm or flowchart.
➢ Works as rough documentation, so the program of one developer can be understood
easily when a pseudo code is written out. In industries, the approach of documentation
is essential. And that’s where a pseudo-code proves vital.
➢ The main goal of a pseudo-code is to explain what exactly each line of a program should
do, hence making the code construction phase easier for the programmer.

EXAMPLES:

Example 1: Write the pseudocode for the addition of two numbers.

1. Begin

2. NUMBER s1, s2, sum

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

2. Print "Enter Any Number to Check, Even or Odd"

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.

3.6.1 Use of a flowchart

The following are the uses of a flowchart:

➢ It is a pictorial representation of an algorithm that increases the readability of the


program.
➢ Complex programs can be drawn in a simple way using a flowchart.
➢ It helps team members get an insight into the process and use this knowledge to collect
data, detect problems, develop software, etc.
➢ A flowchart is a basic step for designing a new process or adding extra features.
➢ Communication with other people becomes easy by drawing flowcharts and sharing
them.

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.

3.6.2 Types of Flowcharts

Three types of flowcharts are listed below:

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.

3.6.3 Flowchart symbols

Types of boxes used to make a flowchart

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

3.6.4 Advantages of Flowchart in C:

Following are the various advantages of flowcharts:

o Communication: A flowchart is a better way of communicating the logic of a program.


o Synthesis: Flowchart is used as working models in designing new programs and
software systems.
o Efficient Coding: Flowcharts act as a guide for a programmer in writing the actual
code in a high-level language.
o Proper Debugging: Flowcharts help in the debugging process.
o Effective Analysis: Effective analysis of logical programs can be easily done with the
help of a related flowchart.
o Proper Documentation: Flowchart provides better and proper documentation. It
consists of various activities such as collecting, organizing, storing, and maintaining all
related program records.
o Testing: A flowchart helps in the testing process.
o Efficient program maintenance: The maintenance of the program becomes easy with
the help of a flowchart.

3.6.5 Disadvantages of Flowchart in C:

The following are the various disadvantages of flowcharts:

o Time-consuming: Designing a flowchart is a very time-consuming process.


o Complex: It isn't easy to draw a flowchart for large and complex programs.
o There is no standard in the flowchart; there is no standard to determine the quantity
of detail.
o Difficult to modify: It is very difficult to modify the existing flowchart.
EXAMPLES

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.

You might also like