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

Lecture#2

The document discusses problem solving in programming, emphasizing the importance of understanding, devising a plan, implementing, and evaluating solutions using Polya's four steps. It highlights the need for clear algorithms and instructions for computers, contrasting them with human understanding. Additionally, it provides examples of algorithms and flowcharts to illustrate the problem-solving process.

Uploaded by

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

Lecture#2

The document discusses problem solving in programming, emphasizing the importance of understanding, devising a plan, implementing, and evaluating solutions using Polya's four steps. It highlights the need for clear algorithms and instructions for computers, contrasting them with human understanding. Additionally, it provides examples of algorithms and flowcharts to illustrate the problem-solving process.

Uploaded by

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

CS-1002

PROGRAMMING
FUNDAMENTALS
Activity Time

What is Problem Solving?


Problem Solving
• In psychology, problem solving refers to a state of desire for reaching
a definite 'goal' from a present condition that either is not directly
moving toward the goal, is far from it, or needs more complex logic
for finding a missing description of conditions or steps toward the
goal. – Wikipedia

• Considered the most complex of all intellectual functions


Problem Solving
◾The process of defining a problem, searching for relevant
information and resources about the problem, and of discovering,
designing, and evaluating the solutions for further opportunities.
Includes:
▪ Finding an Answer to a Question
▪ Figuring out how to Perform a Task
▪ Figure out how to Make Things Work

◾Not enough to know a particular programming language… Must be


able to problem solve…
◾Very desirable to be a good Problem Solver in any CIS discipline.
Polya’s 4 Steps of Problem Solving
• U – Understand the Problem
• D – Devise a Good Plan to Solve

• I – Implement the Plan

• E – Evaluate the Solution


Example:
Solving Math Word Problem
• Read the Problem: Understand the description of problem or
scenario, identifying the knowns and unknowns.
• Decide how to go about solving the problem: Determine what steps
need to be taken to reach the solution
• Solve the Problem: Write the solution
• Test the Answer: Make sure the answer is correct
Solving Computing Problems
• In general, when we solve a computing problem we are taking some
inputs, processing (performing some actions on) the inputs, and then
outputting the solution or results.
• This is the classic view of computer programming – computation as
calculation
• Polya’s steps (UDIE) can be very effective when applied to solving
computing problems
Applying Polya’s Problem Solving to Programming
Step 1 - Understand the Problem
• What is the Problem to be solved? What is the unknown? What is the
condition? What is the data? What is needed to solve the problem?
What actions need to take place?
• Identify the inputs and outputs
• Identify the processes needed to produce the outputs from the given
inputs
• Draw a figure. Introduce suitable notation.
• Isolate Principle parts of the problem.
Applying Polya’s Problem Solving to Programming
Step 2 - Devise a Plan
• Find connections between the knowns and unknowns.
• Simplify: Break the problem into smaller sub-problems
• Design a solution
• Make a plan or list of actions to implement the solution
• Algorithm / Flowchart / Psuedocode
Applying Polya’s Problem Solving to Programming
Step 2 - Devise a Plan (cont.)
• Algorithm
• A FINITE set of clear, executable steps that will eventually
terminate to produce the desired outcome
• Logical design used to solve problems – usually a list of
actions required to perform task
• Pseudocode
• Written like program code but more “English Like” and
doesn’t have to conform to language syntax
• Flowchart
• Diagram that visually represents the steps to be performed
to arrive at solution.
Applying Polya’s Problem Solving to Programming
Step 3 - Implement the Plan
• Implement in a Programming Language
• Carry out the plan checking the preliminary results at each step.
• Code A Little Test A lot
Applying Polya’s Problem Solving to Programming
Step 4 - Evaluate the Solution
• Run the Code
• Check results repeatedly and thoroughly
• Use numerous test cases or data sets
• Use highly varied test case, including expected as well as and unexpected
cases
• Look for new solutions
• Is there a better, easier, or more efficient solution
• Can other problems be solved using these techniques?
Summary
• U - Read the Problem Statement
• Identify the inputs, outputs, and processes
• D - Decide how to Solve the Problem
• Create an Algorithm / Flowchart / Psuedocode
• I - Program the Code
• Implement in Programming Language
• E - Test the Solution
• Run the Code using numerous, varied test cases
Activity Time

8-ball problem.
Problem Statement
You are given 8 identical looking balls. One of
them is heavier than the rest of the 7 (all the
others weigh exactly the same). You are
provided with a simple mechanical balance, and
you are restricted to only 2 uses of it.

Write steps to find the heavier ball.


Homework

13-ball problem.
ALGORITHM
• A sequence of instruction or steps to perform a certain task.
• An algorithm is a sequence of unambiguous instructions/operations
for solving a problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
ALGORITHM
Correct vs. Incorrect Algorithms
• We say that an algorithm is correct when:
✓ It finds a solution in a finite number of steps.
✓ It translates every input instance into the
correct output.

• An algorithm is incorrect when there is at


least one input instance for which the
algorithm gives an incorrect output.
REMARKS
• There may be infinite ways to solve a problem.

• It means there may be infinite algorithms.

• Which algorithm is the best?


• We mainly focus on two perimeters
• Speed
• Accuracy
Fact: computers are dumb machines

• Basic property of a computer (a machine):

• Computers do what we tell them to do


• Unfortunately, computer do not necessarily do what we want
them to do....

(Because we can make mistake in telling the computer what we


want to do... These mistakes are called "bugs")
Algorithm

• Definition: algorithm

Dictionary definition:

• Algorithm = a step-by-step procedure for solving a


problem or accomplishing some task, especially by
means of a computer
Computer Algorithms

• Computer Algorithm:

is an algorithm that can be executed by a computer


Computer Algorithms (cont.)

• Properties of computer algorithms:

• The steps in an algorithm must be consists of operations that can


be executed by a computer
• The step in an algorithm must be unambiguous
(Remember that a dumb machine like a computer will do what it is
told to do.

Resolving ambiguity requires some thinking (intelligence) which


• Computers
computers cannot do !) cannot think.
Algorithm development
• We will now illustrate the process of developing an algorithm
• An algorithm always accomplishes some well-defined task or solves some
well-defined problem
• The task/problem that we will use to illustrate the process of developing an
algorithm is:

• Replacing a burned out light bulb


Instruction for humans on replacing a burned out light bulb
• Typical instructions given to humans on how to replace a
light bulb:

• Remove the burned-out bulb


• Insert a new bulb

These brief instructions assume a lot of common sense


knowledge that a machine does not have !!!
Instruction for humans on replacing a burned out light bulb (cont.)

• What can go wrong if a machine uses these instructions:

• A machine does not know how to remove a bulb


It could yank the bulk out of its socket and damage the
fixture in the process.
• A machine does not know how to insert a bulb
• A machine can replace the bulb with one that has an
inadequate wattage (too bright or too dim)
Instructions for computers on replacing a burned out light bulb

• Computers have no common sense knowledge (really


dumb)
Instructions for computers must be given very explicitly (in
"baby steps")
Instructions for computers on replacing a burned out light bulb
• Example of how you would instruct a computer to change a light bulb:
[The following instructions will remove the burn-out bulb]
repeat until (bulb comes free of socket)
{
turn bulb in counter-clockwise direction
}
[The following instructions will find a suitable bulb]
select a new bulb
repeat until (wattage of bulb selected = wattage of old bulb)
{
discard the selected bulb select another bulb
}
[The following instructions will insert the new bulb]
repeat until (bulb is secure in socket)
{
turn bulb in clockwise direction
}
A real life algorithm

• Here is a famous algorithm that you can try out:

• Take 2 pieces of paper and write on one piece "A" and on the other
piece "B":

• Write a positive integer on each piece of paper.


A real life algorithm (cont.)
• Euclid's Algorithm:
As long as one of the number is not zero (0) do
{
if ( number on A ≥ number on B )
replace the number on A by the value (A - B)
otherwise
replace the number on B by the value (B - A)
}
The Greatest Common Divisor (GCD) of the numbers A
and B is the non-zero number on one of the papers
A real life algorithm (cont.)

• Example of the Euclid Algorithm:

• We start with the following 2 number:

• Since number on A < number on B, we replace the


number on B (= 36) by the value (B − A) = 8:
A real life algorithm (cont.)
• Now number on A ≥ number on B, so we replace the number on A (=
28) by the value (A −B) = 20:

• Again, number on A ≥ number on B, so we replace the number on A


(= 20) by the value (A −B) = 12:
A real life algorithm (cont.)
• Yet again, number on A ≥ number on B, so we replace the number
on A (= 12) by the value (A −B) = 4:

• This time, number on B > number on A, so we replace the number


on B (= 8) by the value (B −A) = 4:
A real life algorithm (cont.)
• Now, number on A ≥ number on B, so we replace the number on A (=
4) by the value (B −A) = 0:

• One of the number on the papers is now zero

According to the Euclid's Algorithm, the other number (= 4) is


equal to the GCD of (28, 36)
Data Operations
Tasks

Human
Algorithm Instruction
Language Computer’s
Pictorial
Language
Representation

Flowchart Program
Data

Constant Variable
Flowchart
A good flowchart should communicate a process clearly and effectively. When
starting out, it's a good idea to focus on a few steps.

✓ Determine the purpose or function of the flowchart


✓ Add steps and connect them with arrows
✓ Add decisions or split paths
✓ Show any loops back to previous steps
✓ Share your flowchart

Most flowcharts should be built using only the Start/End and Action or Process
symbols and should follow a very basic set of best practices. Sticking to these
primary flowchart symbols is the best way to ensure that your diagram will be
easy to understand.
Algorithm Example 1

Example 1: Write an algorithm to determine a student’s final grade


and indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
Algorithm Example 1

Input a set of 4 marks


Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Algorithm Example 1

Step 1: Input M1,M2,M3,M4


Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Algorithm Example 2

Example 2: Write an algorithm and draw a flowchart to convert


the length in feet to centimeter..

Pseudo Code
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with 30
Print length in cm (LCM)
Algorithm Example 2

You might also like