Esc101: Fundamentals of Computing: 2011-12-Monsoon Semester

You might also like

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

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester

Please switch off your mobile phones.

Acknowledgement
Most of the slides used today were taken from the lecture notes of Dr. Surender Baswana.

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Algorithm
A plan or method to solve a problem
Should specify steps that can be followed unambiguously Normally would have an input and an output Should complete the task in finite time

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

How to Add Two Numbers


Execute the following steps: 1. Align the numbers such that the rightmost digit of one number is right above the rightmost digit of the other. 2. If the number of digits is different then add extra zeroes in front of the smaller number. 3. Start from the rightmost digit do the following steps and move leftward. i. Compute the sum of two digits and carry if any. ii. If the sum is single digit, then write that out. iii. Else (the sum is greater than 9), let the sum be xy, then write y and x is the carry for adding the next digits. (For example, if sum is 21, then write 1, and carry is 2.)
Lec-02 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Salient Features of the Algorithm


The entire method consists of a finite sequence of steps/instructions. Each step is precise, unambiguous and can be carried out mechanically. It works for all positive numbers.

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

An Interesting Algorithmic Problem


Searching for a shop At an intersection, roads go to North, East, South, and West directions. There is a person at that intersection, who is searching for a shop. The road on which the shop is located is unknown. The distance of the shop from the intersection is unknown. Design an algorithm to find the shop.
Lec-02 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Examples of problems to be solved algorithmically


Given a number, calculate all its prime factors Given two numbers, find its HCF and LCM Given a string (sequence of characters), list all its permutations. Given a list of numbers, create another list where the numbers appear in increasing order.

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

A few important facts


There are many non-trivial problems for which no algorithm can be designed. There are many interesting problems for which there are no practical algorithms. Of course, there are lots of problems for which efficient algorithms exist.

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Flow Chart
Many times, an algorithm is easy to design/understand, if it is represented pictorially. Different steps are shown as boxes. There are boxes for checking conditions, assigning values, reading, writing, etc. There are arrows which determine the direction of flow, or the order in which steps should be executed.

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Flow Chart for computing Factorial N

Source: Wikipedia
Lec-02 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Any Questions?

Lec-02

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

You might also like