Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BCSE101E: Problem Solving and Programming Day-1 & Day 2 Session

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 56

BCSE101E

Problem Solving and Programming


Day-1 & Day 2 Session

Dr. M Madiajagan
Associate Professor
SCOPE
Skills Required for a Software
Engineer
Technical Skills
– Software Design
– Coding
– Testing
Problem Solving Skills
–logical and analytical thinking
Soft Skills
– Communication
– Team Work
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.
Example Scenario: VIT Vellore has a library. The librarian issues
books only to VIT employees. Careful observation suggests...
Description of reality : There is a library in VIT Chennai campus
and there is a librarian in the library.
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
Case study - Discussion

Have you ever observed this scenario?


Yes!!! What are the problems in the scenario?
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
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 experimental solution
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
• 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.
Computational Problems
• Computation is the process of evolution from
one state to another in accordance with some
rules.
Types of Computational Problems
Problem Solving Life Cycle

For any problem solving strategy logic is prerequisite.


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?
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.
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 requirement.
• Processing requirement or procedures that will be
needed to solve the problem.
• The output.
PRE-PROGRAMMING PHASE
• All these requirements can be presented in a
Problem Analysis Chart (PAC)

Data Processing Output Solution


Alternatives
given in the List of Output List of ideas for
problem or processing requirement. the solution of
provided by required or the problem.
the user procedures.
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 Processing Output Solution Alternatives

Hours Salary = Hours work * Salary 1. Define the hours


work, payrate worked
Pay rate and pay rate as constants.
∗2. Define the hours
worked
and pay rate as input
values.
Miles to Km
Write a Problem Analysis Chart (PAC) to convert
the distance in miles to kilometers where 1.609
kilometers per mile.
Data Processing Output Solution
Alternatives

Distance in Kilometers = Distance in 1. Define the


miles 1.609 x miles kilometers miles as
constants.
∗2. Define the
miles as input
values.
Importance of Logic in problem solving
Determine whether a given number is prime or not?

Data Processing Output Solution Alternatives

Number, N Check if there is a Print Prime or Not 1. Divide N by


factor for N Prime numbers from 2 to N
and if for 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
Problem 2
• Write a Problem Analysis Chart (PAC) to find an area
of a circle where area = pi * radius * radius

Data Processing Output


radius area = 3.14 x radius x radius area
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

depth celsius = 10 x (depth) + 20 Display celsius,


fahrenheit = 1.8 x (celsius) + Display
32 fahrenheit
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 / Display gas needed
miles per gallon, miles per gallon.
cost per gallon Display estimated cost
estimated cost = cost per
gallon x gas needed
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
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


HIPO Chart for Payroll Problem

PAYROLL

READ CALCULATE PRINT


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.
PAC for Extended Payroll Problem
Input Processing Output
Number of hours Gross pay = number of Net pay and write net
worked, pay rate, hours * pay rate pay in cheque
deductions Net pay = Gross pay –
deductions
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
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
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
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
Steps to Develop an Algorithm
Algorithm for Real life Problem
PROBLEM: Heat up a can of soup
ALGORITHM:
1 open can using can opener
2 pour contents of 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.
Properties of an Algorithm
Different patterns in Algorithm
Sequential Algorithms
Algorithm for adding two numbers
Step 1: Read two numbers A and B
Step 2: Let C = A + B
Step 3: Display C
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
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
Selection Algorithms
Algorithm for Conditional Problems
PROBLEM: To decide if a fire alarm should be sounded
ALGORITHM:
1 IF fire is detected condition
2 THEN sound fire alarm action
Another example is:-
PROBLEM: To decide whether or not to go to school
ALGORITHM:
1 IF it is a weekday AND it is not a holiday
2 THEN go to school
3 ELSE stay at home
Pass/ Fail and Average
• Write an algorithm to find the average marks of a
student. Also check whether the student has
passed or failed. For a student to be declared
pass, average marks should not be less than 65.
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Total = Marks1 + Marks2 + Marks3
Step 3 : Average = Total / 3
Step 4 : Set Output = “Student Passed”
Step 5 : if Average < 65 then Set Output =
“Student Failed"
Step 6 : Display Output
Leap Year or Not
Step 1 : Read YEAR
Step 2 : IF ({YEAR%4=0 AND YEAR%100!
=0)OR (YEAR%400=0))
Display "Year is a leap year"
ELSE
Display “Year is not a leap year"
ENDIF
Algorithm for Iterative Problems
This type of loop keeps on carrying out a command or
commands UNTIL a given condition is satisfied, the condition
is given with the UNTIL command, for example:-
PROBLEM: To wash a car
ALGORITHM:
1 REPEAT
2 wash with warm soapy water
3 UNTIL the whole car is clean
Iterational Algorithms – Repetitive Structures

• Find the average marks scored by ‘N’

number of students
Step 1 : Read Number Of Students

Step 2 : Let Counter = 1

Step 3 : Read Marks1, Marks2, Marks3

Step 4 : Total = Marks1 + Marks2 + Marks3

Step 5 : Average = Total / 3

Step 6 : Set Output = “Student Passed”

Step 7 : If (Average < 65) then Set Output = “Student Failed"

Step 8 : Display Output

Step 9 : Set Counter = Counter + 1

Step 10 : If (Counter <= NumberOfStudents ) then goto step 3


Bigger Problems
• If you are asked to find a solution to a major problem, it can
sometimes be very difficult to deal with the complete problem all at
the same time.
• For example building a car is a major problem and no-one knows
how to make every single part of a car.
• A number of different people are involved in building a car, each
responsible for their own bit of the car’s manufacture.
• The problem of making the car is thus broken down into smaller
manageable tasks.
• Each task can then be further broken down until we are left with a
number of step-by-step sets of instructions in a limited number of
steps.
• The instructions for each step are exact and precise.
Top Down Design
• Top Down Design uses the same method to break a
programming problem down into manageable steps.
• First of all we break the problem down into smaller steps
and then produce a Top Down Design for each step.
• In this way sub-problems are produced which can be
refined into manageable steps.
Top Down Design for Real Life Problem
PROBLEM: To repair a puncture on a bike wheel.
ALGORITHM:
1. remove the tyre
2. repair the puncture
3. replace the tyre
Step 1: Refinement:
1. Remove the tyre
1.1 turn bike upside down
1.2 lever off one side of the tyre
1.3 remove the tube from inside the tyre
Step 2: Refinement:
2. Repair the puncture Refinement:
2.1 find the position of the hole in the tube
2.2 clean the area around the hole
2.3 apply glue and patch
Step 3: Refinement:
3. Replace the tyre Refinement:
3.1 push tube back inside tyre
3.2 replace tyre back onto wheel
3.3 blow up tyre
3.4 turn bike correct way up
Still more Refinement:
Sometimes refinements may be required to some of the sub-
problems, for example if we cannot find the hole in the tube,
the following refinement can be made to 2.1:-
Still more Refinement:
Step 2.1: Refinement
2.1 Find the position of the hole in the tube
2.1.1 WHILE hole cannot be found
2.1.2 Dip tube in water
2.1.3 END WHILE

You might also like