IT106-1103B-05 : Introduction to Programming Logic

Download Report

Transcript IT106-1103B-05 : Introduction to Programming Logic

Introduction to
Programming Logic
Instructor: Professor Stephen Osborne
What is a problem?
A problem can present itself in many forms. Examples of problems are as
follows:
• Question: What movie should we see?
• Need to do something: Generate the monthly payroll.
• Obstacle to progress: A part is missing that is causing work to halt on a
project.
• Status of a process: What is the status of the project?
• What-if simulation: How much money needs to be deducted from each pay
check to save for a down payment on a car?
• Evaluation of a solution to another problem: Should traveling be done by
train or car?
• Lack of information: How much money is left on the mortgage?
• Undesirable state: A person has gone into debt.
• Lack of control: A company has decided to downsize.
What is a problem solution?
There are many ways in which a problem can be solved. The following
Are some of the possibilities:
• Algorithm: An algorithm is a series of steps used to arrive at an
outcome that represents the best answer to a problem. It is a
specification of a behavioral process. An algorithm is a finite set of
instructions that govern a behavior step by step, such as the
manipulation of data. Problems that require algorithms are often the
problems that computers solve best (Sprankle, 2006, p. 5).
• Heuristic solutions: Heuristic solutions involve the use of human
ingenuity and reasoning. They are based on learning through
experience (Sprankle, 2006, p. 5).
• Passing of time: Some problems are solved simply by the passing of
time. An undesirable state may change, or it may no longer be a
problem.
• Gaining resources: The solution to a problem may be more
resources—such as more money, time, or people.
The Six Steps of Problem Solving
The six steps of problem solving include the following
1. Identify the problem: What is the problem that
needs to be solved?
2. Understand the problem: Are there considerations
that need to be taken into account?
3. Identify alternative ways to solve the problem:
There may be multiple solutions to the problem.
Write down all possible solutions, and at this point,
don’t rule anything out.
4. Select the best way to solve the problem from the list of
alternative solutions: Factors involved in selecting the best
solution include efficiency, schedule, cost, available resources,
or the need for a long-term solution.
5. List instructions that enable you to solve the problem using
the selected solution: When solving a problem with computers,
this can include pseudocode, or a combination of English and
code.
6. Evaluate the solution: Determine if the solution solves the
problem. If not, return to identifying and understanding the
problem.
Algorithmic Problem Solving
• Using best decision is found for solving
problem
• Step-by-step instructions are used in reaching
your chosen decision
• These steps are known as algorithm
Heuristic Problem Solving
• Cannot be always be reached through a series
of steps but instead require using:
• Reasoning built on knowledge and experience
• Process of trial and error
Difficulties with Problem Solving
Following are some of the difficulties that people encounter with problem
solving:
•
•
•
•
•
•
Lack of training or natural ability for problem solving
Fear of decision making, even when faced with evidence
Inadequate use of the problem-solving steps
Personal biases inserted into the process
Misunderstanding the problem—solving the wrong problem
Inability to explain or abstract the problem in a form that is usable by a
computer
Using a proven process, such as the six steps problem solving process, can help
facilitate understanding and solving a problem. When attempting to use
computers to solve problems, it is important to abstract the problem in a form
that the computer can use. Remember, computers work best with step-bystep or algorithmic solutions.
Algorithm Design
• There are two sides to computer science. The first is the
applied side, which involves programming and system and
software design. The other side is the theoretical side. The
theoretical side fuels the applied side. One example of this is
all well-known algorithms, such as sorting and searching,
pathfinding, or optimization algorithms are all designed in a
general manner on the theoretical side.
• The applied side takes the breakthroughs from the theoretical
side and applies it to specific situations. However, regardless
of how applied the focus of a programmer might be, there are
always situations where no prior algorithm applies and a new
one must be developed.
There are several generic methods of problem
solving that are widely used for many problems.
These methods include brute force, greedy,
divide and conquer, and backtracking.
•
•Solving any given problem efficiently requires
both consideration of all possible methods as
well as a bit of creativity, insight, and luck.
Brute Force
• The brute force method of algorithm design is intended to
bring into mind hammering a screw into a board of wood. The
solution may not be elegant or efficient, but it will work.
Generally, this involves considering every aspect of every
element of an array, regardless of whether each element is
important. It is an exhaustive, programmatic consideration of
everything, which takes time. Generally, brute force methods
are inefficient but still serviceable in many situations.
• The biggest place where the brute force method shows its
difficulty is in very large numbers of input. A brute force
method can perform some tasks quickly with 10 elements, but
it may bog down with 10 million elements. This is particularly
telling if an efficient algorithm could cut that number in half,
by a tenth, or even drop it down to the square root of the brute
force method.
Greedy
• Greedy algorithms involve considering the largest
portion of the problem set first. For example, if the
problem considers how to pack a set of items into a
bag or a box, the greedy method would put the
largest item in first, followed by the second largest,
and so forth.
• The greedy method will occasionally find an
efficient method; however, more often than not, the
solution will be similar to a brute force method.
Divide and Conquer
• Suppose that a problem involved shuffling a deck of cards. It
could be determined that a deck of cards is shuffled if each
half of the deck is shuffled and randomly recombined. Due to
the fact each half could be considered a deck, what occurs is a
recursive solution. The only component left to define is the
base case, which would say that the dividing of decks stops
when the deck only consists of one card.
• Afterwards, each subdeck could be randomly combined to
reproduce the larger deck. This could be continued until the
full deck is shuffled. This method is called divide and
conquer, when the algorithm seeks to divide the problem into
smaller problems which are, in turn, solved in the same
manner.
Backtracking
• A method often called backtracking is one other
possibility toward finding an efficient solution by
starting with the solution and finding the problem.
• If an algorithm can be developed that takes an
appropriate solution and slowly takes parts of it
away until it comes to the problem and the algorithm
can be reversed, it is sometimes possible to
determine an efficient solution to a problem.
Summary
• Algorithm design is often considered an art
and a science. It often takes luck in addition to
perseverance to solve any problem. However,
research often yields a preexisting solution.
Now Let’s Look at our Assignment
Task Type: Discussion Board 2 Deliverable Length: See Assignment Details
Points Possible: 75 Due Date: 9/09/2011 11:59:59 PM CT
Students will be expected to post their first initial discussion board posting by Wednesday of each week.
Discussion posts will be graded and late submissions will be assigned a late penalty in accordance with the
late penalty policy found in the syllabus.
NOTE: All submission posting times are based on midnight Central Time.
Students are expected to post their responses to peers by Sunday. NOTE: All submission posting times are
based on midnight Central Time.
Primary Task Response: Within the Discussion Board area, write 400–600 words that respond to the
following questions with your thoughts, ideas, and comments. This will be the foundation for future
discussions by your classmates. Be substantive and clear, and use examples to reinforce your ideas:
Choose a problem that you would like to see corrected in your community that could be solved through
an algorithmic process. Write an essay that demonstrates how you can use the 6 problem-solving steps
discussed in your text to solve the problem. The step-by-step instructions for step 5 should be detailed
and clear enough that they could be executed by someone else without asking any questions.
Responses to Other Students: Respond to at least 2 of your fellow classmates with a reply of at least 100
words about their primary task response regarding items you found to be compelling and enlightening. To
help you with your discussion, please consider the following questions:
What did you learn from your classmate's posting?
What additional questions do you have after reading the posting?
What clarification do you need regarding the posting?
What differences or similarities do you see between your posting and other classmates' postings?
Phase 1 Discussion Board 2 Resolution
Use the 6 step problem solving method for the below scenario:
• Choose a problem that you would like to see corrected in your community that could
be solved through an algorithmic process. Write an essay that demonstrates how you
can use the 6 problem-solving steps discussed in your text to solve the problem. The
step-by-step instructions for step 5 should be detailed and clear enough that they
could be executed by someone else without asking any questions.
Or
Choose a problem of your own selection and then apply the 6 step problem solving
method to it. The main idea here is to know how apply the problem solving
technique to any situation.
Six Step Solving Process Example
Scenario of a problem
“What to do this evening”
1. Identify the problem. How do individuals wish to spend the evening?
2. Understand the problem. With this simple problem, also, the knowledge base of the
participants must be considered. the only solutions that should be selected are ones that
everyone involved would know how to do. You probably would not select as a possible
solution playing a game of chess if the participants did not know how to play.
3. Identify alternatives
•Watch television
•Invite friends over
•Play video games
•Go to the movies
•Play miniature golf
•Go to the amusement park
•Go to a friend’s party
The list is complete only when only you can think of no more alternatives.
4. Select the best way to solve the problem.
•Weed out alternatives that are not acceptable, such as those that cost too
much money or do not interest one of the individuals involved.
•Specify the pros and cons of each remaining alternative.
•Weigh the pros and cons to make the final decision. this solution will be
the best alternative if all other steps were completed well.
5. Prepare a list of steps (instructions) that will result in a fun evening.
6. Evaluate the solution. Are we having fun yet? If nobody is having fun,
then the planner needs to review the steps to have a fun evening to
see whether anything can be changed, if not then the process must start
again.
Step 5 List of Instructions
• Chose going to a movie as it was an economical
choice due to it got us out of the house but only cost
a little over $20.00.
• Examined the entertainment section of daily news
paper to select a movie that everyone would like.
• Dressed for a casual evening.
• Got into the car and drove to the movie theater.
• Went into the movie theater and found seating to our
liking.
• Watched movie and enjoyed it very much.
Problem Solving in Programming
Why do we need problem-solving techniques, and
how does this relate to computer programming?
• Problem solving is something that we do all day, every
day, almost without thinking about the process at all.
With bigger problems and with problems that have
outcomes of higher risk, such as with strategic business
issues, it is wise to follow the basic steps of problem
solving. This will mitigate the risk that comes when the
problem is not fully understood.
•Each step in a problem-solving technique is
related to a programmer's step to building a
program. A programmer will first seek to fully
understand the problem. He or she will then plan
the logic and flow of a program.
•Next, the programmer writes pseudocode and
translates it into the chosen programming
language. Finally, the programmer implements
the program and begins testing and adjusting as
needed.
What is the role of logic in problem solving,
and what does it look like in business?
•Logic helps us organize activities into the right
order and display particular outcomes for
defined circumstances.
•Many people are familiar with logic problems:
brainteasers in which decision tables are used to
find a solution.
An example of such a problem would sound like this:
•There are three appointment openings at a dental
office, and three individuals want to come in on that
day. Person A must arrive before 10 a.m., person B
cannot come before 10:30 a.m., and person C wants to
be the first appointment of the day.
•How can you accommodate each individual? This is
the kind of work that can be automated by
implementing the right logic and business rules.
What is the purpose of a decision table?
•Decision tables are used to help the programmer
understand all the possible outcomes to a situation and
to help the programmer build all variables into the
program's decision engine.
•Decision tables are made up of four parts: conditions,
possible combinations, possible actions based on the
conditions, and actual actions to be taken based on
conditions.
Definition
Causes
Cause 1
Cause 2
Cause 3
Effects
Effect 1
Effect 2
Combinations
Values 1 2 3 4 5 6 7 8
Y, N
Y Y Y Y NNNN
Y, N
Y Y NNY Y NN
Y, N
Y NY NY NY N
X
X
X
X
X
X
• Components
• A decision table lists
causes and effects in a
matrix. Each column
represents a unique
combination.
• Purpose is to structure
logic
Cause = condition
Effect = action = expected results
Why is problem solving critical in information
systems?
•Problem-solving technique and following all the steps
to analyze all sides of the problem are imperative
when considering information systems. If a business
person bypasses one of the critical steps, then a system
or program could be built that does not actually solve
the problems at hand.
• It is expensive and time consuming to build a
system, so being thorough and systematic to ensure
that that it fully solves the problem at hand is simply
good business.
Programming Tools of the Trade
• Software programs are often created in teams. The person
who writes the code may not be the person who designed the
solution. In many development projects, one person or team
meets with the customer to define the requirements while
another team member designs and documents the solution. A
development team codes the solution, and a software quality
assurance team tests the code.
• To support the team development environment, tools have
been developed to document the results of the various phases
of the project. During the analysis phase of the project, tools
help to clarify the problem and facilitate communication with
other team members.
The following are analysis/design tools
Problem analysis chart
Structure chart
Input-processing-output (IPO) chart
Pseudocode
Flowchart
It is important that analysis and design take place
before writing code.
Problem Analysis Chart
The problem analysis chart is a way to document the analysis of a problem. It consists
of four sections
What data do I have?
What is the expected outcome?
What processing must take place?
What are alternative solutions?
The problem analysis chart documents the information that is known. It helps to clarify
the expected outcome. The third step concentrates on the solution or what processing
will take place to solve the problem.
It is important to consider alternate solutions because initial solutions may not be
feasible due to budget, time, or human resource constraints. Overall, the tool helps to
focus the team on relevant information that can be used to solve a problem.
Problem Analysis Chart
Given Data
Required Results
Section1.Data given in the problem or provided by the user.
These can be known values or general names for
data, such as price, quantity, and so forth.
Section 2. Requirements for the output reports. This is includes
the information needed and the format required.
Processing Required
Solution Alternatives
Section 3.List of processing required. This includes equations
or other types of processing, such as sorting,
searching , and so forth.
Section 4. List of ideas for the solution of the problem.
Structure Chart (Interactivity Chart)
•The structure chart, also known as an interactivity chart,
shows the relationships of subtasks for a given solution. The
tasks are displayed in hierarchical order from general to
specific.
•A structure chart shows what will happen but not how it will
happen. The following example shows that the solution will
input data, compute results, and print results. It does not show
how those routines will work.
A structure chart is read from top to bottom, and left to right. It may
indicate how many times a subroutine is called. Each module is assigned
a tracking number. The number is used to reference the module in other
charts, and it is also used to reflect the relationship to the other modules
in the chart. Structure charts show activities and in what order they must
occur for a program to implement a solution.
IPO Chart
•The IPO chart is an extension of information in the problem
analysis chart. The information in the chart includes input,
output, processing, and optionally a module tracking number.
An IPO chart should have a section built for each subroutine
or task in a process.
• The input shows what data are needed in a particular task.
The processing is a verbal description of what is happening in
the task. Any formulas or calculations should be documented
in this section. The output section documents the information
that is output from the task.
Input, Processing, Output IPO Chart
Input
All input data (from
Section 1 from the
Problem Analysis Chart)
Processing
All processing in steps
(from sections 3 and 4 of
the problem analysis
chart)
Module Reference
Module Reference from
the interactivity chart
Read
Calc
Print
Output
All output requirements
(from Sections 1 and 2 of
the problem analysis c
chart)
Study the following "millionaire list" IPO chart example:
Problem: Construct an algorithm that will produce a list
containing the names of millionaires who paid less in taxes
than you (or the user).
Input
Processing
Output
User taxes
Millionaire names
Millionaire taxes
If millionaireTaxes < List of
userTaxes then add
millionaires who
millionaireName to list paid more in
taxes than the
user.
Pseudocode
• Pseudocode (or algorithm) is the set of specific
instructions drawn from the IPO chart. These steps are
written in a combination of English and code. As the
programmer becomes more comfortable with a
programming language, the pseudocode generally
becomes more like code and less like English.
• When writing pseudocode, no assumptions are allowed,
and no steps can be skipped. The steps are listed in the
order they are to be executed, and all steps must be
included. An algorithm is a logical step-by-step
solution.
Flowchart
• A flowchart is a graphic representation of an
algorithm. Flowcharts capture the flow of data and
any logical decisions that must be made concerning
the data.
• In large and complex programs, creating a
flowchart for each coding construct may not be
possible. In some cases, pseudocode is a better
choice for mapping logic to code.
A flow chart can therefore be used to:
•Define and analyze processes.
•Build a step-by-step picture of the process for
analysis, discussion, or communication.
•Define, standardize or find areas for improvement in
a process
•Also, by conveying the information or processes in a
step-by-step flow, you can then concentrate more
intently on each individual step, without feeling
overwhelmed by the bigger picture.
How to Use the Tool:
Most flow charts are made up of three main types of symbol:
Elongated circles, which signify the start or end of a process.
Rectangles, which show instructions or actions.
Diamonds, which show decisions that must be made.
Within each symbol, write down what the symbol represents. This could be the start or finish
of the process, the action to be taken, or the decision to be made.
Symbols are connected one to the other by arrows, showing the flow of the process.
•To draw the flow chart, brainstorm process tasks, and list
them in the order they occur. Ask questions such as "What
really happens next in the process?" and "Does a decision need
to be made before the next step?" or "What approvals are
required before moving on to the next task?“
•Start the flow chart by drawing the elongated circle shape,
and labeling it "Start".
•Then move to the first action or question, and draw a
rectangle or diamond appropriately. Write the action or
question down, and draw an arrow from the start symbol to
this shape.
•Work through your whole process, showing actions and
decisions appropriately in the order they occur, and linking
these together using arrows to show the flow of the process.
Where a decision needs to be made, draw arrows leaving the
decision diamond for each possible outcome, and label them
with the outcome. And remember to show the end of the
process using an elongated circle labeled "Finish".
•Finally, challenge your flow chart. Work from step to step
asking yourself if you have correctly represented the sequence
of actions and decisions involved in the process. And then (if
you're looking to improve the process) look at the steps
identified and think about whether work is duplicated, whether
other steps should be involved, and whether the right people
are doing the right jobs.
Example:
The example below shows part of a simple flow chart
which helps receptionists route incoming phone calls
to the correct department in a company:
Start of Program
True
False
Processing
Instructions
Processing
Instructions
.
Assignment Decision Flowchart
Summary
• There may be a redundancy of data or duplication of
information among the charts. While this is true to a
certain extent, it is still important to organize the
problem before beginning to write code.
• Different team members will be using different
charts as they work on their part of the solution.
Documentation
• There are two types of documentation: internal and external.
The purpose of internal documentation is to communicate the
implementation details of the program to other programmers.
Internal documentation includes the programmer’s comments
that explain what the code is doing.
• Comments should be included at the start of each function to
explain the purpose of the function. Comments should also be
included for complicated equations or lines of code. Files
should be commented on; at the beginning of each file should
be the author, date, and the description of the file’s contents.
Dependencies should be noted in the file documentation.
Solution Testing
• Solution testing is one of the most important parts of
program creation. When testing a solution, run the
program with typical data. Testing should also occur
with data that are outside of the limits of the data set.
Testing should exercise all of the selection routes that
an algorithm will exercise.
• For example, both the true and false paths of a decision
statement need to be tested. All repetition statements
should be tested. When errors are found, they should be
fixed, and the testing should start again from the
beginning. It is possible that the corrections made could
cause an error in another part of the program code.
•Coding the solution involves putting the solution in a
language that the computer can understand. This step
will be easier if the developer has accomplished the
previously discussed analysis and design steps.
•The temptation is to start coding an idea as soon as a
programmer is presented with a problem and begins
imagining ways to solve it. The importance of
following the problem solving steps, however, bears
repeating.
What are the six steps of problem solving?
1. Identify the problem: What is the problem that needs to be solved?
2. Understand the problem: Are there considerations that need to be
taken into account?
3. Identify alternative ways to solve the problem: There may be multiple
solutions to the problem. Write down all possible solutions, and at this
point, don’t rule anything out.
4. Select the best way to solve the problem from the list of alternative
solutions: Factors involved in selecting the best solution include
efficiency, schedule, cost, available resources, or the need for a long-term
solution.
5. List instructions that enable you to solve the problem using the
selected solution: When solving a problem with computers, this can
include pseudocode, or a combination of English and code.
6. Evaluate the solution: Determine if the solution solves the problem. If
not, return to identifying and understanding the problem.
What is an algorithmic solution to a problem?
Algorithmic problem solving is performed by using a set of instructions
and then finally using best decision is found for solving problem.
What is a heuristic solution to a problem?
Heuristic problem solving cannot be always be
reached through a series of steps but instead
require using things as reasoning built on
knowledge and experience and the process of
trial and error.
What is the purpose of a decision table?
•Decision tables are used to help the programmer
understand all the possible outcomes to a situation and
to help the programmer build all variables into the
program's decision engine.
•Decision tables are made up of four parts: conditions,
possible combinations, possible actions based on the
conditions, and actual actions to be taken based on
conditions.
References
http://vig-fp.prenhall.com/bigcovers/0132492644.jpg
Sprankle, M. (2006). Problem solving and programming concepts (7th ed.). Upper Saddle River,
NJ: Prentice Hall.
CTU Course Materials
http://www.mindtools.com/pages/article/newTMC_97.htm
http://free.clipartof.com/details/112-Flying-Witch-And-Pumpkins-Free-Halloween-VectorClipart-Illustration
http://activerain.com/blogsview/735316/lake-ridge-time-for-pumpkins-and-fall-festival-fun
http://s370.photobucket.com/albums/oo149/Weatherfords/Random/
http://www.picturesdepot.com/wallpapers/4801/halloween+pumpkins.html
http://www.wallpapertop.com/halloween/Halloween-Ghost_544.html