Está en la página 1de 28

Module 2

Problem Solving strategies –


Problem analysis – formal
definition of problem – Solution –
top- down design – breaking a
problem into sub problems-
overview of the solution to the sub
problems by writing step by step
procedure (algorithm) -
representation of procedure by
flowchart - Implementation of
algorithms – use procedures to
achieve modularity.

Examples for algorithms and flow


charts - at least 10 problems
Algorithm

• An algorithm is a finite sequence


of instructions, a logic and explicit
step-by-step procedure for
solving a problem.

• English representation of the logic


The characteristics of a
good algorithm are:
• Precision – the steps are precisely
stated(defined).
• Uniqueness – results of each step
are uniquely defined and only
depend on the input and the result
of the preceding steps.
• Finiteness – the algorithm stops
after a finite number of instructions
are executed.
• Input – the algorithm receives input.
• Output – the algorithm produces
output.
• Generality – the algorithm applies
to a set of inputs.
steps to design an
algorithm.
• Step 1 - START
It represents beginning of the
algorithm.
Step 2 - DECLARE
The variables used in algorithm are
declared in this step.
Step 3 - INPUT
Here we input the values
Step 4 - FORMULA
The required result is generated in this
step.
Step 5 - OUTPUT
It displays the output or result.
Step 6 - STOP
It is an end of Algorithm
find out number is odd or
even
step 1 : start
step 2 : input number
step 3 :check rem=number mod 2
step 4 : if rem=0 then
print "number even"
else
print "number odd"
end if
step 5 : stop
check whether the given
year is leap or not
• 1. START
2. declare year of type integer.
3. Enter the year
4. check the following condition
with if..else statement
(year%4==0&&year%100!=0 ||
year%400==0)

5. if the above condition is TRUE


then Print "leap year"
7. if the above condition is FALSE
then Print "not a leap year".
8. STOP
Flow Chart

• A flowchart is a diagram that


represents a process or algorithm.
The steps are represented by a
series of boxes or other
specialized symbols, then
connected with arrows.
• introduced by Frank Gilberth in
1921
• called “Process Flow Charts” at
the beginning
• Terminator
This symbol represents the beginning and end
point in a program. We use start and stop option in it.
Input/Output Symbol
This symbol is used to take any input or output in
the algorithm.
Process Symbol
A rectangle indicates the processing, calculation
and arithmetic operations
Decision Symbol
It is used when we want to take any decision in
the program.
Connector Symbol
This symbol is used to connect the various
portion of a flow chart. This is normally used when the
flow chart is split between two pages
Data Flow Symbol
This symbol is used to display the flow of the
program. It shows the path of logic flow in a program.
Example
find the sum of the first
50 natural numbers.
find the largest of three
numbers A, B, and C.
Odd or even
factorial value of a
number
• step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6
until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
• °C x 9/5 + 32 = °F
• (°F - 32) x 5/9 = °C
Fahrenheit and converts
into centigrade.
• 1. Start
2. Input Temperature in
Fahrenheit in F variable.
3. Calculate Centigrade C = (F-32)
x5/9
4. Show temperature in
centigrade
5. Stop
all roots of a quadratic
equation ax2+bx+c=0.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2,
rp and ip;
Step 3: Calculate discriminant D←b2-4ac
Step 4: If D≥0
4.1 r1←(-b+√D)/2a
4.2 r2←(-b-√D)/2a
4.3 Display r1 and r2 as roots.
Step 5: Else Calculate real part and
imaginary part
5.1 rp←b/2a
5.2 ip←√(-D)/2a
5.3 Display rp+j(ip) and rp-j(ip) as
roots
Step 6: Stop
sum of the digits of a
given number
• Input a Number
• Initialize Sum to zero
• While Number is not zero
• Get Remainder by
Number Mod 10
• Add Remainder to Sum
• Divide Number by 10
• Print sum
• While (N != 0)
Rem = N % 10;
Sum = Sum + Rem;
N = N / 10;
Fibonacci Sequence Up to
a Certain Number
• First Term = 0
Second term = 1
Third Term = First + Second = 0+1
=1
Fourth term = Second + Third
=1+1 = 2
Fifth Term = Third + Fourth = 2+1
=3
Sixth Term= Fourth + Fifth = 3+2
=5
Seventh Term = Fifth + Sixth = 3+5
=8
Fibonacci Sequence Up to a
Certain Number

• i=0, Num1=0, Num2=1


• While (i < n)
Print Num1
Num1=Num2
Num2=Num1+Num2
i++
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
paliandrom
• Reversed=0
• while( n!=0 )
remain = n%10
reversed = reversed*10 + remain;
n = n/10

• If (n== reveresd)
palindrom

• else
Not palindrom

También podría gustarte