Problem Solving and Python Programming: UNIT I: Computational Thinking and Problem Solving

Algorithms

Definition, Examples, Characteristics

An algorithm is a finite set of instructions for performing a particular task. The instructions are nothing but the statements in simple English language.

Algorithms      AU : May-19, Dec.-19, Marks 8

Definition of Algorithm : An algorithm is a finite set of instructions for performing a particular task. The instructions are nothing but the statements in simple English language.

Example :

Let us take a very simple example of an algorithm which adds the two numbers and store the result in a third variable.

Step 1: Start

Step 2: Read the first number in variable 'a'

Step 3: Read the second number in variable 'b'

Step 4: Perform the addition of both the numbers and store the result in variable 'c'.

Step 5: Print the value of 'c' as a result of addition.

Step 6: Stop.

 

1. Characteristics of Algorithm

1. Each algorithm is supplied with zero or more inputs.

2. Each algorithm must produce at least one output

3. Each algorithm should have definiteness i.e. each instruction must be clear and unambiguous.

4. Each algorithm should have finiteness i.e. if we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after finite number of steps.

Each algorithm should have effectiveness i.e. every instruction must be sufficiently basic that it can in principal be carried out by a person using only pencil and paper. Moreover each instruction of an algorithm must also be feasible.

 

Example 1.3.1 Find the area of a circle of radius r.

Solution :

Inputs to the algorithm :

Radius r of the Circle.

Expected output :

Area of the Circle

Algorithm :

Step 1: Start

Step 2: Read input the Radius r of the Circle

Step 3: Area ← PI*r*r // calculation of area

Step 4: Print Area

Step 5: Stop

 

Example 1.3.2 An algorithm to calculate even numbers between 0 and 99.

Solution :

Step 1 : Start

Step 2: Read n or Initialize n = 99

Step 3: Initialize i = 2

Step 4: If i < = n, then goto step 5 else goto step 7

Step 5: If i%2 = 0, then goto step 5.1,5.2 else goto step 6

    Step 5.1: Print i

    Step 5.2: i = I + 1 goto step 4

Step 6: i = I + 1 goto step 4

Step 7: Stop

 

Example 1.3.3 Outline the algorithm for displaying the first n odd numbers.

AU : May-19, Marks 8

Example 1.3.2 An algorithm to calculate even numbers between 0 and

Solution :

Step 1 : Start

Step 2: Read n

Step 3: Initialize i = 1

Step 4: If i<=n then goto step 5 else goto step 9

Step 5: if i%2!=0 then goto step 6 else goto step 7

Step 6: Print i

i = i + 1

Step 7: i = i + 1

Step 8: goto step 4

Step 9: stop

 

Example 1.3.4 Write an algorithm to find the minimum number in a given list of elements.

AU : Dec.-19, Marks 2

Solution :

Step 1: start

Step 2: Read n numbers and store them in a list.

Step 3: set count =1

Step 4: min = list[count]

Step 5: while(not end of the list) then goto step 6 otherwise goto step 8

Step 6: if(list[count]<min)

min = list[count]

count = count+1

Step 7: goto step 5

Step 8: if the complete list is scanned then display the value present in the min variable as the minimum number in the list.

Review Question

1. What is an algorithm? Summarize the characteristics of a good algorithm.

AU : May-19, Marks 8

 

Problem Solving and Python Programming: UNIT I: Computational Thinking and Problem Solving : Tag: Engineering Python : Definition, Examples, Characteristics - Algorithms