Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

ALGORITHMS AND

FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
■ A typical programming task can be divided into
two phases:
■ Problem solving phase
◻ produce an ordered sequence of steps that describe
solution of problem
◻ this sequence of steps is called an algorithm
■ Implementation phase
◻ implement the program in some programming
language
Steps in Problem Solving
■ First produce a general algorithm (one can use
pseudocode)
■ Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.

◻ Pseudocode is an artificial and informal language


that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
Pseudocode & Algorithm
■ Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.
Pseudocode & Algorithm
Pseudocode:
■ Input a set of 4 marks
■ Calculate their average by summing and dividing
by 4
■ if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
■ Detailed Algorithm
■ Step 1: Input M1,M2,M3,M4
Step 2: GRADE ← (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
ALGORITHM EXAMPLES
Let us take one simple day-to-day example by writing algorithm for
making, “Maggi Noodles‟ as a food.

Step 1: Start
Step 2: Take pan with water
Step 3: Put pan on the burner
Step 4: Switch on the gas/burner
Step 5: Put magi and masala
Step 6: Give two minutes to boil
Step 7: Take off the pan
Step 8: Take out the magi with the help of
fork/spoon
Step 9: Put the maggi on the plate and
serve it
Step 10: Stop.
ALGORITHM EXAMPLES

Write an algorithm to print “Good


Morning‟

Step 1: Start
Step 2: Print “Good Morning‟
Step 3: Stop
ALGORITHM EXAMPLES

Write an algorithm to find area of a rectangle.

Step 1: Start
Step 2: Input length and breadth and
store them as L and B?
Step 3: Multiply by L and B and store it
in area
Step 4: Print area
Step 5: Stop
ALGORITHM EXAMPLES
Write an algorithm to check whether he is eligible
to vote? (more than or equal to 18 years old).
Step 1: Start
Step 2: Input age 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
ALGORITHM EXAMPLES
Write an algorithm to check whether given number is +ve, -ve
or zero.
Step 1: Start
Step 2: Input 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
The Flowchart
■ A graphical representation of the sequence of operations in
an information system or program.

◻ Information system flowcharts show how data flows from source


documents through the computer to final distribution to users.
◻ Program flowcharts show the sequence of instructions in a single
program or subroutine. Different symbols are used to draw each
type of flowchart.
The Flowchart
The flowchart is a diagram which visually presents the flow
of data through processingsystems. This means by
seeing a flow chart one can know the operations performe
d andthe sequence of these operations in a system.
Algorithms are nothing but sequence of
steps for solving problems. So a flow chart can be used for
representing an
algorithm. Flowchart, will describe the operations (and in w
hat sequence) are required to solve a given problem.
The Flowchart

A flowchart is a type of diagram that represents


an algorithm, workflow or process. The flowchart
shows the steps as boxes of various kinds, and
their order by connecting the boxes with arrows.
This diagrammatic representation illustrates a
solution model to a given problem. Flowcharts are
used in analyzing, designing, documenting or
managing a process or program in various fields.
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE ← (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE←(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
Example 2
■ Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
■ Input the length in feet (Lft)
■ Calculate the length in cm (Lcm) by
multiplying LFT with 30
■ Print length in cm (LCM)
Example 2
Flowchart
Algorithm START

■ Step 1: Input Lft


Input

■ Step 2: Lcm ← Lft x 30


Lft

■ Step 3: Print Lcm Lcm ← Lft x 30

Print
Lcm

STOP
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
■ Input the width (W) and Length (L) of a rectangle
■ Calculate the area (A) by multiplying L with W
■ Print A
Example 3
Algorithm START

■ Step 1: Input W,L Input


W, L
■ Step 2: A ← L x W
■ Step 3: Print A A←LxW

Print
A

STOP
Flowcharts
■ Flowcharts is a graph used to depict or
show a step by step solution using
symbols which represent a task.
■ The symbols used consist of geometrical
shapes that are connected by flow lines.
■ It is an alternative to pseudocoding;
whereas a pseudocode description is
verbal, a flowchart is graphical in nature.
Principles of Programming - NI July
2005 29
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.

Process symbol - shows an instruction other than


input, output or selection.

Input-output symbol - shows an input or an output


operation.

Disk storage I/O symbol - indicates input from or output to


disk storage.

Printer output symbol - shows hardcopy printer


output.

Principles of Programming - NI July


2005 30
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.

Off-page connector - provides continuation of a


logical path on another page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.

Principles of Programming - NI July


2005 31
Flowchart – sequence control structure

Statement 1

Statement 2

Statement 3

Principles of Programming - NI July


2005 32
Flowchart – selection control structure

No Yes
Condition

else- then-
statement(s) statement(s)

Principles of Programming - NI July


2005 33
Flowchart – repetition control structure

yes Loop
Condition
Statement(s)

no

Principles of Programming - NI July


2005 34
Flowchart – example 1
Begin

Read birth date

Calculate
Age = current year – birth date

Display
age

End

Principles of Programming - NI July


2005 35
Flowchart – example 2
Begin

Read age

YES Age > 55? NO

print “Pencen” print “Kerja lagi”

End

Principles of Programming - NI July


2005 36
Flowchart – example 5
Begin

sum = 0
current_number = 1

NO
current_number <= 10? print sum

YES
sum = sum + current_number End
current_number = current_number +
1

Principles of Programming - NI July


2005 37
Exercises: Algorithm &
Flowchart
Create an algorithm and a flowchart that
will compute the sum of two numbers. If
the sum is below or equal to twenty, two
numbers will be entered again. If the sum
is above 20, it will display the sum.
Lab Activity: Algorithm &
Flowchart
Create an algorithm and a flowchart that
will output the largest number among the
three numbers.
Do It…..

1. Create an algorithm and a flowchart that will


output for g.c.d.
2. Create an algorithm and a flowchart that will
output the factorial of a given number.
3. Create an algorithm and a flowchart that will
output the Fibonacci series up to a given number.
4. Create an algorithm and a flowchart that will
output all the prime numbers between 2 numbers.
Structured programming concept, programs -
compiler, interpreter.
Structured programming concept
It refers to a programming strategy that
encompasses a number of methodologies to
produce good quality design and code,
which can be easily understood, tested,
debugged, modified and maintained in future
There are three main principles
of structured programming
1. Program design using top-down or
bottom-up approach
2. Decomposition of program into
components, ie., modular programming
3. Structuring of control flow
Program Design
Program design concentrates on planning the
solution as a collection of sub- solution.
It can be done in two ways-
■ top down design
■ bottom-up design.
Top- Down Design
Computer programmers use a divide and
conquer approach to problem solving:
a problem is broken into parts

■ those parts are solved individually


■ the smaller solutions are assembled into a big
solution
■ These techniques are known as top-down
design and modular development.
Bottom- up Design
The bottom-up design is the reverse of the
top-down approach

The process starts with the identification of the


smallest sub-components of the total program,
which can be easily implemented.
Such smallest components are combined to
reach to a more abstract level and plan
components of the higher level. This process is
continued till the complete program does not
get realized
Modular Programming -
Decomposition of Program into components
Modular programming is a general programming
concept where developers separate program
functions into independent pieces. These pieces
then act like building blocks, with each block
containing all the necessary parts to execute one
aspect of functionality.
the modular programming principle suggests
writing the code as a collection of many
modules
A module is a portion of the program which
is responsible to carry out a certain work,
and can be used with other modules
A module may be considered as
decomposed into subordinate modules and
many modules can be combined also to
form a superior module.
A module can be repeatedly called, with
different input values and can also be
reused in other programs with little/as
modifications.
All the modules are developed separately
and the superior module can call the
subordinate modules. This process is known
as "calling' of subordinate module by
superior module. The superior module is
called the calling module and the
subordinate module is called the called
module.
Structuring of Control Flow
The traditional meaning of structured
programming is taken in following a systematic
flow of control throughout a program.

The idea behind structuring the flow of control


is to reduce the complexity, which gets
increased due to abrupt control transfers,
interconnections and cross connections among
various parts of a program
It strongly advocates the principle of single
entry and single exit to all sub-parts of a
program.

Single entry means that there should be only


one way to enter into a sub-part (module,
components or a control statement), and
similarly there should be only one exit from
the sub-part.
The advantage is that once the control
enters the sub-part, it executes the
statements within the sub-part until it
reaches the exit point.
This can be achieved by implementing all
processing requirements using combinations
of three basic control flows:

1. Sequence (steps one after the other)


2. Selection (choose one out of two/many)
3. Iteration (repeat steps many times)
Compiler and Interpreter

We generally write a computer program using high level

programming language. A high level language is understandable by

human and this high level language is called source code.

The high level language is not understandable by computer or

machine. It only understands the program which is written in 0’s

and 1’s in binary which is known as machine code.


To convert the high level language or source code into machine

code, we use compiler or interpreter.

The compiler and interpreter and compiler both are used for

convert a program in high level language to low level or machine

programming language.So, we can say compiler and interpreter

are converter which convey source code to machine code.


Both of them do same work but there have some difference between

compiler and interpreter.


Lvalues and Rvalues in C
There are two kinds of expressions in C −

lvalue − Expressions that refer to a memory location are called


"lvalue" expressions. An lvalue may appear as either the
left-hand or right-hand side of an assignment.

rvalue − The term rvalue refers to a data value that is stored


at some address in memory. An rvalue is an expression that
cannot have a value assigned to it which means an rvalue may
appear on the right-hand side but not on the left-hand side of
an assignment.
Examples of lvalues:
● Variables: int x;
● Array elements: arr[3]
● Pointers pointing to variables: int *ptr = &x;
● Pre-decrement/post-decrement expressions: --x, x++
Examples of rvalues:
● Constants: 5, 'A'
● Arithmetic expressions: 3 + 4, x * 2
● Function call results: func()
● Temporary values: x + y (temporary result of the
addition)
Array
Arrays are fundamental data structures in
programming. They allow us to store multiple
elements of the same data type under a single
variable name.
Why do we need an array?

Let’s consider the situation where we need to get 10 student’s age and store
it for some calculation.
Since age is an integer type, we can store it something like below,

int ag1, age2, age3, age4, age5, age6,


age7, age8, age9, age10;
If more number of student joins, then it is very difficult to declare
a lot of variables and keep track of it.
To overcome this kind of situation, we should use Array data
structure.
we can’t group different data types in array.
Like, combination of integer and char, char and float etc.
Hence Array is called as homogeneous data type.
What are Arrays?
An array is a collection of elements, such as integers or
characters, grouped under a single name. Each element
in an array is accessed using an index. Here's a simple
diagram illustrating the concept:
Declaring and Initializing Arrays
In C, arrays are declared by specifying the data type
followed by the array name and its size in square
brackets. You can also initialize arrays during
declaration.
Syntax of Array Declaration

data_type array_name [size];

or

data_type array_name [size1] [size2]...[sizeN];


In this example:

●We declare an integer array named numbers with a size of


5. This means the array can hold 5 integers.
● We initialize each element of the array with values 10, 20,
30, 40, and 50 using index notation.
● We use printf to print the values of each array element by
accessing them with their respective indices.
Declaration and Initialization with Explicit Values
String Initialization
Symbolic constants

Symbolic constants, also known as "constants


with names," are identifiers in C that represent
constant values. They are typically used to
improve code readability, maintainability, and to
avoid "magic numbers" (literal constants without
explanation) in the code. Symbolic constants are
defined using the #define preprocessor directive
or, in modern C, using the const keyword.
Using #define Preprocessor Directive:

The #define directive defines a symbolic constant. It


instructs the preprocessor to replace occurrences of the
defined constant with its corresponding value throughout
the code.
Using const Keyword:

Modern C supports the use of the const keyword to


define constants with names. These constants have
type information and are subject to type-checking.
Advantages of using symbolic constants

● Improved code readability: Constants with meaningful


names make the code more understandable.
● Easy maintenance: Changing the value of a constant is
done in one place, making updates easier.
● Avoiding magic numbers: By giving a name to a
constant value, you make it clear what the value
represents.

You might also like