Introduction To Algorithms l2-2
Introduction To Algorithms l2-2
1. Definitions
2. Variable assignments
3. Conditional statements
4. Boolean expressions
5. Iterative statements
6. Exercises
2
What are algorithms?
A mathematical formula isn't necessarily an algorithm but formulas are often key elements of algorithms. A
formula describes a relationship between mathematical entities variables and constants but it might not tell us
what to do with that relationship (at least not directly).
For example, the following formula describes the relationship between temperatures in the Celsius and
Fahrenheit scales:
5(F−32)=9C
where
The formula defines the relationship between temperatures in Celsius and Fahrenheit, but it doesn't give us
an explicit algorithm for converting from one to the other. Fortunately, if we have some understanding of
algebra, we can easily write such an algorithm.
First, we can use the basic operations of algebra to convert the previous formula into this form:
3
C = 5(F – 32) /9
What are algorithms?
Example 1
Working from this formula, it's a relatively straightforward task to write an algorithm to convert from Fahrenheit to Celsius:
Note that we've shifted from describing the relationship between the two scales to listing computational steps in a specific
order.
4
What are algorithms?
1. Start.
2. Read r: radius value as the input given by the user.
3. Calculate the Area: 3.14 * r * r.
4. Display the Area.
5. End.
5
What are algorithms?
◼ An algorithm is a sequence of finite instructions, often used for calculation and data processing.
◼ It is formally a type of effective method in which a list of well-defined instructions for completing a
task will, when given an initial state, proceed through a well-defined series of successive states,eventually
terminating in an end-state.
6
Characteristics of an algorithm
source: https://www.geeksforgeeks.org/introduction-to-algorithms/
What do algorithms have to do with programming?
Computer programming primarily involves creating clear, step-by-step procedures, or algorithms, for a computer to follow
in order to achieve specific results. While computers excel at calculations and memory, they are incapable of applying
these abilities to practical problems without human guidance. Tasks like calculating a sine or logarithm may be simple for
a computer, but solving more complex problems, such as graphing functions or performing real-world calculations,
requires algorithms written by programmers to "teach" the computer how to complete them.
Example: plot the graph of y = sin x on the screen, or balance your checkbook, or compute the area of the region
bounded by 1 ≤ x ≤ 100 and 0 ≤ y ≤ 1/ x, the computer is helpless until someone writes algorithms to accomplish these
tasks, and “teaches” them to the computer.
8
Difference between Algorithm and Pseudocode:
An algorithm is a well defined sequence of instructions that provide a solution to the given problem.
A pseudocode is a method which is used to represent an algorithm.
An algorithm has some specific characteristics that describe the process.
A pseudocode on the other hand is not restricted to something. It’s only objective is to represent an algorithm in a
realistic manner.
9
There are three ways to specify an algorithm. They are:
a. Natural language
b. Pseudocode
c. Flowchart
Pseudocode and flowchart are the two options that are most widely used nowadays for
specifying algorithms.
Natural language
It is very simple and easy to specify an algorithm using natural language. But many times, specification of
algorithm by using natural language is not clear and thereby we get brief specification.
Example: An algorithm to perform addition of two numbers.
Step 1: Read the first number, say a.
Step 2: Read the first number, say b.
Step 3: Add the above two numbers and store the result in c.
Step 4: Display the result from c.
Such a specification creates difficulty while implementing it. Hence many programmers prefer specification
of algorithm by means of Pseudocode.
11
Flowchart
In the earlier days of computing, the dominant method for specifying algorithms was a flowchart,
this representation technique has proved to be inconvenient. Flowchart is a graphical
representation of an algorithm. It is a method of expressing an algorithm by a collection of
connected geometric shapes containing descriptions of the algorithm's steps.
12
Pseudocode
What is pseudocode?
Pseudocode is a valuable tool for outlining the logic of a program before it's written or for documenting it after the fact. It
resembles programming code but isn’t directly executable. Pseudocode can express anything from high-level program
logic to detailed functions and methods. However, there’s no standard format or vocabulary for pseudocode, and its
structure can vary greatly, ranging from formal prose to something closely resembling actual code. Despite this flexibility,
pseudocode helps in clearly articulating algorithms for programmers, even those unfamiliar with the specific logic.
Use of pseudocode:
◼ Simplify logic of a program
◼ communicate easily with programmers
◼ Save time and prevent mistakes
13
Pseudocode
Ultimately, the most important characteristic of pseudocode is not really what it is, but what it makes possible. As noted
above, when we start with well-written pseudocode, virtually any programmer with reasonable competence in a given
programming language should be able to implement the algorithm described by the pseudocode, in the given language,
with little or no need for further instruction.
Pseudocode is made up of the following logics structures that have been proved to be sufficient for writing any computer
program:
◼ Sequence Logic
◼ Selection Logic
◼ Iteration Logic
14
Pseudocode
Sequence Logic
It is used to perform instruction in a sequence, which is one after another.
Thus, for sequence logic instructions pseudocode are writing in an order in which they are to be performed
The logic flow of pseudocode are from top to bottom.
15
Pseudocode
Selection Logic
It is used for making decision and for selecting the proper path out of two or more alternative in program logic.
It is also known as decision logic.
Selection logic is depicted as either IF……THEN or IF…… THEN ELSE structure
16
Pseudocode
Iteration logic
It is used to produce loops when one or more instruction may be
executed several times depending on some the conditions
It used structure called DO…WHILE, FOR, REPEAT…UNTIL
17
Pseudocode
18
Pseudocode
1. An algorithm is a procedure. It has two parts; the first part is head and the second part is body.
2. The Head section consists of keyword Algorithm and Name of the algorithm with parameter list.
E.g. Algorithm name1(p1, p2,...,pn)
The head section also has the following:
//Problem Description:
//Input:
//Output:
3. In the body of an algorithm various programming constructs like if, for, while and some statements like
assignments are used.
4. The compound statements may be enclosed with { and \ brackets. if, for, while can be open and closed by
{, } respectively.
Proper indentation is must for block.
5. Comments are written using // at the beginning.
19
Pseudocode
6. The identifier should begin by a letter and not by digit. It contains alphanumeric letters after first letter.
7. The left arrow ":=" used as assignment operator. E.g. v:=10
8. Boolean operators TRUE, FALSE,
9. Logical operators (AND, OR, NOT)
10. Relational operators(comparaison) <(less than),<=(less or equal),>(greater than),>=(greater or
equal),==(equal),!=(not equal), <>(between) are also used.
11. Arithmetics operators +(plus),-(subtract),*(multiply),^(exponent)
12. Input and Output can be done using read and write.
13. Array[], if then else condition, branch and loop can be also used in algorithm.
20
Pseudocode
21
TITLE
THANKS!
9/12/2021 22