0% found this document useful (0 votes)
92 views35 pages

PDF Maker 1734379673925

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)
92 views35 pages

PDF Maker 1734379673925

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/ 35

Chapter 2

Algorithm representation and


Data structure
Introduction
• A computer is a useful tool for solving a great
variety of problems.
• To make a computer do anything (i.e. solve a
problem), you have to write a computer program.
• In a computer program you tell a computer, step
by step, exactly what you want it to do.
• The computer then executes the program,
following each step automatically, to accomplish
the end goal.
Problem solving techniques
Problem solving is the process of transforming the
description of a problem into the solution by using
knowledge of the problem domain and appropriate
problem-solving strategies, techniques, and tools.
There are two approaches of problem solving:
i. Top down
ii. Bottom up
What is Algorithm?
• An algorithm is a step-by-step procedure or set of
instructions for solving a problem or accomplishing a task.
• Is a sequence of well-defined computational steps that
transform input data into the desired output.
• Algorithms can be represented in various forms, including
natural language descriptions, flowcharts, pseudocode, or
programming code.
• Algorithms are used in a wide range of applications, from
simple everyday tasks like finding the shortest route
between two points on a map to complex computational
problems like sorting large datasets or optimizing resource
allocation in computer networks.
Characteristics of a good algorithm

Output unambiguity
6 1
Input Characteristics of Flexibility
5 Algorithm
2
Language
Finiteness
4 3 independent
Guidelines for Developing an Algorithm:

 An algorithm will be enclosed by START (or BEGIN) and STOP


(or END)
 To accept data from the user, generally used statements are INPUT,
READ, GET or OBTAIN
 To display the result or any message, generally used statements are
PRINT, DISPLAY, SHOW or WRITE
 Generally, COMPUTE or CALCULATE is used while describing
mathematical expressions, and based on the situation relevant
operators can be used.
Algorithm Examples
1. Algorithm: Calculation of Simple Interest
• Step 1: Start
• Step 2: Read principal (P), time (T), and rate (R)
• Step 3: Calculate I = P*T*R/100
• Step 4: Print I as Interest
• Step 5: Stop
2. Algorithm to compute square of a number
• Step 1: Start
• Step 2: Read the Number
• Step 3: Compute the square = Number * Number
• Step 4: Print square
• Step 5: Stop

3. Find the average of three numbers.


Step 1: start
Step 2: read values of x, y, z
Step 3: s = q + y+ z
Step 4: A = s/3
Step 5: write value of A
Step 6: stop
Program Development Cycle:

1. Analyze: Define the problem


2. Design: Plan the solution to the problem
3. Choose the Interface: Select the objects
4. Code: Translate the algorithm into a programming language.
5. Debug and Test: Locate and remove any errors in the program.
6. Complete the Documentation: Organize all the materials that
describe the program.
Basic program development tips
The program we design in any programming language need to
be:
 Reliable: the program should always do what it is expected
to do and handle all types of exception.
 Maintainable: the program should be in a way that it could
be modified and upgraded when the need arises.
 Portable: It needs to be possible to adapt the software
written for one type of computer to another with minimum
modification.
 Efficient: the program should be designed to make optimal
use of time, space and other resources of the computer.
Introduction to flow chart and pseudo code
• The sequence of steps to be performed in order to
solve a problem by the computer is known as an
algorithm.
• Algorithms can be expressed in many different
notations, including natural languages, pseudocode,
flowcharts and programming languages.
• Natural language expressions of algorithms tend to
be verbose and ambiguous, and are rarely used for
complex or technical algorithms
‘’’cont
• Pseudocode and flowcharts are structured ways to
express algorithms that avoid many ambiguities
common in natural language statements, while
remaining independent of a particular implementation
language.
• Programming languages are primarily intended for
expressing algorithms in a form that can be executed
by a computer, but are often used to define or
document algorithms.
Flowchart
• Flowchart is a graphical or symbolic
representation of an algorithm or process.
• It is the diagrammatic representation of the step-
by-step solution to a given problem.
• It describes what operations (and in what
sequence) are required to solve a given problem.
• It doesn’t depend on any particular programming
language.
• Flowchart uses different symbols (geometrical
shapes) to represent different processes
Purpose of Flowcharting:
 An aid in developing the logic of a program.
 Verification that all possible conditions have been
considered in a program.
 Provides means of communication with others about the
program.
 A guide in coding the program.
 Documentation for the program.
Example 1: - Draw flow chart of an algorithm to
add two numbers and display their result.
• Algorithm description:-
– Read the two numbers (A and B)
– Add A and B
– Assign the sum of A and B to C
– Display the result ( c)
• Example 2:- Write an algorithm description
and draw a flow chart to check a number is
negative or not.
• Algorithm description:-
– Read a number x
– If x is less than zero write a message negative
– else write a message not negative
• Example 3: - Write the algorithmic description
and draw a flow chart to find the following sum.
Sum = 1+2+3+…. + 50
Algorithmic description:-
1. Initialize sum to 0 and counter to 1
1.1. If the counter is less than or equal to 50
– Add counter to sum
– Increase counter by 1
– Repeat step 1.1
1.2. Else
• Exit
2. Write sum
Pseudocode
• Derived from pseudo and code is a compact and
informal high-level description of a computer
algorithm that uses the structural conventions of
programming.
• It is easy for humans to read
• No standard for pseudocode syntax exists
• It is not an executable program
• Writing it will save you time later during the
construction & testing phase of a program's
development.
Rules used to write pseudocode
1. Write only one statement per line
Each statement in your pseudocode should express just one action
for the computer.
2. Capitalize initial keyword
We will use just a few keywords:
• INPUT/READ – indicates a user will be inputting something.
• OUTPUT/WRITE – indicates that an output will appear on the
screen.
• WHILE – a loop (iteration that has a condition at the beginning)
• FOR – a counting loop (iteration)
• REPEAT – UNTIL – a loop (iteration) that has a condition at the
end
• IF – THEN – ELSE – a decision (selection) in which a choice is
made any instructions that occur inside a selection or iteration
are usually indented.
3. Indent to show hierarchy
We will use a particular indentation pattern in each of the design
structures:
Sequence: keep statements that are “stacked” in sequence all
starting in the same column.
Selection: indent the statements that fall inside the selection
structure, but not the keywords that form the selection
Looping: ident the statements that fall inside the loop, but not the
keywords that form the loop.
Pseudo code examples
Pseudocode Example
: Issue for driver license
BEGIN
NUMBER age
OUTPUT "Enter your age for driving licence"
INPUT age
IF age>=16 THEN
OUTPUT "You can take driving licence"
ELSE
OUTPUT "You can't take driving licence"
ENDIF
END
4. End multiline structures
See how the IF/ELSE/ENDIF is constructed above. The ENDIF is always in
line with the IF.
Always use scope terminators for loops and iteration.
Pseudocode Example :
Print Numbers from 1 to 100.
BEGIN
NUMBER counter
FOR counter = 1 TO 100 STEP 1 DO
OUTPUT counter 5. Keep statements language independent
ENDFOR There may be special features available in the
END language you plan to eventually write the
program in; if you are sure it will be written in
that language, then you can use the features. If
not, then avoid using the special features.
E.g. Draw a flowchart and write a pseudocode to find the
greater number between two numbers.
Solution
Pseudocode Flow Chart
BEGIN
NUMBER A, B
INPUT A
INPUT B
IF A > B THEN
OUTPUT A+ "is higher"
ELSE
OUTPUT B+ "is higher"
ENDIF
END
• Example:-
• Write a program that obtains two integer
numbers from the user. It will print out the
sum of those numbers.
• Pseudocode:
– Prompt the user to enter the first integer
– Prompt the user to enter a second integer
– Compute the sum of the two user inputs
– Display an output that explains the answer as the
sum
– Display the result
Translating algorithms to programming languages

• The sequence of steps to be performed in order


to solve a problem by the computer is known
as an algorithm.
• A good algorithm is like using the right tool in
the workshop.
• Three reasons for using algorithms are
– Efficiency:- time and cost factor involved
– Abstraction:- for complicated problems
– Reusability:- in many different situations.
• E.g. Find the largest number in an unsorted list
of numbers.
• Algorithm by natural language:
– a) Assume the first item is largest.
– b) Look at each of the remaining items in the list
and if it is larger than the largest item so far, make
a note of it.
– c) The last noted item is the largest item in the list
when the process is complete.
Definition of Data Structures
• Data Structures are the programmatic way of
storing data so that data can be used
efficiently.
• Almost every enterprise application uses
various types of data structures in one or the
other way.
• Data Structure is a systematic way to organize
data in order to use it efficiently.
• Following terms are the foundation terms of a data
structure.
• Interface − Each data structure has an interface.
– Interface represents the set of operations that a data
structure supports.
– An interface only provides the list of supported operations,
type of parameters they can accept and return type of these
operations.
• Implementation − Implementation provides the
internal representation of a data structure.
– Implementation also provides the definition of the
algorithms used in the operations of the data structure.
Role of data structures in writing programs

• Correctness − Data structure implementation


should implement its interface correctly.
• Time Complexity − Running time or the
execution time of operations of data structure
must be as small as possible.
• Space Complexity − Memory usage of a data
structure operation should be as little as
possible.
Algorithms Vs Data Structure

• From the data structure point of view, following are


some important categories of algorithms −
• Search − Algorithm to search an item in a data
structure.
• Sort − Algorithm to sort items in a certain order.
• Insert − Algorithm to insert item in a data structure.
• Update − Algorithm to update an existing item in a
data structure.
• Delete − Algorithm to delete an existing item from a
data structure.
Algorithms Vs Data Structure

An algorithm should have the following characteristics


• Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
clear and must lead to only one meaning.
• Input − An algorithm should have 0 or more well-defined
inputs.
• Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of
steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step
directions, which should be independent of any programming
code
W O
R T
P T E
C HA
O F
EN D
Quiz (5%) do it within 20 Min
1. Write an algorithm to determine whether a
given integer is a prime number or not
2. Write an algorithm to calculate the factorial
of a non-negative integer n
3. Write an algorithm to find the maximum
number in a given list of integers
4. Draw a flow chart for question number 1

You might also like