0% found this document useful (0 votes)
34 views

Unit 1

Uploaded by

Sabarigiri Vason
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Unit 1

Uploaded by

Sabarigiri Vason
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT 1 INTRODUCTION TO COMPUTER PROBLEM SOLVING

Algorithms, building blocks of algorithms (statements, control flow, functions), notation (pseudo code, flow chart),
algorithmic problem solving for socio economic conditions in global perspectives, simple strategies for developing
algorithms (iteration, recursion), the efficiency of algorithms.

1.1 Algorithms

An algorithm is a step by step procedure of solving a problem.


The word “algorithm” is derived from the name of the 9 th century Persian mathematician AlKhwarizmi.
An algorithm is defined as a collection of unambiguous instructions which are executed in a specific
sequence to produce an output in a finite amount of time for a given set of input data.
An algorithm can be written in English or in any standard representation.

Properties OF Algorithm:
1. Finiteness: An algorithm must be executed finite number of times. The algorithm must stop,
eventually.Stopping may mean that you get the expected output OR you get a response that no
solution is possible. Algorithms must terminate after a finite number of steps.An algorithm should
not be infinite and always terminate after definite number of steps.

There is no point in developing an algorithm which is infinite as it will be useless for us.

2. Definiteness: Each step of the algorithm must be accurate and clear. Algorithms must specify
every step and the order the steps must be taken in the process.Definiteness means specifying the
sequence of operations for turning input into output. Algorithm should be clear and
unambiguous.Details of each step must be also be spelled out (including how to handle errors).It
should contain everything quantitative and not qualitative.
3. Effectiveness: Each step must be effective, in the sense that it should be primitive (easily
convertible into a program statement) and can be performed exactly in a finite amount of time.
4. Independent: The algorithm must be independent of any programming code.
5. Input/output: Each algorithm must take zero or more quantities as input data and give out
one or more output values.

Methods for Developing an Algorithm :

List the data needed to solve the problem (input) and know what is the end result (output).
Describe the various step to process the input to get the desired output.
Break down the complex processes into simpler statements.
Finally test the algorithm with different data sets.

Qualities of a good algorithm


The following are the primary factors that are often used to judge the quality of the
algorithms.
Time – To execute a program, the computer system takes some amount of time. The lesser
is the time required, the better is the algorithm.
Memory – To execute a program, computer system takes some amount of memory space.
The lesser is the memory required, the better is the algorithm.
Accuracy – Multiple algorithms may provide suitable or correct solutions to a
given problem, some of these may provide more accurate results than others, and such
algorithms may be suitable.

Advantage and Disadvantages of Algorithms


Advantages of Algorithms:

 Efficiency: Algorithms streamline processes, leading to faster and more optimized solutions.
 Reproducibility: They yield consistent results when provided with the same inputs.
 Problem-solving: Algorithms offer systematic approaches to tackle complex problems
effectively.
 Scalability: Many algorithms can handle larger datasets and scale with increasing input sizes.
 Automation: They enable automation of tasks, reducing the need for manual intervention.

Disadvantages of Algorithms:

 Complexity: Developing sophisticated algorithms can be challenging and time-consuming.


 Limitations: Some problems may not have efficient algorithms, leading to suboptimal
solutions.
 Resource Intensive: Certain algorithms may require significant computational resources.
 Inaccuracy: Inappropriate algorithm design or implementation can result in incorrect outputs.
 Maintenance: As technology evolves, algorithms may require updates to stay relevant and
effective.

Example: Algorithm for Addition of two numbers:


Step1: Start
Step 2: Get two numbers a and b as input
Step 3: Add the numbers a & b and store the result in c
Step 4: Print c
Step 5: Stop.

BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow,


functions)

Algorithms can be constructed from basic building blocks namely, sequence, selection and
iteration.

Statements:

Statement is a single action in a computer.

In a computer statements might include some of the following actions


 input data-information given to the program
 process data-perform operation on a given input
 output data-processed result

State:
Transition from one process to another process under specified condition with in a time is
called state.

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. sequence
2. selection
3. iteration

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
Step 5: Stop

Selection:
A selection statement causes the program control to be transferred to a specific part of the
program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.

Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop

Iteration:
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.

Example

Write an algorithm to print all natural numbers up to n


Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop

Functions:

 Function is a sub program which consists of block of code(set of instructions) that


performs a particular task.
 For complex problems, the problem is been divided into smaller and simpler tasks during
algorithm design.

Benefits of Using Functions

 Reduction in line of code


 code reuse
 Better readability
 Information hiding
 Easy to debug and test
 Improved maintainability

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
2.NOTATIONS OF AN ALGORITHM
Algorithm can be expressed in many different notations, including Natural Language, Pseudo code, flowcharts
and programming languages. Natural language tends to be verbose and ambiguous. Pseudocode and
flowcharts are represented through structured human language.

Pseudocode:
Pseudocode is an informal high-level description of the operating principle of a computer program or algorithm.
It uses the basic structure of a normal programming language, but is intended for human reading rather than
machine reading.

It is text based detail design tool. Pseudo means false and code refers to instructions written in programming
language.

Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. The
pseudocode is written in normal English language which cannot be understood by the computer.

Basic rules to write pseudocode:


1. Only one statement per line. Statements represents single action is written on same line. For example to read
the input, all the inputs must be read using single statement.

2. Capitalized initial keywords The keywords should be written in capital letters.

Eg: READ, WRITE, IF, ELSE, ENDIF, WHILE, REPEAT, UNTIL

Example:

Pseudocode: Find the total and average of three subjects

RAED name, department, mark1, mark2, mark3

Total=mark1+mark2+mark3

Average=Total/3

PRINT name, department,mark1, mark2, mark3 ,Average

3. Indent to show hierarchy

Indentation is a process of showing the boundaries of the structure.

4. End multi-line structures Each structure must be ended properly, which provides more clarity.

Example:

Pseudocode: Find greatest of two numbers

READ a, b
IF a>b then

PRINT a is greater

ELSE PRINT b is greater

ENDIF

5. Keep statements language independent.

Pesudocode must never written or use any syntax of any programming language

Advantages of Pseudocode
• Can be done easily on a word processor

• Easily modified

• Implements structured concepts well

• It can be written easily

• It can be read and understood easily

• Converting pseudocode to programming language is easy as compared with flowchart

Disadvantages:
 It is not visual

 We do not get a picture of the design.

 There is no standardized style or format.

 For a beginner, it is more difficult to follow the logic or write pseudo code as compared to flowchart.

Common keywords used in writing a Pseudo code


Comment: //

Start: BEGIN

Stop: END

Input: INPUT, GET, READ

Calculate: COMPUTE, CALCULATE, ADD, SUBTRACT, INITIALIZE

Output: OUTPUT, PRINT, DISPLAY

Selection: IF, ELSE, ENDIF

Iteration: WHILE, ENDWHILE, FOR, ENDFOR


Example: Pseudo code to Add two numbers
BEGIN

GET a, b

ADD c=a+b

PRINT c

END

Flowchart:
A graphical representation of an algorithm.

Flowcharts is a diagram made up of boxes, diamonds, and other shapes, connected by arrows.

Each shape represents a step in process and arrows show the order in which they occur.

Rules for drawing a flowchart


1. The flowchart should be clear, neat and easy to follow.
2. The flowchart must have a logical start and finish.
3. Only one flow line should come out from a process symbol.
4. Only one flow line should enter a decision symbol. However, two
or three flow lines may leave the decision symbol.

5. Only one flow line is used with a terminal symbol.

6. Within standard symbols, write briefly and precisely.


7. Intersection of flow lines should be avoided.

Advantages of Flowchart
Communication:

Flowcharts are better way of communicating the logic of the system.

Effective Analysis

With the help of flowchart, a problem can be analyzed in more effective way. Proper Documentation
Flowcharts are used for good program documentation, which is needed for various purposes.

Efficient Coding

The flowcharts act as a guide or blue print during the system analysis and program development phase.

Systematic Testing and Debugging

The flowchart helps in testing and debugging the program

Efficient Program Maintenance

The maintenance of operating program becomes easy with the help of flowchart

It helps the programmer to put efforts more efficiently on that part.

Disadvantages of Flowchart
Complex Logic: Sometimes, the program logic is quite complicated. In that case flowchart becomes complex and difficult
to use.

Alteration and Modification:

If alterations are required the flowchart may require redrawing completely.


Reproduction:

As the flowchart symbols cannot be typed, reproduction becomes problematic.

Algorithm Flowchart Pseudo code


An algorithm is a It is a graphical It is a language
sequence representation of representatio
of instructions algorithm n of
used to solve a algorithm.
problem
User needs not need Not need
knowledge to write knowledge of knowledge of
algorithm. program to draw program language
or understand to understand or
flowchart write a
pseudo code.

4. ALGORITHMIC PROBLEM SOLVING:


Algorithmic problem solving is solving problem that require the
formulation of an algorithm for the solution.

Understanding the Problem


❖ It is the process of finding the input of the problem that the algorithm
solves.
❖ It is very important to specify exactly the set of inputs the algorithm
needs to handle.
❖ A correct algorithm is not one that works most of the time, but one
that works correctly for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device

❖ If the instructions are executed one after another, it is called


sequential algorithm.If the instructions are executed concurrently,
it is called parallel algorithm.
Choosing between Exact and Approximate Problem Solving
❖ The next principal decision is to choose between solving the
problem exactly or solving it approximately.
❖ Based on this, the algorithms are classified as exact algorithm and
approximation algorithm.

Deciding a data structure:


❖ Data structure plays a vital role in designing and analysis the
algorithms.
❖ Some of the algorithm design techniques also depend on the
structuring data specifying a problem’s instance
❖ Algorithm+ Data structure=programs.

Algorithm Design Techniques


❖ An algorithm design technique (or “strategy” or “paradigm”) is a general
approach to solving problems algorithmically that is applicable to a
variety of problems from different areas of computing.
❖ Learning these techniques is of utmost importance for the following
reasons.
❖ First, they provide guidance for designing algorithms for new
problems,
❖ Second, algorithms are the cornerstone of computer science

Methods of Specifying an Algorithm


❖ Pseudocode is a mixture of a natural language and programming
language-like constructs. Pseudocode is usually more precise than
natural language, and its usage often yields more succinct
algorithm descriptions.

❖ In the earlier days of computing, the dominant vehicle for


specifying algorithms was a flowchart, a method of expressing an
algorithm by a collection of connected geometric shapes containing
descriptions of the algorithm’s steps.

❖ Programming language can be fed into an electronic computer


directly. Instead, it needs to be converted into a computer program
written in a particular computer language. We can look at such a
program as yet another way of specifying the algorithm, although it
is preferable to consider it as the algorithm’s implementation.
Proving an Algorithm’s Correctness
❖ Once an algorithm has been specified, you have to prove its
correctness. That is, you have to prove that the algorithm yields a
required result for every legitimate input in a finite amount of time.
❖ A common technique for proving correctness is to use mathematical
induction because an algorithm’s iterations provide a natural
sequence of steps needed for such proofs.
❖ It might be worth mentioning that although tracing the algorithm’s
performance for a few specific inputs can be a very worthwhile
activity, it cannot prove the algorithm’s correctness conclusively.
But in order to show that an algorithm is incorrect, you need just
one instance of its input for which the algorithm fails.
Analysing an Algorithm
1. Efficiency.
Time efficiency, indicating how fast the algorithm runs,
Space efficiency, indicating how much extra memory it uses.

2. simplicity.
❖ An algorithm should be precisely defined and investigated with
mathematical expressions.
❖ Simpler algorithms are easier to understand and easier to program.
❖ Simple algorithms usually contain fewer bugs.

Coding an Algorithm
❖ Most algorithms are destined to be ultimately implemented as
computer programs. Programming an algorithm presents both a
peril and an opportunity.
❖ A working program provides an additional opportunity in allowing
an empirical analysis of the underlying algorithm. Such an analysis
is based on timing the program on several inputs and then
analysing the results obtained.

5. SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS:


1. iterations
2. Recursions
5.1. Iterations:
A sequence of statements is executed until a specified condition is true is
called iterations.
1. for loop
2. While loop
Syntax for For: Example: Print n natural
numbers
BEGI
FOR( start-value to end-value) DO N
statement GET n
... INITIALIZE
ENDF i=1 FOR
OR (i<=n) DO
PRINT i
i=i
+1
ENDF
OR
END
Syntax for While: Example: Print n natural
numbers
BEGI
WHILE N
(condition) DO GET n
statement INITIALIZE
... i=1
ENDWHILE WHILE(i<=
n) DO
PRINT i
i=i+1
ENDWHILE
END

6.

You might also like