2_Algorithms and Flowchartfinalversion
2_Algorithms and Flowchartfinalversion
Problem Solving
UNIT 1
Topic : Algorithms , Pseudocode
and Flowcharts
Algorithmic Problem
Solving
• Algorithmic-problem solving means solving
problems that require the formulation of an
algorithm for their solution.
Sequence (Action)
Selection (Decision)
Iteration (Repetition)
• Sequence
• Selection
• Repetition
YES
Advantages of Algorithm
Keywords :
• Input: READ, OBTAIN, GET
• Output: PRINT, WRITE
• Compute: COMPUTE, CALCULATE, DETERMINE
• Initialize: SET, INITIALIZE
• Add one: INCREMENT
• Subtract one: DECREMENT
Pseudocode constructs
Pseudocode
• Sequential :
Sequence means that each step or process in the
algorithm is executed in the specified order
PSEUDOCODE Key WORDS CONT….
• Conditional :
• In algorithms the outcome of a decision is either
true or false; there is no state in between
• If condition is true then statements in if block will
be executed otherwise statements the in the else
block will be executed
START
READ a , b
COMPUTE c=a + b
PRINT c
STOP
Pseudocode to find biggest among 2 numbers
START
READ a and b
IF a>b THEN
PRINT “A is big”
ELSE
PRINT “B is big”
ENDIF
STOP
Operators used in algorithm
• Arithmetic operators
• ‘←’ ….Assignment
• ‘+’….. Addition
• ‘–’….. Subtraction
• ‘*’….. Multiplication
• ‘/’….. Division
• Relational operators
• ‘>’ ….. Greater than
• ‘<’ …… Less than
• ‘==’ …… Equality
• ‘>=’ …… Greater than or equal to
• ‘!=’ …… Non- equality
• Logical operator
• AND, OR and NOT
Algorithm example
Examples
Step 1: Start
in ‘c’
Step 1 :Start
Step 2: Get two numbers as input and store it into ‘a’
and ‘b’
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
Find whether a given year is a leap year or not
Algorithm:
BEGIN
Step 1 : Accept the YEAR
Step 2 : IF ((YEAR%4=0 AND YEAR%100!=0) OR (YEAR%400=0))
Display “Year is a leap year”
ELSE
Display “Year is not a leap year”
END IF
END
For the logic in the IF condition refer Truth Table for Leap year problem
27
Algorithm to calculate factorial
Step 1: Start
Step 2: Read the number num.
Step 3: Initialize i = 1 and fact = 1
Step 4: Repeat step 4 through 6 until i is less than or
equal to num
Step 5: fact fact * i
Step 6: ii+1
Step 7: Print fact
Step 8: Stop
Stepwise Refinement
Definition
Stepwise refinement (also known as decomposition) is the process
of breaking down a complex algorithm into smaller, more
manageable parts. Each part is refined step by step until it is
simple enough to be coded.
Purpose: Helps to tackle large problems by dividing them into sub-
problems, allowing for gradual development
Characteristics: Focuses on modular development, making it
easier to debug and maintain.
Each refinement results in a more detailed and closer-to-code
description.
Process:
Start with a general solution. Refine each step into more specific
details until all steps are concrete and implementable.
Example 1: Calculating the Sum of a List
of Numbers
Step 1: General Problem
1. Get the list of numbers.
2. Calculate the sum of all numbers.
3. Display the sum.
• Step 2: Refined Steps
1.Ask the user how many numbers will be input.
2.Initialize the sum to zero.
3.For each number, add it to the sum.
4.After all numbers are added, display the total sum.
• Step 3: Detailed Refinement
1. Input n, the number of numbers to be summed.
2. Set sum = 0.
3. Repeat the following for i = 1 to n:Input the next number.
4. Add this number to sum.Output sum.
Top-Down Design
Definition: Top-down design is a problem-solving approach where
the system or algorithm is broken down from the highest level
(general concept) into smaller sub-components (detailed functions
or processes).
Purpose: This method ensures that the overall structure and goal of
the algorithm are addressed first, and then detailed functionality is
developed.
• Characteristics:
• Focuses on high-level organization, making sure each component
works towards the larger system's goals.
• Encourages thinking about the entire structure before diving into
details.
Algorithm example
Algorithm example
Flowcharts
• A flowchart is a visual representation of sequence of steps
and decision needed to perform a process.
• Complex logic
If the program logic is quite complicated, flowchart
becomes complex and clumsy
• Alterations and modifications
Difficult to alter the flowchart
• Reproduction
Since the flowchart symbols cannot be typed in, the
reproduction of a flowchart becomes a problem
• Loss of objective
Essentials of what has to be done can easily be lost in
the technical details of how it is to be done
Exercises
• Flow-chart for printing the area of a circle for a
given radius.