CSE1012 - Problem Solving and Programming
SESSION-2
Dr. L.PAVITHRA
Assistant Professor (sr)
SCOPE
VIT University
1
Problem
• Problem is a puzzle that requires logical thought and /or mathematics to
solve.
• A puzzle could be a set of questions on a scenario which consists of
description of reality and set of constraints about the scenario.
2
Example
• Scenario :
VIT AP campus has a library. The librarian issues books
only to VIT employees. Find out the utilization percentage
of the books in the library and of books by each faculty
….Careful observation suggests...
• Description of reality : There is a library in VIT AP
campus and there is a librarian in the library.
3
Problem
• Constraint : Librarian issues books only to VIT employees
Questions about the scenario:
1. How many books are there in the library?
2. How many books can be issued to an employee?
3. Does the librarian issue a book to himself? etc
4
Types of Problems
• All Problems do not have a straightforward solutions.
• Some problems, such as balancing a checkbook or baking a cake, can be
solved with a series of actions.
• These solutions are called algorithmic solutions.
• There may be more than one solution for a problem
• Identify all possible ways to solve a problem and choose one among
them
5
Types of Problems
• The solutions of other problems, such as how to buy
the best stock or whether to expand the company, are not so
straightforward.
• These solutions require reasoning built on knowledge and experience,
and a process of trial and error.
• Solutions that cannot be reached through a direct set of steps are called
heuristic solution
6
Problem Solving with Computers
• Computers are built to solve problems with algorithmic solutions, which
are often difficult or very time consuming when input is large
• Solving a complicated calculus problem or alphabetizing 10,000
names is an easy task for the computer
• So the basis for solving any problem through computers is by developing
an algorithm
7
• Field of computers that deals with heuristic types of problems
is called Artificial Intelligence (AI)
• Artificial intelligence enables a computer to do things like
human by building its own knowledge bank
• As a result, the computer’s problem-solving abilities are
similar to those of a human being.
• Artificial intelligence is an expanding computer field,
especially with the increased use of Robotics. 8
Computational Problems
• Computation is the process of evolution from one
state to another in accordance with some rules.
9
Types of Computational Problems
10
Problem Solving Life Cycle
For any problem solving strategy logic is prerequisite.
11
12
Logic – Basis for solving any
problem
• Definition : A method of human thought that involves
thinking in a linear, step by step manner about how a
problem can be solved
• Logic is a language for reasoning.
• It is a collection of rules we use when doing reasoning.
• Eg: John's mum has four children.
– The first child is called April.
– The second May.
– The third June.
• What is the name of the fourth child? 13
What Problem Can Be Solved By
Computer
• Solving problem by computer undergo two phases:
– Phase 1:
• Organizing the problem or pre-programming phase.
– Phase 2:
• Programming phase.
14
PRE-PROGRAMMING PHASE
• Analyzing The Problem
– Understand and analyze the problem to determine
whether it can be solved by a computer.
– Analyze the requirements of the problem.
– Identify the following:
• Data/Input requirement.
• Processing requirement or procedures that will be
needed to solve the problem.
• The output. 15
PRE-PROGRAMMING PHASE
• All these requirements can be presented in a Problem
Analysis Chart (PAC)
Data/Input Processing Output Solution
Alternatives
given in the problem List of processing Output requirement. List of ideas for the
or provided by the required or solution of
user procedures. the problem.
16
PRE-PROGRAMMING PHASE
• Payroll Problem
– Calculate the salary of an employee who works by hourly basis. The
formula to be used is
Salary = Hour works * Pay rate
Data/Input Processing Output Solution Alternatives
Hours work, Salary = Hours work * payrate Salary 1. Define the hours worked
Pay rate and pay rate as constants.
∗2. Define the hours worked
and pay rate as input values.
1A-17
Miles to Km
Write a Problem Analysis Chart (PAC) to convert the distance
in miles to kilometers where 1.609 kilometers per mile.
Data/Input Processing Output Solution
Alternatives
Distance in miles Kilometers = 1.609 x Distance in 1. Define the miles as
miles kilometers constants.
∗2. Define the miles
as input values.
1A-18
Importance of Logic in problem solving
Determine whether a given number is prime or not?
Data/Input Processing Output Solution Alternatives
Number, N Check if there is a Print Prime or 1. Divide N by numbers from 2 to N and if for
factor for N Not Prime all the division operations, the reminder is non
zero, the number is prime otherwise it is not
prime
2. Same as 1 but divide the N from 2 to N/2
3. Same as Logic 1 but divide N from 2 to
square root of N
19
Problem 2
• Write a Problem Analysis Chart (PAC) to find an area of a
circle where area = pi * radius * radius
Data/Input Processing Output
radius area = 3.14 x radius x radius area
1A-21
Problem 3
• Write a Problem Analysis Chart (PAC) to compute and display the
temperature inside the earth in Celsius and Fahrenheit. The relevant
formulas are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32
Data/ Processing Output
Input
depth celsius = 10 x (depth) + 20 Display celsius,
fahrenheit = 1.8 x (celsius) + 32 Display fahrenheit
1A-22
Problem 4
Given the distance of a trip in miles, miles per gallon by the
car that is used in the trip and the current price of one gallon
of fuel (gas), write a program to determine the fuel required
for the trip and the cost spent on the fuel.
Input Processing Output
Distance in miles, gas needed = distance / miles per Display gas needed
miles per gallon, gallon.
cost per gallon Display estimated cost
estimated cost = cost per gallon x gas
needed
23
HIPO Chart
• Developing the Hierarchy Input Process Output (HIPO)
or Interactivity Chart
– When problem is normally big and complex.
– Processing can be divided into subtasks called modules
– Each module accomplishes one function
– These modules are connected to each other to show the interaction
of processing between the modules 24
HIPO Chart
• Programming which use this approach (problem is divided
into subtasks) is called Structured Programming
Main Module
Module 1 Module 2 Module 3
Module 4 Module 5 Module 6
25
HIPO Chart for Payroll Problem
PAYROLL
READ CALCULATE PRINT
26
Extended Payroll Problem
• You are required to write a program to calculate both
the gross pay and the net pay of every employee of
your company. Use the following formulae for
calculation:
– Gross pay = number of hours worked * pay rate
– Net pay = gross pay – deductions
• The program should also print the cheque that tells
the total net pay.
27
PAC for Extended Payroll Problem
Input Processing Output
Number of hours worked, Gross pay = number of hours * pay Net pay and write net pay in
pay rate, deductions rate cheque
Net pay = Gross pay – deductions
28
HIPO Chart
Payroll
0000
Calculate Calculate Write
Gross Pay Net Pay Cheque
1000 2000 3000
Get Get Calculate
Hourly Pay rate Deductions
Worked 1200 2100
1100
29
Temperature of Earth
• Write a Hierarchy Input Process Output (HIPO) to compute and display
the temperature inside the earth in Celsius and Fahrenheit. The relevant
formulas are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32
Temperature
0000
Calculate temperature Calculate temperature Display
in celsius In fahrenheit Temperature
1000 2000 3000
Get depth
1100
30
Algorithm
• Step by step procedure to solve a problem
• 'In Computer Science following notations are used to
represent algorithm
• Flowchart: This is a graphical representation of
computation
• Pseudo code: They usually look like English
statements but have additional qualities
31
32
Algorithm
• Algorithms are not specific to any programming language
• An algorithm can be implemented in any programming language
• Use of Algorithms
– Facilitates easy development of programs
– Iterative refinement
– Easy to convert it to a program
– Review is easier
33
Steps to Develop an Algorithm
34
Algorithm for Real life Problem
PROBLEM: Heat up a can of soup
ALGORITHM:
1. open a can using can opener
2. pour contents of a can into saucepan
3. place saucepan on ring of cooker
4. turn on correct cooker ring
5. stir soup until warm
may seem a bit of a silly example but it does show us that the
order of the events is important since we cannot pour the
contents of the can into the saucepan before we open the can.
35
Properties of an Algorithm
36
What is an Algorithm
• Step by step procedure to solve any task or real world problem.
Characteristics
• Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs.
• Well-Defined Outputs: The algorithm must clearly define what output will be yielded and
it should be well-defined as well.
• Finiteness: The algorithm must be finite, i.e. it should not end up in an infinite loops or
similar.
• Feasible: The algorithm must be simple, generic and practical, such that it can be executed
upon will the available resources. It must not contain some future technology, or anything.
• Language Independent: The Algorithm designed must be language-independent, i.e. it
must be just plain instructions that can be implemented in any language, and yet the output
will be same, as expected. 37
Different patterns in Algorithm
38
Algorithm for adding two numbers
Step 1: Read two numbers A and B
Step 2: Let C = A + B
Step 3: Display C
39
Area of a Circle
Step 1 : Read the RADIUS of a circle
Step 2 : Find the square of RADIUS and store it in SQUARE
Step 3 : Multiply SQUARE with 3.14 and store the result in
AREA
Step 4: Print AREA
40
Average Marks
• Find the average marks scored by a student in 3 subjects:
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Sum = Marks1 + Marks2 + Marks3
Step 3 : Average = Sum / 3
Step 4 : Display Average
41
Flow Chart
and
Phases of Making
an Executable Code
42
Drawing Flowcharts
– Flowchart is the graphic representations of the individual
steps or actions to implement a particular module
– Flowchart can be likened to the blueprint of a building
– An architect draws a blueprint before beginning construction
on a building, so the programmer draws a flowchart before
writing a program
– Flowchart is independent of any programming language. 43
Flow Charts
A flow chart is an organized
combination of shapes,
lines and
text
that graphically
illustrate a
process or structure.
44
PRE-PROGRAMMING PHASE
Symbol Function
Show the direction of data flow or logical solution.
Indicate the beginning and ending of a set of actions or instructions
(logical flow) of a module or program.
Indicate a process, such as calculations, opening and closing files.
45
PRE-PROGRAMMING PHASE
Indicate input to the program and output from the program.
Use for making decision. Either True or False based on certain condition.
Use for doing a repetition or looping of certain steps.
Connection of flowchart on the same page.
Connection of flowchart from page to page.
46
Sequential Logic Structure
47
Sale Problem
Statement: Given the unit price of a product and the quantity of
the product sold, draw a flowchart to calculate and print the total
sale.
Solution: Stepwise Analysis of the Sale Problem
• Read the unit price and the quantity
• Calculate total sale = unit price and quantity
• Print total sale
48
PRE-PROGRAMMING PHASE
START A
READ TOTAL SALE =
UNIT PRICE UNITPRICE ´ QUANTITY
READ PRINT
QUANTITY TOTALSALE
A STOP
49
Find the average of three numbers
50
Assignment Task
51
Do Algorithm & FLOWCHART for the following
• To find the average speed of a car given the journey time and
the distance travelled
• To find the volume n of a cube given the length of a side.
• To find the volume of a pyramid, given the length and breadth
of its base and its height.
• Find the volume of a cone given its diameter and height
• Given two overlapping rectangles on a plane. We are given bottom
left and top right points of the two rectangles. We need to find the
total area (Green and pink areas in below diagram
52
53