0% found this document useful (0 votes)
12 views

Module 1 TF1

Uploaded by

kavirajanp2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module 1 TF1

Uploaded by

kavirajanp2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

BCSE101E –

Computer Programming : Python

MODULE 1

Dr. KAVIYA ELAKKIYA


AP (Senior) / SCOPE

1
Module 1

2
Problem Solving
Definition and Steps

3
Definition
◼ Problem-solving is defined as a
process of understanding the problem,
designing an algorithm for it, and
finally implementing the solution to it
◼ Problem solving is the process of a
problem analysis and resolving it in the
best way possible for the situation.
◼ Problem solving is the process of
identifying a problem, developing
possible solution paths, and taking the
appropriate course of action.

4
Problem Solving Process

5
Objective

◼ List and explain the steps in problem solving.

6
Steps in Problem Solving
1) Identify and define the
Problem
2) Analyze the Problem
3) Identifying Possible
Solutions
4) Selecting the Best Solutions
5) Evaluating Solutions
6) Develop an Action Plan
7) Implement the Solution

7
Identify and Define the Problem
◼ Write down a statement which summarizes the
problem. This statement may be long if the problem
is complex.
◼ Set a goal for solving the problem.
◼ Example: The computer will boot up but
immediately turn itself off.
When the problem is solved the computer will be
usable.

8
Analyze the Problem
◼ Ask these questions:
❖ How long has the problem been going on?

❖ What are the causes of the problem?

❖ What are the effects and symptoms of the


problem?
❖ Are there multiple steps to solving this specific
problem?

9
Identifying Possible Solutions
◼ Brainstorming
❖ Those working to solve the problem should come
up with as many solutions as possible.
❖ Often one person’s ideas may spur the ideas of
others.
❖ All ideas should be written down to evaluate later.

10
Selecting the Best Solutions
◼ Use the brainstormed list of possible solutions.
◼ Discuss each of the possible solutions and why they
may or may not work.
◼ Select the most feasible solution.

11
Evaluating Solutions
◼ Make a chart with pros of the solution on one side
and the cons on the other.
◼ Ask these questions after making the chart:
❖ What are the advantages of each solution?

❖ Are there any disadvantages to the solution?

❖ Do disadvantages outweigh advantages?

❖ Would the solution actually solve the problem?

12
Develop an Action Plan
◼ Make a step by step plan which
should include:
❖ Tasks, what is to be done

❖ Estimated time to complete


each task
❖ Who will do each task

◼ When this plan is complete,


post it where it will be seen.

13
Implement the Solution
◼ Follow the steps in the Action Plan
◼ For complicated things, make a three
column chart:
❖ What could go wrong?

❖ How can you prevent this from


happening?
❖ How will we fix it if something does
go wrong?
◼ Evaluate results:
Is the problem solved?
14
Organizational Tools
◼ We have to organize the problem first
◼ Certain organizational tools will help us to solve the
problems on the computer
◼ Those tools include
◼ Problem Analysis Chart (PAC)
◼ Structure/ Interactivity Chart
◼ Input Processing Output (IPO) chart

◼ Algorithms

◼ Flowcharts

15
Problem Analysis Chart (PAC)
◼ Understand the problem
◼ Analyze the requirements of the problem
◼ A good way to analyze a problem is to separate it into
four parts, problem analysis chart (PAC):
1. The given data (Constant and Variables)

2. The required results (the output)

3. The processing that is required in the problem


(equation and expressions)
4. A list of solution alternatives.

16
Problem Analysis Chart (PAC)
Given Data Required Results
1. Data given in the problem or 2. Requirements for the output
provided by the user. These reports. This includes the
can be known values or information needed and the
general names for data such format required.
as price, quantity and so forth
Processing Required Solution Alternatives
3. List of processing required. 4. List of ideas for the solution
This includes equations or of the problem
other types of processing,
such as sorting, searching and
so forth

17
PAC Payroll Example
◼ Calculate the gross pay of an employee. The formula
to be used is
GrossPay = Hours * PayRate

◼ Develop PAC for a solution to this problem?


Given Data Required Results

Hours Gross Pay


PayRate
Processing Required Solution Alternatives
GrossPay = Hours * PayRate Define the hours worked and
pay rate as constants

18
Example
◼ Write a Problem Analysis Chart (PAC) to convert the
distance in miles to kilometers where 1 mile is
approximately equal to 1.6 kilometers.

Data Processing Output

Distance in Kilometers = 1.6 * miles Distance in


miles kilometers

19
Example
◼ Write a PAC to find the area of a circle where area = pi
* radius * radius

Data Processing Output

Radius Area = 3.14 * radius * radius Area

20
Exercise
1. Write a PAC to compute and display the
temperature inside the earth in Celsius and
Fahrenheit. The relevant formulas are
Celsius = 10 * (depth) + 20
Fahrenheit = 1.8 * (Celsius) + 32
2. Write a PAC that asks a user to enter the distance of
a trip in miles, the miles per gallon estimate for the
user’s car and the average cost of a gallon of gas.
Calculate and display the number of gallons of gas
needed and the estimated cost of the trip

21
Algorithm
◼ A process or set of rules which must be followed to
complete a particular task.

◼ Basically, a step-by-step procedure to complete any task.

22
Need for Algorithm
◼ To develop a framework for instructing computers.
◼ To introduce the notation of basic functions.
◼ For defining and describing a large problem in small
parts, so that it is simple to execute.

23
Characteristics of Algorithm
◼ An algorithm should be defined clearly.
◼ An algorithm should have one or more inputs.
◼ An algorithm should produce at least one output.
◼ An algorithm should be executed and finished in finite
number of steps.
◼ An algorithm should be easy to perform.
◼ Each step started with a specific indentation like, “Step 1”,
◼ There must be “Start” as the first step and “End” as the last
step of the algorithm.

24
Some Basic Steps to design an
Algorithm
1. Start – Start the algorithm
2. Input – Take the input values in which the algorithm will
execute.
3. Conditions – Perform some conditions on the inputs to
get the desired output.
4. Output – Printing the outputs.
5. End – End the execution.

25
An example to make a cup of tea
Step 1: Start
Step 2: Take some water in a bowl.
Step 3: Place the bowl on a gas burner.
Step 4: Turn on the gas burner
Step 5: Wait for some time until the water is boiled.
Step 6: Add some tea powder to the water according to the requirement.
Step 7: Again wait for some time until the water turns into brown color
as tea.
Step 8: Then add some sugar according to taste.
Step 9: Again wait for some time until the sugar is melted.
Step 10: Turn off the gas burner and serve the tea in a cup.
Step 11: End

26
Swap two numbers with a
third variable
Step 1: Start
Step 2: Take two numbers as input.
Step 3: Declare another variable as “temp”.
Step 4: Store the first variable to “temp”.
Step 5: Store the second variable to the first variable.
Step 6: Store the “temp” variable to the second
variable.
Step 7: Print the first and second variables.
Step 8: End
27
Exercise
◼ Find the area of a rectangle

28
Exercise
◼ Find the area of a rectangle

Step 1: Start
Step 2: Take the Height and Width of the
rectangle as input.
Step 3: Declare a variable as “area”
Step 4: Multiply Height and Width
Step 5: Store the multiplication to “Area”, (its look
like area = Height x Width)
Step 6: Print “area”;
Step 7: End
29
Exercise
◼ Prepare a bowl of noodles

30
Representation of Algorithms
◼ Two common methods of representing an
algorithm —flowchart and pseudocode.
◼ Either of the methods can be used to represent an
algorithm while keeping in mind the following:
◼ It showcases the logic of the problem solution,
excluding any implementation details.
◼ It clearly reveals the flow of control during
execution of the program.

31
Flowchart
◼ A flowchart is a visual representation of an algorithm
which is made up of boxes, diamonds and other shapes,
connected by arrows.
◼ Each shape represents a step of the solution process
and the arrow represents the order or link among the
steps.
http://www.flowgorithm.org/documentation/index.html

32
Flow Chart Symbols

33
Flow Chart Example

34
Flow Chart Example

Continuation
35
Flow Chart Example

36
Flow Chart Example

37
Exercise
◼ Read the number of subjects (min. 5) from the user, get
the marks of each subject, find the total marks of all the
subjects and output its corresponding average.

38
Flow Chart Example

39
Differences between Algorithm and Flowchart

40
Differences between Algorithm and Flowchart

41
Exercise
◼ Calculate the gross pay of an employee by writing an
algorithm and drawing a flowchart for the same. The
formula to be used is
GrossPay = Hours * PayRate

42
43
Pseudocode
◼ Pseudocode is neither an algorithm nor a program.

◼ It is an abstract form of a program.

◼ It consists of English like statements which performs


the specific operations.

◼ It is close to the actual programming language.

◼ Programmer can start writing the actual code utilizing


the pseudocode.

44
Example

45
Example – Simple Interest calculation
◼ Flow Chart
◼ Algorithm
Step 1: Start
Step 2: get P, n, r value
Step3:Calculate
SI=(p*n*r)/100
Step 4: Display S
Step 5: Stop

46
Pseudocode
BEGIN
READ P, n, r
CALCULATE SI
SI=(p*n*r)/100
DISPLAY SI
END

47
Example – Engineering Cutoff
Calculation
◼ Algorithm ◼ Flow chart
Step 1: Start
Step 2: Get P,C,M value
Step 3: Calculate
Cutoff= (P/4+C/4+M/2)
Step 4: Display Cutoff
Step 5: Stop

48
Pseudocode

BEGIN
READ P,C,M
CALCULATE Cutoff = (P/4+C/4+M/2)
DISPLAY Cutoff
END

49
Example – Factorial of a Given Number
◼ Algorithm
Step 1: Start
Step 2: Get n value
Step 3: Set initial value i=1, fact=1
Step 4: Check i value if(i<=n) goto Step 5 else goto Step 8
Step 5: Calculate fact=fact*i
Step 6: Increment i value by 1
Step 7: Goto Step 4
Step 8: Print fact value
Step 9: Stop
50
Flow Chart

51
Pseudocode
BEGIN
GET n
INITIALIZE i=1,fact=1
IF (i<=n) DO
fact=fact*i
i=i+1
END IF
PRINT fact
END
52
Exercise
Write PAC, algorithm, pseudocode and draw flowchart
for the following:
1. Addition of two numbers.
2. Checking the voting eligibility.
3. Print ‘n’ natural numbers.
4. Area of a rectangle.
5. Area and circumference of a circle.
6. Checking the leap year.
7. Checking positive number or negative number.
8. Checking odd number or even number.

53
Exercise
Write PAC, algorithm, pseudocode and draw flowchart
for the following:
9. Greatest of three numbers.
10. Calculate the total marks of a student in 5 subjects.

54

You might also like