KEMBAR78
Algorithms and Flowcharts | PDF | Algorithms | Control Flow
0% found this document useful (0 votes)
9 views48 pages

Algorithms and Flowcharts

Uploaded by

nagulan0407
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)
9 views48 pages

Algorithms and Flowcharts

Uploaded by

nagulan0407
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/ 48

INTRODUCTION TO PROBLEM

SOLVING
Learning Objectives
❖ Develop basic principles of computational thinking.
❖ Understand the concept of problem solving in the context of computer
science
❖ Decompose a given problem into its component parts including input(s),
process(es), and output(s).
❖ Use different methods of design to construct the solution to a problem –
algorithm, flowchart and pseudocode.
❖ To demonstrate the use the three constructs for developing algorithms:
sequence, decision, and repetition.
❖ To understand the finite- and infinite- loop.
STEPS FOR PROBLEM SOLVING By analysing a problem, we
would be able to figure out
Problem solving is the what are the inputs that
process of identifying a problem, our program should accept
developing an algorithm for the and the outputs that it
identified problem and finally should produce.
implementing the algorithm to develop
a computer program.

Test for correctness of


output for various input
parameter, response time,
meet requirements It is essential to devise a
solution before writing a
program code for a given
After finalising the problem. The solution is
algorithm, we need to represented in natural
convert the algorithm language and is called an
into the format which algorithm.
can be understood by
the computer to
generate the desired
solution.
Objectives
❖understand the
concept of Algorithm
ALGORITHM
Algorithm is a step-by-step process of solving a well-defined
computational problem.
Identify the input to be taken from the user.
Processing or computation to be performed to get the desired
result
• The output desired by the user

Input Process Output


Objectives
❖define and use the
three constructs for
developing algorithms: WRITE AN ALGORITHM TO PRINT
sequence, decision,
and repetition. “GOOD MORNING”.
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
CHARACTERISTICS OF AN ALGORITHM”.
• Precise – the steps are precisely stated or defined
• Unique – results of steps are well defined and only
depend on the inputs
• Finite – the algorithm always stops after a finite number
of steps
• Input – receives some input
• Output – produces some output
• There can be more than one approach to solve a problem and hence we can have more than one
algorithm for a particular problem.
Objectives
❖solve problems by
using flowchart
❖Define and use
the three constructs
FLOWCHART
for developing
flowchart: ❖ A flowchart is a type of diagram that represents the algorithm
sequence, decision,
and repetition. graphically using boxes of various kinds, in an order connected by
❖understand the
finite- and infinite- arrows.
loop.

❖Each shape represents a step of the solution process and the arrow

represents the order or link among the steps.


Objectives
❖solve problems by
using flowchart FLOWCHART SYMBOLS
❖Define and use Symbols Functions
the three constructs
for developing Start/stop
flowchart:
sequence, decision,
and repetition. Input/output
❖understand the
finite- and infinite-
loop. Processing

Decision Box

Flow of control
Connector
PSEUDOCODE
A pseudocode is another way of representing an algorithm.
It is a detailed description of instructions that a computer must follow
in a particular order.
It is intended for human reading and cannot be executed directly by
the computer.
The word “pseudo” means “not real,” so “pseudocode” means “not
real code”.
PSEUDOCODE
Some frequently used keywords while writing Pseudocode are:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
WRITE AN ALGORITHM , FLOWCHART AN PSEUDOCODE
TO FIND THE SQUARE OF A NUMBER
Step 1: Get a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Display square

INPUT num
COMPUTE square = num*num
PRINT square
WRITE AN ALGORITHM TO DISPLAY THE SUM OF TWO
NUMBERS ENTERED BY USER, USING BOTH PSEUDOCODE
AND FLOWCHART.
Step 1: Get two numbers and store it to num1 &num2
Step 2: Compute num1 + num2 and store it in Result
Step 3: Display Result

INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
WRITE AN ALGORITHM TO CALCULATE AREA AND PERIMETER
OF A RECTANGLE, USING BOTH PSEUDOCODE AND
FLOWCHART.
Step 1: Get length and breadth and store
Step 2: Compute Area=length * breadth
Step3: Compute Perimeter= 2 * (length + breadth)
Step 4: Display Area and Perimeter

INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
print Perim
Objectives
❖define and use the
three constructs for
FLOW OF CONTROL
developing algorithms:
sequence, decision,
and repetition.
❖The flow of control depicts the flow of events.
❖The events can flow in a sequence, or on branch
based on a decision or even repeat some part for a
finite number of times.
❖The program shall be categorized into three ways:
(i) sequence
(ii) selection
(iii) iteration or looping statements.
Objectives
❖define and use the
three constructs for
developing algorithms:
SEQUENCE STATEMENTS:
sequence, decision,
and repetition.

In this program,
all the instructions
are executed one
after another.
Objectives
❖define and use the
three constructs for
WRITE AN ALGORITHM TO FIND AREA OF A
developing algorithms:
sequence, decision,
RECTANGLE.
and repetition.

Step 1: Start
Step 2: Take length and breadth of rectangle and
store them as Length and Breadth
Step 3: Multiply by Length and Breadth and store
it in area
Step 4: Print area
Step 5: Stop
Excercise 1

1. Write an algorithm, pseudocode and flowchart that


finds the average of two numbers
Objectives
❖define and use the
three constructs for
developing algorithms:
SELECTIVE STATEMENTS
sequence, decision,
and repetition.
In this, some portion of the
program is executed
based upon the conditional
test. If the conditional test
is true, compiler will
execute some part of the
program, otherwise it will
execute the other part of
the program.
Objectives
❖define and use the
three constructs for
WRITE AN ALGORITHM TO CHECK
developing algorithms:
sequence, decision,
WHETHER PERSON IS ELIGIBLE TO VOTE?
and repetition. Step 1: Start
Step 2: Take age of the person and
store it in age
Step 3: Check age value, if age >=
18 then go to step 4 else step 5
Step 4: Print “Eligible to vote” and go
to step 6
Step 5: Print “Not eligible to vote”
Step 6: Stop
Excercise 2

1. Draw a flowchart to get marks out of 100 and display


the Student’s result whether passed / Failed.
2. Write an pseudocode to check whether a number is
odd or even
WRITE A PSEUDOCODE TO CHECK
WHETHER A NUMBER IS ODD OR EVEN
PRINT "Enter the Number"
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"
Excercise 3

Write an algorithm to check whether given


number is +ve, -ve or zero.
Objectives
❖define and use the
three constructs for
WRITE AN ALGORITHM TO CHECK WHETHER
developing algorithms:
sequence, decision,
GIVEN NUMBER IS +VE, -VE OR ZERO.
and repetition.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Check n value, if n > 0 then go to step 5 else go to step
4
Step 4: Check n value, if n < 0 then go to step 6 else go to step
7
Step 5: Print “Given number is +ve” and go to step 8
Step 6: Print “Given number is -ve” and go to step 8
Step 7: Print “Given number is zero”
Step 8: Stop
Objectives
❖define and use the
three constructs for
REPETITION/ITERATIVE
developing algorithms:
sequence, decision,
STATEMENTS
and repetition.
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‟.
Objectives
❖define and use the WRITE AN ALGORITHM TO PRINT ALL
three constructs for
developing algorithms: NATURAL NUMBERS UP TO “N”.
sequence, decision,
and repetition.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Store 1 in I
Step 4: Check I value, if I<=n then go to step 5 else go to step 7
Step 5: Print I
Step 6: Increment I value by 1 and go to step 4
Step 7: Stop
Excercise 4

1. Write a pseudocode to accept 5 numbers and find


their sum and average
2. Draw the flowchart to accept numbers till the user
enters 0 and then find their average.
Objectives
❖define and use the
three constructs for WRITE PSEUDOCODE TO ACCEPT 5 NUMBERS AND
developing algorithms:
sequence, decision,
PRINT THEIR SUM AND AVERAGE
and repetition.
Step1: SET count = 0, sum = 0
Step 2: WHILE count < 5 , REPEAT steps 3 to 5
Step 3: INPUT num
Step 4: COMPUTE sum = sum + num
Step 5: INCREMENT count by 1
Step 6: COMPUTE average = sum/5
Step 7: PRINT sum ,average
Objectives
❖define and use the
Excercise 4
three constructs for
developing
Write an algorithm to accept numbers till the user enters 0 and
algorithms: then find their average.
sequence, decision,
and repetition.
Step 1: Start
Step 2: Take a number and store it in n.
Step 3: Store 0 in Count and sum=0
Step 4: Check n value, if n<>0 then go to step 5 else go to step 9
Step 5: sum=sum+n
Step 6: Increment Count value by 1
Step 7 : Take a number and store it in n.
Step 8: Go to step 4
Step 9: Store Average=sum/Count
Step 10: Print Average
Step 11: Stop
Excercise 4

1. Write an algorithm to determine a student’s final


grade and indicate whether he/she is passing or failing.
The final grade is calculated as the average of four
marks.
2. Write an algorithm to find the largest and sum of n
numbers.
3. Write an algorithm to convert the length in feet to
centimeter
Excercise 4

1. Write an algorithm to find the largest of n numbers


Step 1: Start
Step 2: Take a no of numbers to enter and store in n.
Step 3: Store 1 in I and large=0
Step 4: Check I value, if I<=n then go to step 5 else go to step 10
Step 5: Take a number and store it in num
Step 6: Increment I value by 1
Step 7 : check if large<num the go to step 8 else go to step 9
Step 8: store large=num
Step 9: Go to step 4
Step 10: Print large
Step 11: Stop

©Brooks/Cole, 2003
Excercise 4

Develop an algorithm to enter a mark scored by a student


in a particular exam into grades (A,B,C,D) as given
Numerical Letter Grade
condition
<40 E
>=40 and <=54 D
>=55 and <=69 C
>=70 and <=85 B
>85 A
Algorithm
Step 1: Start
Step 2: Take a marks of a student out of 100 and store in marks.
Step 3: Check if marks>85 then go to step 11 else goto step 4
Step 4: Check if marks>=70 then go to step 10 else goto step 5
Step 5: Check if marks>=55 then go to step 9 else goto step 6
Step 6: Check if marks>=40 then go to step 8 else goto step 7
Step 7: print “Grade E” and go to step 12
Step 8 : print “Grade D” and go to step 12
Step 9: print “Grade C” and go to step 12
Step 10: print “Grade B” and go to step 12
Step 11: print “Grade A”
Step 12: Stop

©Brooks/Cole, 2003
Objectives
❖solve problems by
DRAW A FLOW CHART TO FIND FACTORIAL
using flowchart
❖Define and use
OF ANY NUMBER(ITERATIVE).
the three constructs
for developing
flowchart:
sequence, decision,
and repetition.
❖understand the
finite- and infinite-
loop.
Objectives
❖solve problems by
DRAW A FLOW CHART TO FIND BIGGEST
using flowchart
❖Define and use
NUMBER AMONG “N” NUMBERS.
the three constructs
for developing
flowchart:
sequence, decision,
and repetition.
❖understand the
finite- and infinite-
loop.
Objectives
❖solve problems by
using flowchart
FINITE AND INFINITE LOOP
❖Define and use In looping statements, if some set of statements are executed “n”
the three constructs times (fixed number of times), then it is called “finite loop”.
for developing
flowchart:
At the same time, if some set of statements are executed again and
sequence, decision, again without any end (infinite times), then it is called “infinite
and repetition. loop”.
❖understand the For example if we are not incrementing “I‟ (index) value, then we
finite- and infinite- will get endless (infinite) loop. The following is an example of
loop.
infinite loop.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Store 1 in I
Step 4: Check I value, if I<=n then go to step 5 else go to step 6
Step 5: Print I and Go to step 4
Step 6: Stop
WRITE A PSEUDOCODE TO PRINT WHETHER A PERSON IS A
CHILD,TEENAGER OR ADULT BASED ON THE CRITERIA GIVEN
BELOW:
CHILD (<13) ,TEENAGER (>13 BUT <20), ADULT (>=20)
INPUT Age
IF Age < 13 THEN
PRINT "Child"
ELSE IF Age < 20 THEN
PRINT "Teenager"
ELSE
PRINT "Adult"
BENEFITS OF PSEUDOCODE
Pseudocode of a program helps in representing the basic functionality of
the intended program.
Pseudocode helps to review the steps to confirm that the proposed
implementation is going to achieve the desire output.
VERIFYING ALGORITHM
Verification means to ensure that the algorithm is working as intended.
The software designer should make sure that the functioning of all the components are
defined correctly, checked and verified in every possible way.
We can check this for small numbers , for which we can manually calculate and verify
the output.
The mechanism of verifying algorithm is called Dry-Run.
A dry run is the process of a programmer manually working through their code to
trace the value of variables. (with pen and paper)
CHARACTERISTICS OF A DRY RUN
It is carried out during design, implementation, testing or maintenance.
It is used to identify logical errors in a code.
TRACE TABLES
Dry running an algorithm is carried out using trace
tables where the impact of each line of the code is
seen on the values of the variables of the algorithm.
Example
Step1: INPUT num num count num<500
Step 2: SET count=0 16 0 True
32 1 True
Step3: WHILE num<500 REPEAT steps 4 to5
64 2 True
Step 4: COMPUTE num= num*2 128 3 True
Step 5: INCREMENT count 256 4 True
Step 6: PRINT num, count 512 5 False
WRITE PSEUDOCODE TO PRINT EVEN NUMBERS FROM 1
TO N AND DRY RUN THE CODE USING TRACE TABLES
Step 1:PRINT “ enter n” n Num If num<=n num%2==0 Re
sul
Step 2: INPUT n t
Step 3: SET num=1 6 1 True False
Step 4: WHILE num<=n REPAET steps 5 to 7 2 True True 2
3 True False
Step 5:IF num MOD 2==0 THEN step6 ELSE step7
4 True True 4
Step 6: PRINT num 5 True False
Step 7: INCREMENT num 6 True True 6
7 False
COMPARISON OF ALGORITHM
There can be more than one approach to solve a problem
using computer and hence we can have more than one
algorithm.
Algorithms can be compared and analysed on the basis of
Time-wise efficiency- the amount of processing time they
need to run
Space-wise efficiency - the amount of memory that is
needed to execute.
COMPARISON OF ALGORITHM
There can be different ways to write algorithms to check whether a given number is
prime or not as shown below:
(i) Starting with divisor 2, divide the given number (dividend) and check if there are
any factors. Increase the divisor in each iteration and repeat the previous steps as
long as divisor < dividend. If there is a factor, then the given number is not prime
(ii) In (i), instead of testing all the numbers till the dividend, only test up to half of the
given value (dividend) because the divisor can not be more than half of the dividend
(iii) In method (i), only test up to the square root of the dividend (numbers)
iv) Given a prior list of prime number till 100, divide the given number by each
number in the list. If not divisible by any number, then the number is a prime else it is
not prime
COMPARISON OF ALGORITHM
i) More processing time. If given no is large it takes more
time to give the output.
ii) is more efficient than i) as it checks for the divisibility till
half the number and thus it reduces the time for
computation.
iii) is even more efficient as it checks for divisibility till the
square root of the number.
iv) use only prime number which is smaller than the given
number for divisibility, it further reduces the calculation.
CODING
Once an algorithm is finalized, it should be coded in a high-level
programming language as selected by the programmer.
Programs written using binary digits are directly understood by the
computer hardware, but they are difficult to deal with and
comprehend by humans. This led to the invention of high-level
languages which are close to natural languages and are easier to
read, write, and maintain, but are not directly understood by the
computer hardware
Example for High Level Languages are
Python, C, C++, Java, Fortran
A program written in a high-level language is called source code. We need to
translate the source code into machine language using a compiler or an
interpreter, so that it can be understood by the computer.
CHARACTERISTICS OF A GOOD PROGRAM
❖Precision — the steps are precisely stated or 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 always stops after a finite number of steps.

❖Input — the algorithm receives some input.

❖Output — the algorithm produces some output.


DECOMPOSITION
Sometimes a problem may be complex, that is, its solution is not directly
derivable. In such cases, we need to decompose it into simpler parts.
The basic idea of solving a complex problem by decomposition is to
'decompose' or break down a complex problem into smaller sub
problems
These sub problems are relatively easier to solve than the original
problem. Finally, the subproblems are combined in a logical way to
obtain the solution for the bigger, main problem.
Each sub problem can be solved independently and by different persons
(or teams). Having different teams working on different sub problems
can also be advantageous because specific sub problems can be
assigned to teams who are experts in solving such problems.
QUESTIONS
1. Write an algorithm, flowchart and pseudocode to find the area of a
triangle.
3. Write an algorithm, flowchart and pseudocode to find the sum of all
even number up to given number.
4. Write an algorithm, flowchart and pseudocode to find the area of a
circle.
5. Write an algorithm, flowchart and pseudocode to find the smallest
number among n numbers.
6. Write an algorithm, flowchart and pseudocode to find the sum of all
multiples of 5 up to given number.
7. Write an algorithm, flowchart and pseudocode to find sum of n numbers.

You might also like