1.3 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:

1. Getting specified output is essential after algorithm is executed.

2. One will get output only if algorithm stops after finite time.

3. 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 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

1.3.1 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

Step2: Area ← PI*r*r // calculation of area

Step3: Print Area

Problem 2: Ravi has to attend at least 70% of Practical Classes for C programming to be eligible to appear in the examination. There are total 50 Practical Classes for C programming. He has attended 20 out of 30 classes held so far. Find, at least how many more classes to be attended by Ravi to be eligible for appearing in Practical Examination.

Inputs to the algorithm:

i.Percentage of Attendance Required

ii.Total Number of Classes

iii. Number of Classes held so far

iv.Number of classes attended so far by Ravi

Expected output:

Number of classes to be attended to become eligible for appearing in examination

Algorithm:

Step1: Read Total Number of Classes C

Step2: Read Percentage Required P

Step3: Read Classes already attended Ca

Step4: Total Classes to be attended Ct← C*P/100

Step5: More Classes to be attended Cm←CtCa

Step6: Print Cm

Problem 3: Convert temperature Fahrenheit to Celsius

Inputs to the algorithm:

Temperature in Fahrenheit

Expected output:

Temperature in Celsius

Algorithm:

Step 1: Read Temperature in Fahrenheit F

Step 2: C←5/9*(F32)

Step 3: Print Temperature in Celsius: C

Problem 4 : Ramshewak goes to market for buying some fruits and vegetables. He is having a currency of Rs 500 with him for marketing. From a shop he purchases 2.0 kg Apple priced Rs. 50.0 per kg, 1.5 kg Mango priced Rs.35.0 per kg, 2.5 kg Potato priced Rs.10.0 per kg, and 1.0 kg Tomato priced Rs.15 per kg. He gives the currency of Rs. 500 to the shopkeeper. Find out the amount shopkeeper will return to Ramshewak. and also tell the total item purchased.

Before we write algorithm for solving above problem let we find out what are the inputs to the algorithm and what is expected output.

Inputs to the algorithm are:

1. amount of different items purchase, for example 2.0 kg Apple etc.

2. price of the items, for example Mango is Rs. 35.0 per kg

3. total amount given to the shopkeeper

Expected output:

Amount to be returned by shopkeeper after deducting total price of the purchased

vegetables and fruits, and total items purchased.

Algorithm:

Step1: Total ← 0, i ← 1;

Spet2: Read amount purchased and unit price of item i

Step3: Total Total + amount of item i* price per unit of item i

Step4: i← i+1

Step5: Repeat Step 2 to 4 for each purchased item

Step6: Read Total amount given to the shopkeeper as GivenAmount

Step7: RefundAmount ←GivenAmountTotal

Step8: Print amount to be refund is Rs.: RefundAmount

Step9: Print total item purchased are: i

Problem 5: Find factorial of N.

Inputs to the algorithm are:

1. Number N

Expected output:

Factorial of N

Algorithm:

Step 1 : Result R←1

Step 2: I ←1

Step 3: Read the Number N

Step 4: Compare I with N

Step 5: If I <=N Then R ← R*I ,Otherwise GOTO Step 8

Step 6: I← I+1

Step 7: Repeat Step 4 and 6

Step 8: Print R

Problem 6: Print the Table of N.

Inputs to the algorithm are:

Number N

Expected output:

Table of N

Algorithm:

Step 1: I←1

Step 2: Read N

Step 3: If I <= 10 then Print I*N otherwise GOTO Step 6

Step 4: I I+1

Step 5: Repeat Step 3 and 4

Step 6: Stop

1.3.2 Properties of algorithm

Donald Ervin Knuth has given a list of five properties for an algorithm, these properties are:

1) 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.

2) 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 for each activity in the algorithm.

3) 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.

4) 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 result 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.

5) 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.

Any algorithm should have all these five properties otherwise it will not fulfill the basic objective of solving a problem in finite time. As you have seen in previous examples, every step of an algorithm puts you closer to the solution.

While writing an algorithm for a problem if you are writing some operations such as a loop for performing some operation(s) repeatedly, then your loop must stop after finite number of iteration\repetition. While solving complex activity, for example for getting the total sum of all the items purchased from a general store, the better approach is to calculate the price of all the items separately and then add them to get the final sum. By doing this, you assure the finiteness and effectiveness of the operations in your algorithm.

Comments

Popular posts from this blog

3.8 SECURE NETWORK DEVICES

3.5 SECURITY ISSUES FOR SMALL AND MEDIUM SIZED BUSINESSES

3.6 TOOLS FOR NETWORK SECURITY