[go: up one dir, main page]

0% found this document useful (0 votes)
22 views19 pages

Algorithmic Problem Solving

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

Algorithmic Problem Solving

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

Algorithmic Problem Solving

Algorithm

“An algorithm is a set of instruction for a


specific task”
Characteristics of Algorithms
• Well-ordered
• Unambiguous operations
• Effective Computational Operations
• Input
• Has a result
• Takes a finite amount of time
Building Blocks of Algorithms
• Statements
• State
• Control Flow
• Sequence
• Selection
• Repetition
• Functions
• Pseudocode
• Flowcharts
Sequence
Write an algorithm to compute the area of trapezium.
Algorithm: Area of Trapezium
Input: Three floating point variables a, b, h
Output: The Floating point variable area
Step1: Start
Step2: Read “Enter the values of two bases and
height”, a, b, h.
Step3: Compute the area
Area=0.5*(a+b)*h
Step4: Write “the area of trapezium”, area.
Step 5: Stop
Selection
The algorithm chooses one of them any alternatives for
execution.
Example: An algorithm to compute the larger of two numbers
Algorithm: Larger Of Two
Input: Two integer numbers a, b.
Output: The largest among a and b
Step1: Start
Step2: Read “Enter the values of a and b”,a, b
Step 3: If a>b then
Step4: Write “The greatest value is”, a
Step5: Else
Step6: Write “The greatest value is”,b
Step 7: Stop
Repetition
One or more steps of the algorithm are executed repeatedly
for a finite number of times.
Example: An algorithm to compute sum of natural numbers
Algorithm: SumOfNatural
Input: Three integer numbers i, max and sum
Output: The Sum of natural numbers
Step1: Start
Step2: Read the maximum number of natural numbers
as max
Step3: For I in range 1 to max repeat the following
steps
Step3.1:sum←sum+i
Step4: Write the sum of natural numbers as sum.
Step 5: Stop
Functions

Cohesive self contained block of code that


perform specific functionalities.
• Help us to write modular codes which in
turn make debugging easier
• Help to write code which is reusable
• Help in program maintenance.
Example: Write an algorithm to compute the area of a
triangle using user-defined function called Area of
Triangle() that accepts the base and height and returns the
computed area.
Algorithm: AreaOfTriangle(b,h)
Step 1: area = 0.5*b*h
Step 2: return area
Algorithm: Main Procedure
Step1: Start
Step2: Write “Program to compute the area of triangle”
Step3: Read “Enter the value of base”, b
Step4: Read “Enter the value of height”,h
Step 5: a= AreaofTriangle(b,h)
Step6: Write “the area of triangle is”,a
Step 7: Stop
Pseudocode
Write a pseudocode to check whether the entered
number is prime or not

Read a number as num


For I in range (2,n) repeat the following steps
If n mod I equals0
Write “The number is not prime”
Break
Else
Write “The number is prime”
Pseudocode Meaning
← Assignment Symbol=
Read To read data from keyboard or file
Write To Write data to standard output
Or file
For I in range1toN For loop for iteration
If If conditional statement
Else Else part of IF conditional
statement
Notations

PreparedBy,Dr.SaranyaKG
PreparedBy,Dr.SaranyaKG
• Assignment Symbol
←or=
Is used to assign value to the variable.
e.g. to assign value 5 to the variable
HEIGHT, statement is
HEIGHT←5
or
HEIGHT=5
• The symbol ‘=’ is used in most of the
programming language as an assignment
symbol, the same has been used in all
the algorithms and flowcharts.
• The statement C = A + B means that add the
value stored in variable A and variable B
then assign/store the value in variable C.
• The statement R=R+1means that add I to
the value stored in variable R and then
assign/store the new value in variable R
Mathematical Operators
Relational Operators
Algorithm & Flowchart to find the sum of two
numbers

Step-1: Start
Step-2: Input first numbers say A
Step-3: Input second number say B
Step-4: SUM = A + B
Step-5: Display SUM
Step-6: Stop

You might also like