0% found this document useful (0 votes)
3 views

2_Algorithms and Flowchartfinalversion

The document discusses the fundamentals of algorithmic problem solving, emphasizing the importance of algorithms as structured sequences of steps to efficiently solve problems. It covers key characteristics of algorithms, the algorithm development process, and various representation techniques such as pseudocode and flowcharts. Additionally, it highlights the advantages and limitations of using algorithms and flowcharts in programming and problem-solving.

Uploaded by

deershamt2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2_Algorithms and Flowchartfinalversion

The document discusses the fundamentals of algorithmic problem solving, emphasizing the importance of algorithms as structured sequences of steps to efficiently solve problems. It covers key characteristics of algorithms, the algorithm development process, and various representation techniques such as pseudocode and flowcharts. Additionally, it highlights the advantages and limitations of using algorithms and flowcharts in programming and problem-solving.

Uploaded by

deershamt2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Logical Thinking and

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.

• The formulation of algorithms have always been an


important element of problem-solving .
What is an Algorithm?

• An algorithm is a sequence of steps to solve a


particular problem

• An algorithm is a collection of well-defined,


unambiguous and effectively computable
instructions, if executed it will return the proper
output.
• Well-defined- The instructions given in an algorithm should
be simple and well specified.

• Unambiguous- The instructions should be clear , there


should not be ambiguity .

• Effectively computable- The instructions should be written


step by step ,which helps computer to understand the
control flow .
Importance of algorithms
• The importance of algorithms lies in their role as the foundation for
solving problems efficiently
Here are key reasons why algorithms are crucial:
• Efficiency: Solve problems quickly and with minimal resources.
• Scalability: Handle growing data or complexity smoothly.
• Accuracy: Ensure consistent and precise results.
• Automation: Power automated systems for tasks like sorting and
prediction.
• Problem-Solving: Break down complex problems into manageable
steps.
• Optimization: Find the best solutions, improving performance and
outcomes.
An algorithm has the following
characteristics :
Input: An algorithm may or may not require input

Output: Each algorithm is expected to produce at least one result

Definite: Each instruction must be clear and unambiguous

Finite: If the instructions of an algorithm are executed, the


algorithm should terminate after finite number of steps

Unambiguous: each step of the algorithm is clear and precise,


leaving no room for confusion or multiple interpretations.

Feasible: if each step An algorithm is can be completed within a


An algorithmic
Development Process
An algorithm development process consists of five major
steps.

• Step 1: Obtain a description of the problem.

• Step 2: Analyze the problem.

• Step 3: Develop a high-level algorithm.

• Step 4: Refine the algorithm by adding more detail.

• Step 5: Review the algorithm.


Building Blocks of Algorithm

• Any algorithm can be constructed using three basic building


blocks.

• These three building blocks are

Sequence (Action)

Selection (Decision)

Iteration (Repetition)
• Sequence

This describes a sequence of actions that a program carries out one


after another, unconditionally. Execute a list of statements in order.

• Selection

Selection is the program construct that allows a program to choose


between different actions. Choose at most one action from
several alternative conditions.

• Repetition

Repetition (loop) may be defined as a smaller piece of code that can


be executed several times in a main program. Repeat a block of
statements while a condition is true.
NO YES NO

YES
Advantages of Algorithm

• It is a step-wise representation of a solution to a given


problem, which makes it easy to understand.

• An algorithm uses a definite procedure.

• It is not dependent on any programming language, so it


is easy to understand for anyone even without
programming knowledge.

• Every step in an algorithm has its own logical sequence


so it is easy to debug.
Algorithm Representation
Techniques
• Algorithm representation techniques help in designing and
visualizing the structure of an algorithm before implementation.

• Three common techniques are


• Pseudocode,
• Stepwise refinement,
• Top-down design:
Pseudo Code
• “Pseudo” means imitation or false and “code”
refers to the instructions written in a programming
language.

• Pseudocode is another high-level representation


used for planning a program

• Pseudocode is also called Program Design


Language(PDL)
Pseudo code

• Pseudo code is a detailed readable description of


what a computer program or algorithm must do,
expressed in a formally-styled natural language
rather than in a programming language.

• Pseudo code is sometimes used as a guideline in the


process of developing a program
Guidelines to write
Pseudocode

• Pseudocode is written using structured English.

• Pseudocode should be concise

• Keyword should be in capital letter


Pseudo code
• An outline of a program, written in a form that can
easily be converted into real programming
statements.

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

IF condition THEN [ TRUE BLOCK ]


sequence 1
ELSE [FALSE BLOCK]
sequence 2
ENDIF
PSEUDOCODE Key WORDS
CONT….
• Repetition :
Repetition can be implemented using constructs like the
repeat loop, while loop, and if.. then .. goto .. loop.
It will repeat the statements until condition is true
Repeat until Condition
Statements ;



End Repeat
Examples
• Pseudocode to add two numbers:

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

Algorithm for Addition of two numbers

Step 1: Start

Step 2: Get two numbers ‘a’ and ‘b’ as input

Step 3: Add the numbers ‘a’ and ‘b’ and store it

in ‘c’

Step 4: Print ‘c’


Algorithm to find biggest among 2 numbers

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: ii+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.

• Flow charts are easy-to-understand diagrams that show how


the steps of a process fit together

• Flowchart can be used to


 Demonstrate the way code is organized.
 Visualize the execution of code within a program.
 Specify the method of solving a problem
 Understand the flow of the process or an algorithm
Flow Chart Constructs
Example 1 : sequential
flowchart to add two numbers entered by user.
Example 2 : conditional
flowchart to find biggest of two numbers entered by user.
Example 3 : REPETITION
flowchart to display numbers 1 to 10.
Flowchart for finding the factorial of a number
FLOW CHART EXAMPLE
FLOWCHART EXAMPLE
• Problem:
Draw a flowchart to find the roots of a quadratic equation.
FLOW-CHART EXAMPLE
• Problem:
Prepare a flowchart to read the marks of a student and
classify them into different grades. If the marks
secured are greater than or equal to 90, the student is
awarded Grade A; if they are greater than or equal to
80 but less than 90, Grade B is awarded; if they are
greater than or equal to 65 but less than 80, Grade C is
awarded; otherwise Grade D is awarded.
FLOW-CHART EXAMPLE Cont..
FLOW-CHART EXAMPLE
FLOWCHART ADVANTAGES
• Communication
better way of communicating the logic of a system
• Effective analysis
problems can be analyzed more effectively
• Proper documentation
good program documentation needed for various purposes
• Efficient coding
guide or blueprint during the systems analysis and program
development phase
• Proper debugging
Flowcharts help in the debugging process.
• Efficient program maintenance
Maintenance of an operating program becomes easy with the
help of a flowchart
FLOWCHARTS LIMITATIONS

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

• Flowchart for finding whether the given number is


even or odd

You might also like