BCA Paper-V Unit-1
BCA Paper-V Unit-1
BCA Paper-V Unit-1
Lesson Structure
1.0 Objective
1.1 Introduction
1.2 Problem Solving Tecniques.
1.2.1 Steps for Problem-solving
1.2.3 Using computer as a Problem-solving Tool.
1.3 Algorithms
1.3.1 Definition
1.3.2 Properties of algorithm
1.3.3 Key features of algorithm
1.3.4 Analysis of algorithm
1.3.5 Analysis of algorithm complexity
1.3.6 Computational Complexity
1.3.7 The order of notation
1.4 Top Down Design
1.5 Flowcharts
1.5.1 Basic Symbols used in Flowchart Design
1.6 Summary
1.7 Questions for Exercise
1.8 Suggested Reading
1.0 Objective
5
Problem Solving
develop the algorithm which is used in the design phase of software development
process to help the programmers and users to clearly understand the solution to
the problem.
perform the analysis of algorithm efficiency;
understand the different symbols used to design flowcharts.
draw flowchart for the problem.
1.1 INTRODUCTION
RESULT
A computer cannot solve a problem on its own. One has to provide step by step
6
Problem Solving
solutions of the problem to the computer. Infact the task of problem solving is not that of the
computer. It is the programmer who has to write down the solution to the problem in terms of
simple operations which the computer can understand and execute.
7
Problem Solving
then try to work backwards to the starting point. Even a guess at the solution to the
problem may be enough to give us a foothold to start on the problem.
1.3 ALGORITHMS
The first step in the program development is to devise and describe a precise plan of
what you want the computer to do. This plan, expressed as a sequence of operations, is
called an algorithm. An algorithm is just an outline or idea behind a program.
1.3.1 Definition
The typical meaning of an algorithm is a formally defined procedure for performing
some calculation. An algorithm is a finite set of steps defining the solution of a particular
problem. It is expressed in pseudo code-something resembling C language or pascal, but
with some statements in English rather than within the programming language.
Developing an efficient algorithm requires lot of practice and skill. It must be noted that
an efficient algorithm is one which is capable of giving the solution to the problem by using
minimum resources of the system such as memory and processor’s time.
8
Problem Solving
Algorithm is a language independent, well - structured and detailed. It will enable the
programmer to translate into a computer program using any high-level language as it provides
a blueprint for writing a porogram to solve a particular problem.
Sequence
It means that each step of the algorithm is executed in the specified order. For example
an algorithm to add two numbers performs the steps in a purely sequential order.
Example 1.1
9
Problem Solving
Decision
Decision statements are used when the outcome of the process depends on some
condition.
A decision statement can be stated in the following manner.
If condition then
Process 1
Else
Process 2
This is popularly known as the if-else construct. Here, if the condition is true then process 1 is
excuted else process 2 is excuted. Example 1.2 shows an algorithum that checks if two
numbers are equal.
Example 1.2
Write an algorithm to check if two entered numbers are equal.
1. Start
2. Input the first number, x
3. Input the second number, y
4. If x = y then
Print “Both are Equal”
else
Print “ Both are not equal”
5. Stop
Repetition
Repitition involves executing one or more steps for a number of times.
It can be inplemented by using the constructs like while, do while and for loops. Example
1.3 shows an algorithum that prints the sum of first 10 natural numbers.
Example 1.3
1. Start.
2. Intialize and set i = 0, n = 10, sum = 0
3. Repeat steps 3 through 5 while i<=n
10
Problem Solving
Example 1.4
Example 1.5
Write an algorithm to print the percentage and grade of a student using the following
rules:
Percentage Grade
Above 75 A
60-75 B
40-60 C
Less than 40 Fail
Solution :
1. Start
2. Input paper 1 marks, p1.
3. Input paper 2 marks, p2.
11
Problem Solving
Example 1.6
Example 1.7
Write an algorithm to check that whether the entered number is prime or not.
Solution
1. Start
2. Read the number as num
3. Initilialize and set i =2, flag = 1
12
Problem Solving
Every algorithm uses some of the computer’s resoueces like central processing time
and internal memory to complete its task. Because of high cost of computing resources, it is
desirable to design algorithm that are economical in the use of CPU time and memory.
Efficiency of algorithms is tied in with the design, inplementation and analysis of algorithm.
13
Problem Solving
more algorithms that are intended to solve the same problem. This is an important step
because the use of an algorithm that is more efficient in terms of time, resources required,
can save time and money.
Once we have defind the problem and have an idea of how to solve it, we can then
use the powerful techniques for designing algorithms. Most of the problems are complex or
large problems and to solve them we have to focus on to comperhend at one time, a very
limited span of logic or instructions. A technique for algorithm design that tries to accommodate
this human limitation is known as top-down design or stepwise refinement.
Top down design suggests taking the general statements about the solution one at a
time, and then breaking them down into a more precise subtask / sub-problem. These sub-
problems should more accurately describe how the final goal can be reached. The process
of repeatedly breaking a task down into a subtask and then each subtask into smaller subtask
must continue until the sub-problem can be implemented as the program statement. With
each spitting, it is essential to define how sub-problems interact with each other. In this way,
the overall structure of the solution to the problem can be maintained. Preservation of the
overall structure is important for making the algorithm comprehensible and also for making it
possible to prove the correctness of the solution.
14
Problem Solving
Complex or
Large Problem
Complex
Solution
1. 5 FLOW CHARTS
The next step after the algorithm development is the flowcharting. Flowcharts are used
in programming to diagram the path in which information is processed through a computer to
obtain the desired results. Flowcharts is a graphical repesentation of an algorithm. It makes
use of symbols which are connected among them to indicate the flow of information and
procesing. It will show the general outline of how to solve a problem or perform a task. It is
prepared for better understanding of the algorithm.
Advantages of flowcharts
1. The flowcharts shows the logic of a problem displayed in pictorial fashion which
felecitates easier checking of an algorithm.
2. The flowchart is good means of communication to other users. It is also a compact
means of recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into parts. These parts
can be connected to make master chart.
4. The flowchart is a permanent record of the soluition which can be consulted at a later
time.
15
Problem Solving
Algorithm Flowchart
1. A method of representing the step-by- 1. Flowchart is a diagrammatic re-
step logical procedure for solving a presentation of an algorithum. It is
a problem. constructed using different types of
boxes and symbols.
2. It contains step - by - step English 2. The flowchart employs a series of
description, each step representating blocks and arrows, each of which
a particular operation leading to represents a particular step in an
solution of problem. algorithm.
3. These are particularly useful for 3. These are useful for detailed repre-
small problems. sentations of complicated programs.
4. For complex programs, algorithms 4. For complex programs, flowcharts
prove to be inadequate. prove to be adequate.
1. Start / stop
3. Input/output
6. Process
16
Problem Solving
Example 1. 8
The flowchart for the Example 1.1 is shown below :
Start
Read a, b
Sum = a + b
Print sum
Stop
17
Problem Solving
Example 1. 9
The flowchart for the Example 1.2 is shown below :
Start
Read x, y
NO
If x = y then Print “Both number
are not equal”
yes
Stop
18
Problem Solving
Example 1.10.
Start
Yes
Is i < = 10
No
Display Sum
Stop
19
Problem Solving
Example 1.11
Start
Calculate
Sal = (nohr *payhr) + travelallowance
Print Sal
Stop
1. 6 Summary
Programming Languages are used to creare programs that control the behaviour of
a system, to express algorithms or as a mode of human computation. To process a problem
different problem - solving tools are available that help in finding the solution to the problem in
an efficient way. Writing the algorithm and drawing the flowchart for the solution to the stated
problem are the major steps which should be followed. Top down design provides the way of
handing the logical complexity in computer algorithm. It allows building solutions to problems
in a stepwise fashion. We are using C language - a standardized, industrial-strength
programming language known for its power and portability as an implementation vechicle for
these problem solving techniques using computer.
20
Problem Solving
1. 8 Suggested Reading
1. How to solve it by Computer, 5th Edition, R G Dromey, PHI, 1992.
2. Introduction to Computer Algorithms, Second Edition, Thomas H.Cormen, MIT press,
2001.
3. The Art of Computer Programming, Vol. 1: Fundamental Algorithms, 3rd Edition,
Donald E Knuth, July 17,1997.
4. Fundamentals of Algorithmics, Gilles Brassword, Paul Bratley, PHI, 1996.
5. Fundamentals Algorithms, Third Edition, Donald E Knuth, Addison-Wesley, 1997.
6. Programming in C by R Subburaj, Vikas Publishing House, Delhi, Year 2000.
21