MCA First Year- BMC 102
Unit-1.1 Problem Solving & Programming Thumb Rules are:
Introduction
1. “Read the problem at least three times (or however many makes
You can’t solve a problem you don’t understand. There is a difference
you feel comfortable)”
between the problem and the problem you think you are solving. It’s easy to
start reading the first few lines in a problem and assume the rest of it because 2. Work through the problem manually with at least three sets of
it’s similar to something you’ve seen in the past. sample data
Example: Let’s take a simple problem to find Even Numbers If there are no Problem Solving Approach
even numbers, return the empty Here are some questions that run through my
mind:
How can a computer tell what is an even number?
Divide that number by 2
see if its remainder is 0.
Take out a piece of paper and work through the problem manually. Think of
at least three sets of sample data you can use.
Consider corner and edge cases as well.
Corner case: a problem or situation that occurs outside of normal
operating parameters, specifically when multiple environmental
variables or conditions are simultaneously at extreme levels, even though
each parameter is within the specified range for that parameter.
Edge case: problem or situation that occurs only at an extreme
(maximum or minimum) operating parameter
Problem Solving strategies/Techniques
There are two very commonly-used strategies in solving problems are the
algorithmic and heuristic.
Algorithmic
• A finite series of steps which if faithfully performed will
always result in a task or process being completed -- in this
case, the problem will be solved.
• In problem solving, this approach is sometimes called a
"brute-force" solution because all possible rearrangments are
tried.
Heuristic
• A rule of thumb -- selective searches involving looking at
only those portions of the problem space that are most likely
to produce a solution.
Problem Statement:
Problem Statement help diagnose the situation so that your focus is on the
problem, helpful tools at this stage include Algorithms and flowcharts for
identifying the expected steps of a process. Therefore, to solve any problem,
Collect and analyze information and data
Talk with people familiar with the problem
If at all possible, view the problem first hand
Confirm all findings
To Understand and Analyze the Problem following techniques used
1. Algorithm
Characteristics of an algorithm
An algorithm must possess the following properties
1. Finiteness: An algorithm must terminate in a finite number of steps Example 4. Write an algorithm to find the largest of three numbers X,
2. Definiteness: Each step of the algorithm must be precisely and Y, Z.
unambiguously stated Step 1: Read the numbers X, Y, Z.
3. Effectiveness: Each step must be effective, in the sense that it should Step 2: if (X > Y) Big = X else BIG = Y
be primitive easily convert able into program statement) can be Step 3: if (BIG < Z)
performed exactly in a finite amount of time. Step 4: Big = Z
4. Generality: The algorithm must be complete in itself so that it can Step 5: Print the largest number i.e. Big
be used to solve problems of a specific type for any input data. Step 6: Stop.
5. Input/output: Each algorithm must take zero, one or more quantities Techniques used
as input data produce one or more output values. Pseudocodes A way of expressing algorithms that uses a mixture of English
An algorithm can be written in English like sentences or in any standard phrases and indention to make the steps in the solution explicit. There are no
representation sometimes, algorithm written in English like languages are grammar rules in pseudocode is not case sensitive. A mixture of English and
called Pseudo Code formatting to make the steps in an algorithm explicit
Example 1. Suppose we want to find the average of three numbers, the Algorithm vs. Pseudocode
algorithm is as follows
Step 1 Read the numbers a, b, c
Step 2 Compute the sum of a, b and c
Step 3 Divide the sum by 3
Step 4 Store the result in variable d
Step 5 Print the value of d
Step 6 End of the program
Example 2: Write an algorithm to calculate the simple interest using the
formula. Simple interest = P*N* R/100. Where P is principle Amount, N
is the number of years and R is the rate of interest.
Step 1: Read the three input quantities’ P, N and R.
Step 2: Calculate simple interest as Simple interest = P* N* R/100
Step 3: Print simple interest.
Step 4: Stop.
Example 3. Area of Triangle: Write an algorithm to find the area of the
triangle.
Let b, c be the sides of the triangle ABC and The included angle
between the given sides.
Step 1: Input the given elements of the triangle namely sides b, c
and angle between the sides A.
Step 2: Area = (1/2) *b*C* sin A
Step 3: Output the Area Step
4: Stop.
What is a flowchart? 5. Connectors: Whenever flowchart becomes complex or it spreads
A flowchart is a diagram that depicts a process, system or computer over more than one page, it is useful to use connectors to avoid any
algorithm. They are widely used in multiple fields to document, study, plan, confusions. It is represented by a circle.
improve and communicate often complex processes in clear, easy-to-
understand diagrams.
PICTORIAL REPRESENTATION OF PROGRAM
Basic Symbols used in Flowchart Designs 6. Flow lines: Flow lines indicate the exact sequence in which
1. Terminal: The oval symbol indicates Start, Stop and Halt in a instructions are executed. Arrows represent the direction of flow of
program’s logic flow. A pause/halt is generally used in a program logic
control and relationship among different symbols of flowchart.
under some error conditions. Terminal is the first and last symbols in
the flowchart.
General Rules to draw flowchart
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with
no other entry points. The exit point for all flowchart symbols is on the
2. Input/Output: A parallelogram denotes any function of input/output bottom except for the Decision symbol.
type. Program instructions that take input from input devices and 3. The Decision symbol has two exit points; these can be on the sides or
display output on output devices are indicated with parallelogram in the bottom and one side.
a flowchart. Advantages of Flowchart:
Flowcharts are better way of communicating the logic of system.
Flowcharts act as a guide for blueprint during program designed.
Flowcharts helps in debugging process.
With the help of flowcharts programs can be easily analyzed.
3. Processing: A box represents arithmetic instructions. All arithmetic
It provides better documentation.
processes such as adding, subtracting, multiplication and division are Flowcharts serve as a good proper documentation.
indicated by action or process symbol. Disadvantages of Flowchart:
It is difficult to draw flowchart for large and complex programs.
In this there is no standard to determine the amount of detail.
Difficult to reproduce the flowcharts.
4. Decision Diamond symbol represents a decision point. Decision
It is very difficult to modify the Flowchart.
based operations such as yes/no question or true/false are indicated Takes more time to draw.
by diamond in flowchart. Imagine developing a detailed flowchart for a program containing
50000 lines or statements of
instructions
Example 1 : ENTER TWO NUMBER FROM THE USER
DISPLAY THE SUM OF THE NUMBERS
START
ACCEPT
NUM1
ACCEPT
NUM2
CALCULATE
SUM=NUM1+NUM2
DISPLAY
SUM
STOP
EXAMPLE 2: ENTER TWO NUMBER FROM THE USER , FIND OUT EXAMPLE 3: ENTER THREE NUMBER FROM THE USER , FIND
THE LARGEST VALUE OUT THE LARGEST VALUE- NUM1, NUM2,NUM3
CASE 1 : 5, 7, 3 5> 7 NO , 7> 3 YES 7
START
NUM1 > NUM2 NO
NUM2 > NUM3 YES
PRINT NUM2
CASE2: 5,7,8
5>7 NO, 7 > 8 NO -> 8
ACCEPT NUM1> NUM2 -> NO
NUM1,NUM2 NUM2> NUM3-> NO
PRINT NUM3
CASE 3: 8,7,3
8>7 YES , 8> 3 YES PRINT 8
NUM1> NUM2 YES , NUM1>NUM3
YES PRINT NUM1
YES
IS A>B ? DISPLAY A
NO
DISPLAY B
STOP
EXAMPLE 4: PRINT ALL ODD NUMBERS FROM 1 TO 100
START
N=1
DISPLAY N
N=N+2
YES
NO
IS N>99
STOP
?
Thus a flowchart represents graphically step by step a problem solution or
an algorithm using specific symbols. Each symbol has name.
Symbols used to Draw Flowchart
Examples
Write a pseudocode and draw a flow chart to make a cup of tea
Pseudocode and Flowchart
Check The Temperature freezing point
Playing Snakes and Ladder
Exercise
1. Explain steps involve in drawing of a flowchart.
2. Explain uses of Flowchart.
3. Write algorithm for the problem given below
Ramsh goes to market for buying some fruits and vegetables. He has
Rs 500 with him for marketing. From a shop he purchases 2.0 kg
Apple priced Rs. 50.0 per kg, 1.5 kg Mango priced Rs.35.0 per kg,
2.5 kg Potato priced Rs.10.0 per kg, and 1.0 kg Tomato priced Rs.15
per kg. He gives the currency of Rs. 500 to the shopkeeper. Find out
the amount shopkeeper will return to Ramshewak. and also tell the
total item purchased.
4. Find factorial of N?
5. Draw a flowchart to find the sum of first 100 natural numbers. 9)
Draw a flowchart to find the largest of three numbers x, y and z.
6. Write an Algorithm and draw flowchart for determining a number is
prime or not prime number or not
7. Draw a flowchart which generates first 50 items of the Fibonacci
series: 1, 1, 2, 3, 5, 8,…?
8. Design an algorithm to convert a decimal number, n, to binary
format?
What is Programming? To write a program for a computer to follow, we must go through a Problem
Computer programming is a medium for us to communicate with computers solving phase/ process:
just like we use Hindi or English to communicate with each other, 1. Problem-Solving Phase
programming is a way for us to deliver our instructions to the computer. 1. Analysis and Specification. Understand (define) the problem and what
Programming Planning or scheduling the performance of a task the solution must do.
or an event 2. General Solution (Algorithm). Specify the required data types and the
Computer programming: The process of specifying the data types and the logical sequences of steps that solve the problem.
operations for a computer to apply to data in order to solve a problem 3. Verify. Follow the steps exactly to see if the solution really does solve
Data Information in a form a computer can use the problem.
Information Any knowledge that can be communicated 2. Implementation Phase
Data type The specification of how information is represented in 4. Concrete Solution (Program). Translate the algorithm (the general
the computer as data and the set of operations that can be applied solution) into a programming language.
to it 5. Test. Have the computer follow the instructions. Then manually check
the results. If you find errors, analyze the program and the algorithm
to determine the source of the errors, and then make corrections.
Once a program has been written, it enters a third phase:
maintenance.
Maintenance Phase
6. Use. Use the program.
7. Maintain. Modify the program to meet changing requirements or to
correct any errors that show up while using it.
How to Do Programming?
A computer is not intelligent. It cannot analyze a problem and come up with
a solution.A Person who (the programmer) analyze the problem, develop the
instructions for solving the problem, and then have the computer carry out the
instructions.
Design Methods: Designing is the first step for obtaining solution of a given Relation among modules is not The modules must be related for
problem. The purpose of designing is ~o represents the solution for the always required. better communication and work flow.
system. It is really difficult to design a large system because the complexity Primarily used in code Finds use primarily in testing.
system cannot be represented easily. So various methods have been evolved implementation, test case
for designing. generation, debugging and
Top-Down Design:. 'Every system has several hierarchies of components. module documentation.
The top-level component represents the whole tern. Top-Down design method
starts from top-level component to lowest level (bottom) component. this References
design method, the system is divided into some major components. 1. Hanly J. R. and Koffman E. B.,“Problem Solving and Program Design in
C”, Pearson
Education.
2. [Link]
flow-chart,-programming-language_35900/
3. [Link]
4. [Link]
Difference between Top-down and Bottom-up Approach
Top-Down Approach Bottom-Up Approach
In this approach problem is Starts from bottom, focus on data first
divided into smaller units and (like Object Oriented approach)
then solve it. solving small modules and adding
them up together.
May contain redundant Redundancy can easily be eliminated.
information.
A well-established Communication among steps is
communication is not required. mandatory.
The individual modules are Works on the concept of data-hiding
thoroughly analyzed. and encapsulation.
Structured programming OOP languages like C++ and Java,
languages such as C uses top- etc. uses bottom-up mechanism.
down approach.