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

Building Blocks of Algorithm

Algorithm is basically a sequence of instructions written in simple English language. The algorithm is broadly divided into two sections -1) Heading Section and 2) Body Section

Building Blocks

AU : Dec.-19, Maks

• Algorithm is basically a sequence of instructions written in simple English language. The algorithm is broadly divided into two sections -1) Heading Section and 2) Body Section

Algorithm is a procedure consisting of heading and body. The heading consists of keyword Algorithm and name of the algorithm and parameter list.

Then in the heading section we should write following things :

#Problem Description :

#Input :

#Output :

• Then body of an algorithm is written, in which various programming co for, while or some assignment statements may be written.

• Algorithmic body consists of basic building blocks such as Simple Statements, Control Flow Statements and Functions

(1) Simple Statements

• The simple statements consists of assignment statements, input/output statements and comment statements.

• Using assignment operator ← an assignment statement can be given.

For instance :

Variable ← expression

• There are other types of operators such as Boolean operators such as true or false. Logical operators such as and, or, not. And relational operators such as <, < =, >, > =, =

•The array indices are stored with in square brackets '[' ']'. The index of array usually start at zero. The multidimensional arrays can also be used in algorithm. The inputting and outputting can be done using read and write.

For example:

write("This message will be displayed on console") ;

read(val);

• The comment statemement is non executable statement. It provides additional

information about the programming statement. In Python the comment is specified using #

(2) Control Flow Statements Control flow :

The process of executing the individual statements in a given order is called control flow.

The control can be executed in three ways

1. Sequence 2. Selection 3. Iteration

a. Sequence :

All the instructions are executed one after another is called sequence execution. This describes a sequence of actions that a program carries out one after another, unconditionally.

Execute a list of statements in order.

Example :

Add two numbers:

Step 1: Start

Step 2: get a,b

Step 3: calculate c=a+b Step 4: Display c

Step 5: Stop

b. Selection :

Selection is the program construct that allows a program to choose between different actions. Choose at most one action from several alternative conditions.

A selection statement causes the program control to be transferred to a specific part of the program based upon the condition.

If the conditional test is true, one part of the program will be executed, otherwise it will execute the other part of the program.


Algorithm to find biggest among 2 numbers :

Step 1 : Start

Step 2: Get two numbers as input and store it in to a and b Step

Step 3: If a is greater than b then

Step 4: Print a is big

Step 5: else

Step 6: Print b is big

Step 7: Stop

c. Iteration :

In some programs, certain set of statements are executed again and again based upon conditional test. i.e. executed more than one time. This type of execution is called looping or iteration

Example

Write an algorithm to print all natural numbers up to n

Step 1: Start

Step 2: get n value.

Step 3: initialize i=1

Step 4: if (i<=n) go to step 5 else go to step 7

Step 5: Print i value and increment i value by 1

Step 6: go to step 4

Step 7: Stop

Repetition(loop) may be defined as a smaller program that can be executed several times in a main program. Repeat a block of statements while a condition is true.

Algorithm for Washing Dishes

Step 1 : Stack dishes by sink.

Step 2: Fill sink with hot soapy water.

Step 3: While moreDishes

Step 4: Get dish from counter,Wash dish

Step 5: Put dish in drain rack.

Step 6: End While

Step 7: Wipe off counter.

Step 8: Rinse out sink.

Algorithm to calculate factorial no :

Step 1: Start

Step 2: Read the number num.

Step 3: Initialize i is equal to 1 and fact is equal to 1

Step 4: Repeat step 4 through 6 until i is equal to num

Step 5: fact=fact * i

Step 6: i=i+1

Step 7: Print fact

Step 8: Stop

(3) Function

The Function is a programming constructs used for giving a name to a piece of coding. This piece of code is referred as function body.

When the name of the function is called, the body is executed. Each execution of the body is called activation of that body.

The function can be called from expression. For example -

total=amount+Calculate(interest);

In above expression the function Calculate is called.

Syntax of function

<function-name>(<parameter-list>)

Elements of Function

Various elements of function are –

1. Name for declaration of function

2. Body consisting of local declarations and statements

3. Formal parameters which are the placeholders of actuals

4. Optional result type.

For example : Consider following C code


int a,b; int c; a = 10;

Fig. 1.4.2 Example for use of function

Review Question

1. Discuss about the building blocks of algorithms. AU : Dec.-19, Marks 8

 

Problem Solving and Python Programming: UNIT I: Computational Thinking and Problem Solving : Tag: Engineering Python : - Building Blocks of Algorithm