1.0 Introduction_Problem_Solving_IPOS_Algorithms
1.0 Introduction_Problem_Solving_IPOS_Algorithms
Since computers are used everywhere, you can get involved with
computers from just about any field of study. However, there are
specific fields that are more computer-related than others. For example. the fields of electrical
engineering and computer systems engineering primarily focus on the design and
manufacturing of computer hardware, while the fields of software engineering and computer
science primarily focus on the design and implementation of software.
System Software: is designed to operate the computer’s hardware and to provide and
maintain a platform for running applications. (e.g., Windows, MacOS, Linux, Unix, etc..)
Application Software: is designed to help the user perform one or more related
specific tasks. Depending on the work for which it was designed, an application can
manipulate text, numbers, graphics, or a combination of these elements.
(e.g., office suites, web browsers, video games, media players, etc…)
-2-
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
The area of software design is huge. In this course, we will investigate the basics of creating
some simple application software. If you continue your degree in computer science, you will
take additional courses that touch upon the other areas of system software and middleware.
Software is usually written to fulfill some need that the general public, private industry or
government needs. Ideally, software is meant to make it easier for the user (i.e., the person
using the software) to accomplish some task, solve some problem or entertain him/herself.
Regardless of the user’s motivation for using the software, many problems will arise when
trying to develop the software in a way that produces correct results, is efficient ad robust,
easy to use and visually appealing. That is where computer science comes in:
So, computer science is all about taking in information and then performing some
computations & analysis to solve a particular problem or produce a desired result, which
depends on the application at hand.
Computer science is similar to mathematics in that both are used as a means of defining and
solving some problem. In fact, computer-based applications often use mathematical models
as a basis for the manner in which they solve the problem at hand.
Instead, computers have their own languages that they understand. Each of these
languages is known as a programming language.
A programming language “boils down to” a set of words, rules and tools that
are used to explain (or define) what you are trying to accomplish. There are many different
programming languages just as there are many different "spoken" languages.
-3-
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
There are also other types of programming languages such as functional programming
languages and logic programming languages. According to the Tiobe index (i.e., a good site
for ranking the popularity of programming languages), as of February 2011 the 10 most
actively used programming languages were (in order of popularity):
For many years, we used JAVA as the basis in this course, due to its popularity as well as its
ease of use. However, JAVA does have some drawbacks for new programmers, pertaining to
some overhead in getting started with the language.
The Computer Sciences Accreditation Board (CSAB) identifies four general areas that it
considers crucial to the discipline of computer science:
theory of computation
- investigates how specific computational problems can be solved efficiently
However, in addition, they also identify other important fields of computer science:
Since we will be using computers to solve problems, it is important to first understand the
computer’s information processing model. The model shown in the diagram below assumes a
single CPU (Central Processing Unit). Many computers today have multiple CPUs, so you
can imagine the above model duplicated multiple times within the computer.
- 14 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
A typical single CPU computer processes information as shown in the diagram. Problems are
solved using a computer by obtaining some kind of user input (e.g., keyboard/mouse
information or game control movements), then processing the input and producing some kind
of output (e.g., images, test, sound). Sometimes the incoming and outgoing data may be in
the form of hard drives or network devices.
In regards to problem solving, we will apply the above model in that we will assume that we are
given some kind of input information that we need to work with in order to produce some
desired output solution. However, the above model is quite simplified. For larger and more
complex problems, we need to iterate (i.e., repeat) the input/process/output stages multiple
times in sequence, producing intermediate results along the way that solve part of our
problem, but not necessarily the whole problem. For simple computations, the above model is
sufficient.
It is the “problem solving” part of the process that is the interesting part, so we’ll break this
down a little. There are many definitions for “problem solving”. Here is one:
There are 6 steps that you should follow in order to solve a problem:
1. Input: get all the grades … perhaps by typing them in via the keyboard or by
reading them from a USB flash drive or hard disk.
3. Output: output the answer to either the monitor, to the printer, to the USB flash
drive or hard disk … or a combination of any of these devices.
As you can see, the problem is easily solved by simply getting the input, computing something
and producing the output. Let us now examine the 6 steps to problems solving within the
context of the above example.
- 15 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
In our example, we well understand that the input is a bunch of grades. But we need to
understand the format of the grades. Each grade might be a number from 0 to 100 or
it may be a letter grade from A+ to F. If it is a number, the grade might be a whole
integer like 73 or it may be a real number like 73.42. We need to understand the
format of the grades in order to solve the problem.
We also need to consider missing grades. What if we do not have the grade for every
student (e.g., some were away during the test) ? Do we want to be able to include that
person in our average (i.e., they received 0) or ignore them when computing the
average ?
We also need to understand what the output should be. Again, there is a formatting
issue. Should we output a whole or real number or a letter grade ? Maybe we want
to display a pie chart with the average grade. It is our choice.
Finally, we should understand the kind of processing that needs to be performed on the
data. This leads to the next step.
- 16 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
In order to come up with a model, we need to fully understand the information available
to us. Assuming that the input data is a bunch of integers or real numbers x1,x2,…,xn
representing a grade percentage, we can use the following computational model:
That is very straight forward (assuming that we knew the formula for computing the
average of a bunch of numbers). However, this approach will not work if the input data
is a set of letter grades like B-, C, A+, F, D-, etc.. because we cannot perform addition
and division on the letters. This problem solving step must figure out a way to produce
an average from such letters. Thinking is required.
After some thought, we may decide to assign an integer number to the incoming letters
as follows:
A+ = 12 B+ = 9 C+ = 6 D+ = 3 F=0
A = 11 B=8 C=5 D=2
A- = 10 B- = 7 C- = 4 D- = 1
If we assume that these newly assigned grade numbers are y1,y2,…,yn, then we can
use the following computational model:
As for the output, if we want it as a percentage, then we can use either Average1
directly or use (Average2 / 12), depending on the input that we had originally. If we
wanted a letter grade as output, then we would have to use (Average1/100*12) or
(Average1*0.12) or Average2 and then map that to some kind of “lookup table” that
allows us to look up a grade letter according to a number from 0 to 12.
Do you understand this step in the problems solving process ? It is all about figuring
out how you will make use of the available data to compute an answer.
- 17 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
Pseudo Code
Notice that:
Although flowcharts can be visually appealing, pseudocode is often the preferred choice
for algorithm development because:
It can be difficult to draw a flowchart neatly, especially when mistakes are made.
Pseudocode fits more easily on a page of paper.
Pseudocode can be written in a way that is very close to real program code,
making it easier later to write the program (i.e., in step 4).
Pseudocode takes less time to write than drawing a flowchart.
Pseudocode will vary according to whoever writes it. That is, one person’s pseudocode
is often quite different from that of another person. However, there are some common
control structures (i.e., features) that appear whenever we write pseudocode.
- 18 -
COMP140
05/1005 – An
n Introduction to Computer Science and Problem Solving Fall 2011
1
These are
a shown here
h along with
w some examples:
e
1. Make sure
s switchh is turned on
o
2. Checkk if lamp is plugged
p in
3. Checkk if bulb is burned out
4. …
cond
dition: ma aking a deccision and doing
d one th
hing or som
mething
elsse dependinng on the outcome
o of the decision.
if bulb is burned
b out
then repplace bulb
otherwise e buy new lamp
repettition: rep
peating som
mething a fiixed numbe
er of times or
o until
some conditio
on occurs.
repeat
get a ne ew light bulb
put it in
n the lamp
until lampp works or no
n more bu
ulbs left
repeat 3 times
t
unplug lamp
plug intto different socket
…
stora
age: sto
oring inform
mation for usse in instructions furth
her down the list
x a new bulb
count 8
jump
ping: be
eing able to jump to a specific
s step when nee
eded
if bulb works
w
then goto step 7
these are kinds “inner steps” that belong to that structure. Some people will us brackets to
indicate what is in or out of a control structure as follows:
repeat {
get a new light bulb
put it in the lamp
} until (lamp works or no more bulbs left)
repeat 3 times {
unplug lamp
plug into different socket
}
The point is that there are a variety of ways to write pseudocode. The important thing
to remember is that your algorithm should be clearly explained with no ambiguity as to
what order your steps are performed in.
Whether using a flow chart of pseudocode, you should test your algorithm by manually
going through the steps in your head to make sure that you did not forget a step or a
special situation. Often, you will find a flaw in your algorithm because you forgot about
a special situation that could arise. Only when you are convinced that your algorithm
will solve your problem, should you go ahead to the next step.
Consider our previous example of finding the average of a set of n grades stored in a
file. What would the pseudocode look like ? Here is an example of what it might look
like if we had the example of n numeric grades x1 ... xn that were loaded from a file:
Algorithm: DisplayGrades
- 20 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
It would be wise to run through the above algorithm with a real set of numbers. Each
time we test an algorithm with a fixed set of input data, this is known as a test case.
You can create many test cases. Here are some to try:
For now, we will not discuss the details of how to produce the above source code. In
fact, the source code would vary depending on the programming language that was
used. Learning a programming language may seem difficult at first, but it will become
easier with practice.
The computer requires precise instructions in order to understand what you are asking it
to do. For example, if you removed one of the semi-colon characters ( ; ) from the
program above, the computer would become confused as to what you are doing
because the ( ; ) is what it understands to be the end of an instruction. Leaving one of
them off will cause your program to generate what is known as a compile error.
- 21 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011
The longer your program becomes, the more likely you will have multiple compile errors.
You need to fix all such compile errors before continuing on to the next step.
When you run your program, if all is well, you should see the
correct output. It is possible however, that your program works
correctly for some set of data input but not for all. If the output of your program is
incorrect, it is possible that you did not convert your algorithm properly into a proper
program. It is also possible that you did not produce a proper algorithm back in step 3
that handles all situations that could arise. Maybe you performed some instructions out
of sequence. Whatever happened, such problems with your program are known as
bugs.
You should fix as many bugs in your program as you can find. To find bugs effectively,
you should test your program with many test cases (called a test suite). It is also a
good idea to have others test your program because they may think up situations or
input data that you may never have thought of. The process of finding and fixing errors
in your code is called debugging and it is often a very time-consuming “chore” when it
comes to being a programmer. If you take your time to carefully follow problem solving
steps 1 through 3, this should greatly reduce the amount of bugs in your programs and
it should make debugging much easier.
For example, if the result of your program is a long list of numbers, but your intent was to
determine a pattern in the numbers or to identify some feature from the data, then simply
producing a list of numbers may not suffice. There may be a need to display the information
in a way that helps you visualize or interpret the results with respect to the problem. Perhaps
a chart or graph is needed.
It is also possible that when you examine your results, you realize that you need additional
data to fully solve the problem. Or, perhaps you need to adjust the results to solve the
problem more efficiently (e.g., your game is too slow).
It is important to remember that the computer will only do what you told it to do. It is up to you
to interpret the results in a meaningful way and determine whether or not it solves the original
problem. It may be necessary to re-do some of the steps again, perhaps going as far back as
step 1 again, if data was missing.
So there you have it. Those are the 6 steps that you should follow in order to solve problems
using computers. Throughout the course, you should try to use this approach for all of your
assignments. It is a good idea to practice problem solving to make sure that you understand
the process. Below are some practice exercises that will help you practice the first 3 steps of
the problem solving process. Later, you will gain experience with steps 4 through 6.
PRACTICE EXERCISES
Formulate a model and then develop an algorithm for each of the following problems. In each
case, start with a simple algorithm and then try to think about situations that can realistically go
wrong and make appropriate adjustments to the algorithm. Keep in mind that there is no
“right” answer to these problems. Everyone will have a unique solution.
- 23 -