Day 2 - Material - 19-09-2022
Day 2 - Material - 19-09-2022
SESSION-2
Dr. L.PAVITHRA
Assistant Professor (sr)
SCOPE
VIT University
1
Problem
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...
4
Types of Problems
an algorithm
7
• Field of computers that deals with heuristic types of problems
is called Artificial Intelligence (AI)
9
Types of Computational Problems
10
Problem Solving Life Cycle
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.
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
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
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.
Main Module
25
HIPO Chart for Payroll Problem
PAYROLL
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
28
HIPO Chart
Payroll
0000
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
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
44
PRE-PROGRAMMING PHASE
Symbol Function
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.
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
START A
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