




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document introduces the concept of algorithms and algorithmic problem solving. It covers the characteristics of algorithms, building blocks of algorithms, algorithm notations, and examples of algorithms. the importance of algorithms in computer programming and the qualities of a good algorithm. It also discusses the analysis of algorithms and the different notations used to express them. illustrative problems to help readers understand the concept of algorithmic problem solving.
Typology: Study notes
1 / 36
This page cannot be seen from the preview
Don't miss anything!
Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation (pseudo code, flow chart, programming language), algorithmic problem solving, simple strategies for developing algorithms (iteration, recursion). Illustrative problems: find minimum in a list, insert a card in a list of sorted cards, guess an integer number in a range, Towers of Hanoi.
1.1 ALGORITHM
What is algorithm? An algorithm is a collection of well-defined, unambiguous and effectively computable instructions ,if execute it will return the proper output.
well-defined - The instructions given in an algorithm should be simple and defined well.
Unambiguous - The instructions should be clear,there should not be ambiguity.
effectively computable - The instructions should be written step by step ,which helps computer to understand the control flow.
Algorithm is defined as “a sequence of instructions designed in such a way that if the instructions are executed in the specified sequence, the desired result will be obtained”.
It is also defined as “any problem whose solution can be expressed in a list of executable instruction”.
computer.
pseudocode, flowcharts and programming languages.
resource usage, and is often practiced abstractly without the use of specific programming language or implementation.
algorithms in order to guide program design decisions.
performance varies with the data passed to it.
games, finance and robotics.
algorithm‟s performance in a formal manner.
Characteristics of algorithm
Qualities of a good algorithm
until particular condition is met.
a range of input data.
1.2 Building Blocks of Algorithms
Algorithms can be constructed from basic building blocks namely, sequence, selection and iteration.
1.2.1 Statements Statement is a single action in a computer.
In a computer statements might include some of the following actions
1.2.2 State: Transition from one process to another process under specified condition with in a time is called state.
1.2.3 Control flow:
The process of executing the individual statements in a given order is called control flow. The control can be executed in three ways
1.2.3.1 Sequence All the instructions are executed one after another is called sequence execution. Example:
Add two numbers:
Step 1 : Start Step 2 : get a,b Step 3 : calculate c=a+b Step 4 : Display c
1.2.4 Functions
a particular task.
algorithm design. Benefits of Using Functions
Example:
Algorithm for addition of two numbers using function Main function() Step 1: Start Step 2: Call the function add() Step 3: Stop
sub function add() Step 1: Function start Step 2: Get a, b Values Step 3: add c=a+b Step 4: Print c Step 5: Return
Examples of algorithm
Problem 1 : Find the area of a Circle of radius r.
Inputs to the algorithm: Radius r of the Circle. Expected output: Area of the Circle Algorithm:
Step 1 : Start Step 2 : Read input the Radius r of the Circle Step 3 : Area PIrr // calculation of area Step 4 : Print Area Step 5 : Stop
Problem2 : Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm: First num1. Second num2. Expected output: Sum of the two numbers. Algorithm :
Step 1 : Start Step 2 : Read\input the first num1. Step 3 : Read\input the second num2. Step 4: Sum num1+num2 // calculation of sum Step 5 : Print Sum Step 6 : Stop
Problem 3 : Convert temperature Fahrenheit to Celsius Inputs to the algorithm: Temperature in Fahrenheit Expected output: Temperature in Celsius Algorithm: Step 1 : Start Step 2 : Read Temperature in Fahrenheit F Step 3 : C 5/9*(F-32) Step 4 : Print Temperature in Celsius: C Step 5 : End
Problem 4: Find the largest number between A and B Inputs to the algorithm : A, B Expected output : Largest A or B
1.3 Algorithm Notations(Expressing Algorithms)
As we know that ,an algorithm is a sequence of finite instructions,often used for calculation and data processing.
Algorithms can be expressed in many kinds of notation,including
Definition:-
planning the program logic.
program. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.
Pseudocode
Flowchart
Programming Language
Rules for writing Pseudocode
Advantages
compared with converting a flowchart to programming language.
Disadvantage
compared to flowchart.
Pseudocode is made up of the following logic structure ,
Sequence Logic
performed. The logic flow of pseudocode is from top to bottom.
Pseudocode to add two numbers:
START READ a,b COMPUTE c by adding a &b PRINT c STOP
Selection Logic
paths in program logic.
Structure.
FOR i <= n, then DISPLAY i INCREMENT i END FOR END
Problem 4: Find the greater number between two numbers. BEGIN Read A, B IF A is less than B BIG = B SMALL = A ELSE BIG = A SMALL = B WRITE / DISPLAY BIG, SMALL END
Problem 5: To determine a student whether successful or fail. BEGIN READ student „s grade IF student's grade is greater than or equal to 50 Print "passed" ELSE
Print "failed“
Definitions: A flowchart is a schematic representation of an algorithm or a stepwise process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in designing or documenting a process or program.
A flow chart, or flow diagram, is a graphical representation of a process or system that details the sequencing of steps required to create output.
A flowchart is a picture of the separate steps of a process in sequential order. The benefits of flowcharts are as follows:
logic of a system to all concerned.
effective way.
documentation, which is needed for various purposes.
systems analysis and program development phase.
becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part
The Limitations of using flowcharts :
that case, flowchart becomes complex and clumsy.
may require re-drawing completely.
of flowchart becomes a problem.
done.
Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols, which are frequently required for flowcharting many computer programs.
An oval flow chart shape indicates the start or end of the process, usually containing the word “Start” or “End”.
These are the basic symbols used generally. Now, the basic guidelines for drawing a flowchart with the above symbols are that:
be listed out in logical order.
not be any room for ambiguity in understanding the flowchart.
lines can leave it to denote possible answers.
Flowchart is made up of the following logic structure ,
Sequence Logic
In a computer program or an algorithm, sequence involves simple steps which are to be executed one after the other. The steps are executed in the same order in which they are written.
Below is an example set of instructions to add two numbers and display the answer.
Flowchart to find factorial of given no
pre-test loop post-test
Problem 3 : Convert temperature Fahrenheit to Celsius.
Problem 4 : Flowchart for an algorithm which gets two numbers and prints sum of their value
Problem5 : Flowchart for the problem of printing even numbers between 0 and 99.