UNIT I
Building Blocks of Algorithms
Dr. Nisha Soms, AP(Sr.Gr.)/CSE
UCSG004 Python Programming for Engineers
PROBLEM SOLVING
 Problem solving is a systematic approach to define the
problem and creating number of solutions.
 The problem solving process starts with the problem
specifications and ends with a correct program.
PROBLEM SOLVING TECHNIQUES
 Problem solving technique is a set of techniques that helps in
providing logic for solving a problem.
 Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs
18-01-2024
2
THE PROBLEM SOLVING ASPECT
18-01-2024
3
 Is a creative process that involves the following
phases
 Problem Definition Phase
 Getting started on a problem
 The use of specific examples
 Similarities among problems
 Working backwards from solutions
ALGORITHMS
 An algorithm is a procedure consisting of a finite set of
unambiguous rules (instructions) which specify a finite
sequence of operations that provides the solution to a
problem.
OR
 A sequence of activities to be processed for getting
desired output from a given input.
OR
 Formal Definition: An algorithm is a finite number of
clearly described, unambiguous “double” steps that can
be systematically followed to produce a desired result for
the given inputs in a finite amount of time.
18-01-2024
4
ALGORITHMS
18-01-2024
5
 Convert temperature Fahrenheit to Celsius
 Inputs to the algorithm: Temperature in Fahrenheit
 Expected output: Temperature in Celsius
 Algorithm:
 Step1: Start
 Step 4: Read Temperature in Fahrenheit F
 Step 2: C = 5/9*(F*32)
 Step 3: Print Temperature in Celsius: C
 Step5: End
BUILDING BLOCKS OF ALGORITHMS/TYPES
18-01-2024
6
Building Block Common name
Sequence Action
Selection Decision
Iteration Repetition or Loop
 In a sequence structure, an action, or event, leads to the next
ordered action in a predetermined order.
 In a selection structure, a question is asked, and depending on the
answer, the program takes one of two courses of action, after which
the program moves on to the next event
 An iteration is a single pass through a group/set of instructions. Most
programs often contain loops of instructions that are executed over
and over again.
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
7
 Statements:
 Statement is a single action in a computer.
 In a computer statements might include some of the
following actions
 input data-information given to the program
 process data-perform operation on a given input
 output data-processed result
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
8
 State:
 Transition from one process to another process
under specified condition with in a time is called
state.
 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
 Sequence
 Selection
 Iteration
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
9
 Sequence:
 All the instructions are executed one after another is
called sequence execution.
 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
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
10
 Selection:
 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.
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
11
 Example
Write an algorithm to check whether he is eligible
to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
12
 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
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
13
 Functions:
 Function is a sub program which consists of block of
code(set of instructions) that performs a particular task.
 For complex problems, the problem is been divided into
smaller and simpler tasks during algorithm design.
 Benefits of Using Functions
 Reduction in line of code
 code reuse
 Better readability
 Information hiding
 Easy to debug and test
 Improved maintainability
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
18-01-2024
14
 Algorithm for addition of
two numbers using
function
 Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop

 sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
THANK YOU
18-01-2024
15

Building blocks of Algblocks of Alg.pptx

  • 1.
    UNIT I Building Blocksof Algorithms Dr. Nisha Soms, AP(Sr.Gr.)/CSE UCSG004 Python Programming for Engineers
  • 2.
    PROBLEM SOLVING  Problemsolving is a systematic approach to define the problem and creating number of solutions.  The problem solving process starts with the problem specifications and ends with a correct program. PROBLEM SOLVING TECHNIQUES  Problem solving technique is a set of techniques that helps in providing logic for solving a problem.  Problem solving can be expressed in the form of 1. Algorithms. 2. Flowcharts. 3. Pseudo codes. 4. Programs 18-01-2024 2
  • 3.
    THE PROBLEM SOLVINGASPECT 18-01-2024 3  Is a creative process that involves the following phases  Problem Definition Phase  Getting started on a problem  The use of specific examples  Similarities among problems  Working backwards from solutions
  • 4.
    ALGORITHMS  An algorithmis a procedure consisting of a finite set of unambiguous rules (instructions) which specify a finite sequence of operations that provides the solution to a problem. OR  A sequence of activities to be processed for getting desired output from a given input. OR  Formal Definition: An algorithm is a finite number of clearly described, unambiguous “double” steps that can be systematically followed to produce a desired result for the given inputs in a finite amount of time. 18-01-2024 4
  • 5.
    ALGORITHMS 18-01-2024 5  Convert temperatureFahrenheit to Celsius  Inputs to the algorithm: Temperature in Fahrenheit  Expected output: Temperature in Celsius  Algorithm:  Step1: Start  Step 4: Read Temperature in Fahrenheit F  Step 2: C = 5/9*(F*32)  Step 3: Print Temperature in Celsius: C  Step5: End
  • 6.
    BUILDING BLOCKS OFALGORITHMS/TYPES 18-01-2024 6 Building Block Common name Sequence Action Selection Decision Iteration Repetition or Loop  In a sequence structure, an action, or event, leads to the next ordered action in a predetermined order.  In a selection structure, a question is asked, and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event  An iteration is a single pass through a group/set of instructions. Most programs often contain loops of instructions that are executed over and over again.
  • 7.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 7  Statements:  Statement is a single action in a computer.  In a computer statements might include some of the following actions  input data-information given to the program  process data-perform operation on a given input  output data-processed result
  • 8.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 8  State:  Transition from one process to another process under specified condition with in a time is called state.  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  Sequence  Selection  Iteration
  • 9.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 9  Sequence:  All the instructions are executed one after another is called sequence execution.  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
  • 10.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 10  Selection:  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.
  • 11.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 11  Example Write an algorithm to check whether he is eligible to vote? Step 1: Start Step 2: Get age Step 3: if age >= 18 print “Eligible to vote” Step 4: else print “Not eligible to vote” Step 6: Stop
  • 12.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 12  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
  • 13.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 13  Functions:  Function is a sub program which consists of block of code(set of instructions) that performs a particular task.  For complex problems, the problem is been divided into smaller and simpler tasks during algorithm design.  Benefits of Using Functions  Reduction in line of code  code reuse  Better readability  Information hiding  Easy to debug and test  Improved maintainability
  • 14.
    BUILDING BLOCKS OFALGORITHMS (statements, state, control flow, functions) 18-01-2024 14  Algorithm for addition of two numbers using function  Main function() Step 1: Start Step 2: Call the function add() Step 3: Stop   sub function add() Step 1: Function start Step 2: Get a, b Values Step 3: add c=a+b Step 4: Print c Step 5: Return
  • 15.