Você está na página 1de 29

Problem Solving

Concepts

Introduction
The most important skill for a computer scientist is
problem-solving:
the ability to formulate problems, think creatively about
solutions, and express a solution clearly and accurately.
Programming involves
problem solving and
translating the solutions into a computer program using
a programming language (a.k.a . coding)
A programmer skill: thinking ability to produce the
solution and knowledge about the programming language

Nature of problem
A problem in computer science is the statement of

specifications that requires a solution


Problem can be concrete or abstract
Concrete : they have known values to use in
making the solution e.g.:
i) Compute the area of a circle when its radius is 5.5 cm
ii) Ali drove to Abus house at 60 km/h. Abus house is 80
km away. Ali arrived at Abus house at 3:22 p.m. What time
did Ali leave?.
The solution is designed to solve a single specific problem

Nature of problem
Abstract : they have no particular values to be used in
making the solution e.g.:
i) Compute the area of a circle with a given
radius
ii) Ali drove to Abus house at 60 km/h. Abus house
is 80 km away. What time should Ali leave if he wants
to arrive at a certain particular time?.
iii) Calculate the monthly payment for a given loan
amount with a specific loan duration and interest
rate.
requires an abstract/general solution which can solve
several problems when concrete information is given.

Nature of problem
Abstract problems are the more common problem (well
focus more on solving abstract problems)
Abstract problems are the types that computer
scientists are faced with every day

2 Types of Problem
Solutions

Algorithmic Solutions
Steps involved in following a set of
instructions.
Ex. Calculating your checkbook balance

The solution will be the same each


time the algorithm is followed.
Most computers use
algorithmic types of problems.

2 Types of Problem
Solutions

Heuristic Solutions
Solutions that cant be reached by
following a direct set of steps.
Created by using reasoning built upon knowledge
and experience and by trial and error.
Ex. - Determining which stock is the best value
- How to decide which program to choose

for your undergraduate study

The results may not produce the same


results each time the algorithm is
executed.
Artificial intelligence deals with heuristic
types of problems.

6 Steps in Problem Solving in


Everyday Life
1. Identify the problem
If you dont know what the problem is, you cant
create a solution for it.
Ex.Calculate Payroll for 10 Employees
Ex.Provide directions

6 Steps in Problem Solving in


Everyday Life
2. Understand the problem

Must understand what is involved in


the problem before finding the
solution
Perform
Research - to form your own knowledge
base
Observations by
Watching current activity
Reviewing past activities

Ex. Giving instructions to someone on


how to find an address.

6 Steps in Problem Solving in


Everyday Life
3. Identify alternative ways to solve the
problem.
Rarely does a problem have only one solution.
Look at

Efficiency
Speed
Accuracy
Acceptability

Dont just find one solution and stop there.


Rarely is it the best solutionespecially
for novice programmers

6 Steps in Problem Solving in


Everyday Life
4. Select the best way to solve the problem
from the list of alternative solutions.
Establish criteria to help when performing
evaluations on each alternative
Include pros and cons for each solution
Select the best solution based
on the research

COP1000

McManus

11

6 Steps in Problem Solving in


Everyday Life
5. List instructions that enable
you to solve the problem using
the selected solution.
1. Create a numbered, step-by-step
set of instructions
2. Instructions must be
included in the knowledge base
So, whats a knowledge base?

COP1000

McManus

12

6 Steps in Problem Solving in


Everyday Life
6. Evaluate the solution.
Test the solution

Are the results accurate?


Does it satisfy the needs of the client?
Is it acceptable to the client?
Does the solution solve the original problem?

COP1000

McManus

13

Problem Solving with


Computers
Definitions:
Solution instructions followed to produce
best result (in step 5)
Result outcome, computer-assisted
answer
Program instructions for solution coded
using a computer language

014

Steps in Building a
Program
Analyzing the problem
Developing an algorithm
Writing the program (by the programmer,
including internal commenting)
Testing the program (by the programmer as
well as the user)

Steps in Building a
Program

Problem Analysis:
Before writing any program, a programmer must clearly
understand the problem,what input are to be used,the
desired output,the procedure that will produce the result
and the constraints to be considered.
The analysis starts by answering these questions:
What would be considered the input data?
What would be considered the output information?
What are the formulas/processes you have to create to
solve this solution?
Are there any special conditions/constraints?

Steps in Building a
Program
Problem Analysis:

Steps in Building a
Program
Writing Algorithm:

write the algorithm in pseudocode or flowchart

need to identify the basic steps clearly in the correct


order from what you found in the analysis step

typically, the steps follows a familiar pattern: input,


processing and output
test is to verify the correctness of the algorithm (hand
trace the values of the variables in every steps of your
algorithm)

Steps in Building a
Program
Test Algorithm:

pretend that you are the computer and follow the steps
of your algorithm explicitly while keeping track of how
the variables are changing.
hand trace the values of the variables in every steps of
your algorithm

Steps in Building a
Program
Coding:

develop the program by coding the algorithm


in a programming language, such as Java and
implement the program to the user environment such as
Android
need to have knowledge in the programming language in
terms of the language syntax
can use IDEs such as Eclipse, NetBeans etc to facilitate the
coding tasks.

Steps in Building a
Program
Testing/Debugging Program:

test to verify that the program meets the


requirements by creating test cases
test can avoid bugs such as logic errors
program also need to be maintained by
modifying it if the problem domain or
requirements changes or problem arises

Algorithm
An algorithm is a clear stepby-step
sequence of instructions to solve a
problem
analogy: recipe (refer next slide)
In mathematics and computing, an
algorithm is a procedure (a finite set of
well-defined
instructions)
for
accomplishing some task which, given
an initial state, will terminate in a
defined end-state. (source:Wikipedia)

Algorithm: Baking a Cake

Algorithm
comes from the 9th century Persian
mathematician
Abu Abdullah Muhammad ibn
Musa al-Khwarizmi.
2 ways to represent algorithm:
a) pseudo code
using English-like statements to list the
steps to be taken in the solution.
b) flowchart
using diagrams that employ symbols to
describe the workflow of steps involved in
the solution.

Pseudocode

Pseudocode statements usually perform input,


output, and processing (eg. arithmetic operations)
The input statements are to get data needed to
perform computation e.g.:
Input student name, student mark
Input length, width, height of a box
Input loan amount, interest rate, loan duration
The output statements are to display information for
the user e.g.:
Output "Enter Student Name: "
Output final grade
Output "The volume of the box is ", theVolume
Output "The loan montly payment = ",
monthlyPayment

Pseudocode

The processing statements are to perform


computation e.g.:
priceAverage = (price1 +price2 + price3
+ price4 + price5) / 5
theVolume = length x width x height
BMI = weight / (height x height)
These statements represent arithmetic
assignment statements. The right side of
the statement is the arithmetic expression
using standard mathematical notation.
The left side is where the result of the
computation is stored for further processing.

Pseudocode to calculate the


perimeter of a rectangle

Start
Output "Enter rectangle length: "
Input length
Output "Enter rectangle width: "
Input width
Calculate perimeter = 2 x(length + width)
Output perimeter
End

Flowchart

a graphical way of depicting a problem in terms of its


inputs, outputs, and processes.
the basic elements are as follows:
Rounded Rectangle (start/end of a program)

Parallelogram (program input and output)

Rectangle (processing)

Diamond (decision)

Arrow (execution flow)

Flowchart to calculate the perimeter of a


rectangle.

Start

Input length,
width

Perimeter = 2 x
(length + width)

Output
perimeter

Stop

Você também pode gostar