Вы находитесь на странице: 1из 60

MODULE 3

Unit I
Ms. J. Blake
What is a Problem?
• A problem is not always something negative,
but simply a matter or situation that could be
better, improved on or just requires a solution.
Problem Solving
• Simply the process of finding a solution to a
problem.
• It is a cognitive process that can be achieved
by following a framework of activities.
Problem
• “Di Trenz” is giving a 20% discount to all
customers whose final bill exceeds $5000.
Calculate Dwayne’s Final bill if he spent $8700.
Problem
Your little brother Kevin comes
home all bloody and bruised. He kicks the chair, yells at your
Mum, and
swears a blue streak. It turns out
that he got beaten up by a bunch of
kids on the way home from school.
The kids are bigger and have a tough
reputation, but your little brother
also has a bit of a mouth on him and
always fights back. Kevin asks you to help him out, since you are
even bigger
than the kids who beat him up.
What do you do?
Step to Problem Solving
• Define the problem
• Analyze the problem (using tools, such as
questionnaires, interviews, observation,
reviewing documents),
• identify and evaluate possible solutions,
• Select and justify the optimal solution
• Implement
• Review
Define problem

Gather facts

Consider possibilities
based on facts

Create action Plan

Implement Action Plan

If problems stop
If problems Persist
Observe results

Problem resolved,
terminate process
Step to Problem Solving
• Define the problem- Identifying the actual
issues. One may write them down (Problem
statement). By doing this you will ensure that
the right problem is addressed
• Analyze the problem- Finding out what is
actually wanted of us and how we can proceed
to the solution. Identify inputs outputs and
processing that is required to complete tasks
Step to Problem Solving
• Propose and evaluate Possible solutions-
Problems may have multiple solutions. Identify
solutions that may be available, select the most
suitable for solving the issues, identify the
strengths and weaknesses of each solution.
• Select and justify the optimal solutions
Based on solution evaluations, choose the most
efficient or suitable (optimal) that will solve the
problem
Step to Problem Solving
• Implement and Review
The optimal solution is put into practice and
tested. It is observed/ reviewed to see if it
works correctly and addresses the issues for
which it was implemented.
STEPS TO PROBLEM SOLVING in COMPUTING
(scrambled)
1. Analyze the problem
2. Develop an algorithm
3. Identify and define the problem
4. Test the algorithm to see if it works
5. Test and debug the program
6. Write a program in a programming language

NB. An algorithm is a set of instruction steps to solve a


given problem.
A program is a set of instructions written for the
computer aimed to solve a problem, using
codes/computer language
Identify and define the problem
• Outlining the reasons why a problem may
have occurred or the root of the problem
• Understanding the details behind the problem
Analyzing the problem
Taking complex problems and breaking it into
easily managed components. This is the most
important step and may use one of two
approaches:
a) Top down design
b) Stepwise refinement
Approaches to analyzing a problem
• Top down design
This analyzes the problem (bigger or complex)
and breaks it into smaller problems or sub-
problems. You would solve each sub problem
and combine the solutions to solve the
original problem
• Stepwise refinement
Is a more precise way of doing the top down
design. This breaks a problems into smaller
problems, then those smaller problems into
even smaller problems.
Significant parts of problem solving
• Input
Input is data that you will need in order to calculate
or solve problem. ( think about what data would
have to be entered to solve the problem)
• Processing
This tells you what needs to be done to solve the
problem (Comparing, converting values)
• Output
Contain solutions or end results that you require
• Storage (sometimes)
Results /input stored to be used/reused in aprogram.
• IPO CHARTS
An easier way of identifying the components or
parts of a problem is to create an IPO chart.
An IPO chart is a table that groups the different
instructions and statements according to the
type of component.
Example: Write a program that adds the weight
of two people to show a total weight.
INPUT PROCESSING OUTPUT
Weight of person 1 Total weight= weight of Show the total weight
person 1 +weight of person
2

Weight of person 2
Role of Information In solving
problems
• The Minister of agriculture is interested in
marketing and exporting breadfruit and ackee
to Asian and European countries.
• What is your best advise for him in getting this
done? How can he go about doing this?
• What kind of information will he need to
collect to address the concern?
Identification of information required in solving
problems…(1)
• There is no doubt that improvements in information and
communication technologies have narrowed the
information gap between the rich and poor sections of
the world.
• Information, which used to be available only in the rich
world, is now more likely to be available in the
developing world as well.
• For an individual, an organization, or a country to be
successful in this new world order, it is imperative that
one be able to analyze real life problems not only in
terms of the purely physical resources required for its
solution, but also in terms of the information necessary.
Identification of information required in solving
problems…(2)

• An example may illustrate the point. A successful exporter of


sauces, jellies and condiments in one of the Caribbean
territories had discovered that there was a much larger
demand for the guava jelly than the exporter could meet.
• At first sight, this might seem like a simple supply problem. It
might seem that there simply were not enough guavas
available to produce jelly in the required quantities.
• However, a little probing showed this preliminary analysis to
be incorrect.
• It turned out that the exporter sourced his guavas by sending
persons to the various markets in the territory to buy up all
the guavas they could find.
Identification of information required in solving
problems…(3)

• Clearly, this was not the most efficient way of ensuring a


steady supply of the fruit.
• It would have been much better if sellers of guavas were
aware of the exporter's need for their fruit or,
alternatively, if the exporter knew which producers might
have guavas for sale.
• In other words, the supply problem arose not because
there were not enough guavas available; it arose because
the exporter did not know where to find them.
• Thus, what might have seemed to have been a supply
problem was in fact an information problem.
Identification of information required in solving
problems…(4)

• An important criterion to getting information (i.e. correct


information) is research.
• Yes, research is an intimidating or even fearful concept,
but you should realize that you are engaged in research
in many of your day-to-day activities.
• For example, finding out the time of the day, the location
of the next class or looking up a number in a telephone
directory are all examples of simple research.
• It is therefore essential to identify information necessary
to solve personal, commercial, scientific and social
problems.
Categorization of information
in solving problems
• In the solution of a problem, information can be categorized as:

Category Synonyms

Essential crucial, vital, important, necessary, needed, required

Desirable wanted, useful, advantageous

Extraneous irrelevant, inappropriate, unrelated

Cosmetic superficial, made-up, aesthetic


Characteristics of (Valuable) Information
Sources
Criteria for selecting Characteristics for
appropriate rejecting or accepting a
information sources piece of information
• Time • Bias
• Criticality • Accuracy
• Accuracy • Cultural context
• Currency • Completeness
• Currency of information
• Refereed sources
Characteristics of Information
 Distortion Security
Disguise Shareability
Reliability Availability
Inconsistency Lifespan
Incomprehensibility Information as a
Subject to saleable commodity
interpretation Format
Value Medium
Relevance
Confidentiality
Developing and Testing an Algorithm
• An algorithm is a set of instruction steps to
solve a given problem. When you write it, you
use the I.P.O. components in a logical
sequence to solve a problem.
• Algorithms can be written in the narrative, in
pseudocode or through flowcharts.
• Pseudocode is a language that mimics real
programming language; its commands and
syntax are similar to those of real
programming.
Developing and Testing an Algorithm
Cont’d
• Narrative means writing out the step-by-step
solution to the problem in full sentences.

• Flowcharts are diagrams that arrange the


components of a program in a logical
sequence which helps to avoid logical errors.
• Flow charts show different shapes linked using
arrowed lines.
Characteristics of an
Algorithm
1. Must have a finite number of
steps
2. Precise
3. Unambiguous
4. Sequential: flow of control from
one process to another
5. Must terminate.
Developing and Testing an Algorithm
In algorithm data can be either CONSTANTS or
VARIABLES.

• A constant is data that has a known value and


keeps that value throughout the program
• A constant does not change its value; this is
any data that will not change its value
throughout a process or calculation
EXAMPLE: Calculating the area of a circle (pi)
Developing and Testing an Algorithm

• Variables can change their value throughout a


program, or do not have a known, fixed value.

• EXAMPLE: Adding the prices of three items to


get a total.
Developing and Testing an Algorithm
Note: Before creating an algorithm you need to
know and be able to distinguish between the
different data types.
INTEGERS
Integers are whole numbers, so they cannot
have any decimal points and cannot represent
fractions. The can be represented as positive
or negative numbers.
Data Types cont’d
Real Numbers
Real numbers are numbers that include decimal
points. They are sometimes called floating
point numbers and can be negative.
This is any number that is likely to have a
decimal point or could change its value so it
becomes a decimal number.
Data Types cont’d
NOTE: Not all data is numeric. A program can
output text or messages such as “pass” or
“fail”, and input values that consist of
characters or symbols.
• Characters or Strings
Characters are single letters (‘a’) or symbols (‘/’)
A string is a group of characters. (“Good Luck!”)
Control Structures
• Control structures are instructions or
statements that determine the flow of control
of steps in an algorithm.
• They indicate how instructions will be carried
out.
• TYPES: Sequence; Selection; Iteration
(Repetition)
Control Structures
• Sequential: Instructions are written in the order it
should be executed
• Selection: A choice has to be made between two or
more options. (IF statements). The choice of options
is dependent on whether the condition is true of
false.
IF <Condition> THEN
<Instruction if condition is true>
ELSE
<Instruction if condition is false>
ENDIF
Control Structures
• Iteration: Used to repeat a number of
processes a number of times. It represents the
repetitive stages in processing.
Types: Definite or indefinite iteration
• Write an algorithm that prompts the user
to read the amount of rainfall for each
day of the month of November. Print the
total rainfall.
• Write an algorithm to read the amount of
rainfall for each month of a year. Print
the total rainfall for the year and the
monthly average.
Work Questions cont’d
• Write a pseudocode program that calculates
the total wage of an employee. The program
should allow the user to input the employee
name, ID number and number of hours
worked. Employees are paid $100 an hour. If
the employee worked over 40 hours then they
should receive an additional bonus of 25% of
their wage. Output the employees name and
total wage. (6 Marks)
M=1
N=2 M N P
P=1 2 1
WHILE M<30 DO 5
N=M*2
P=N+1
M=P+2
ENDWHILE
Complete the trace table above
N J S

6
Read N
For J = 1 to N Do
6
If J < 5 then
S= (N + J) *2 6
Else
6
S= (N-J) * 2
Endif 6
Endfor 6
• Perfect Pizza wants to install a system to record orders for pizza and
chicken wings.
• When regular customers call Perfect Pizza on the phone, their
phone number goes automatically into the Pizza system. The phone
number invokes the name, address, and last order date comes
automatically up on the screen.
• Once the order is taken, the total, including tax and delivery, is
calculated. Then the order is given to the cook. A receipt is printed.
• Occasionally, special offer (coupons) is printed so the customer can
get a discount. Drivers who make deliveries give customers a copy
of the receipt and coupon (if any). Weekly totals are kept for
comparison . with last year’s performance Draw a context diagram
for Perfect Pizza
Steps to Program Implementation

1. Create the source


code
2. Interpret and
compile
3. Link
4. Test and execute
5. Maintain the
program
Programming Paradigm
• A programming paradigm is a style or “way” of
programming.
The major paradigms are:
• Procedural
• Object-oriented
• Functional
• Imperative Programming (IP) is one of the popular Programming
Paradigms which executes a sequence of
steps/instructions/statements in some order.

• Declarative programming is when you write your code in such a


way that it describes what you want to do, and not how you want
to do it. It is left up to the compiler to figure out the how.
Programming Paradigm
• See additional power point sent
Additional reading
• http://lildiaz00.blogspot.com/2010/11/project-4-advantages-and-
disadvantages.html
• https://sites.google.com/site/cs4217jan2011team2/programming-
paradigms/object-oriented-paradigm
• https://neonbrand.com/website-design/procedural-programming-vs-
object-oriented-programming-a-review/
• http://www.lispcast.com/procedural-paradox
Procedural paradigm
• Procedural programming uses a list of
instructions to tell the computer what to do
step by step.
• Procedural programming relies on procedures,
also known as routines.
• Each step could actually be a complex subtask
that includes many smaller steps.

Example of procedural languages:


Fortran, Cobol, C
Advantages of Procedural
• Its relative simplicity, and ease of
implementation of compilers and interpreters.
• The ability to re-use the same code at
different places in the program without
copying it.
• An easier way to keep track of program flow.
The ability to be strongly modular or
structured.
• Need for less memory.
Disadvantages of Procedural
• Data is exposed to whole program, so no
security for data.
• Difficult to relate with real world objects.
• Difficult to create new data types reduces
extensibility.
• Importance is given to the operation on data
rather than the data.
Key features:
https://sites.google.com/site/mattysassignme
nts/home/p1
Object Oriented Programming
• Object-oriented programming, or OOP, is an
approach to problem-solving where all
computations are carried out using objects.
• An object is a component of a program that
knows how to perform certain actions and
how to interact with other elements of the
program.
Example of Object oriented languages:
Java, Perl, Python, C#
Advantages of Object-Oriented
• Code Reuse and Recycling: Objects created for Object Oriented
Programs can easily be reused in other programs.
• Encapsulation : Once an Object is created, knowledge of its
implementation is not necessary for its use.
• Objects have the ability to hide certain parts of themselves from
programmers. This prevents programmers from tampering with values
they shouldn't.
• Object controls how one interacts with it, preventing other kinds of
errors.
• Design Benefits: Large programs are very difficult to write. Object
Oriented Programs force designers to go through an extensive planning
phase, which makes for better designs with less flaws.
• Software Maintenance: An Object Oriented Program is much easier to
modify and maintain than a non-Object Oriented Program. So although
a lot of work is spent before the program is written, less work is
needed to maintain it over time.
Disadvantages of Object-Oriented
• Size: Object Oriented programs are much larger
than other programs..
• Effort: Object Oriented programs require a lot of
work to create. Specifically, a great deal of
planning goes into an object oriented program
well before a single piece of code is ever written.
• Speed: Object Oriented programs are slower than
other programs, partially because of their size.
Other aspects of Object Oriented Programs also
demand more system resources, thus slowing the
program down.
Object-Oriented Vs. Procedural
• In procedural programming data and functions
are stored in separate memory location, while in
OOP data and functions are stored in same
memory location
• In Procedural Programming the data is not the
focus and would not be introduced until needed
in the program.
• With Procedural Programming the program
would wait until the flow of the program got to
the data vs. Object-oriented Programming where
the data would be stored when the class was
loaded.
Object-Oriented Vs. Procedural
• Object-oriented Programming uses objects
where Procedural Programming uses records.
Functional Programming Paradigm
• Functional programming is an approach to
problem solving that treats every computation
as a mathematical function.
• The outputs of a function rely only on the values
that are provided as input to the function and do
not depend on a particular series of steps that
precede the function.
Example of Functional languages:
• Erlang, Haskell, Lisp, Scala
https://www.linkedin.com/pulse/definition-functional-programming-rainer-grimm
https://wiki.haskell.org/Functional_programming#Features_of_functional_languages
Advantages of Functional Paradigm
• Bugs-Free Code: As FP they don’t raise any side-
effects that means we can write Error-free code
or Bugs-free code or Less Error-prone Code.
• Efficient Parallel Programming & Better
Performance : They use only Immutable Data and
use Independent Units to write programs.
Programs can therefore run Parallel or
Concurrently because each runs independently
without changing state. Because of this
applications gain better performance.
Advantages of Functional Paradigm
• Increase Readability and Maintainability:
Functional Programming (FP) also improves
Readability and Maintainability because they
work Independently and they don’t change
state.
Disadvantages of Functional Paradigm
• Requires Lot of Memory:FP don’t have state.
They always creates new Objects to perform
actions instead of modifying existing objects.
Because of this, FP Applications takes lot of
memory.
• Functional programming can be less efficient
than other languages.
• Purely Functional Languages are not really
used in commercial software development.
• Semantic error. Writing invalid program logic
that produces incorrect results when the
instructions are executed.
• The syntax of the source code may be valid,
but the algorithm being employed is not.
• Syntatic errors (Syntax): When the rules of
the programming language are not followed.

Вам также может понравиться