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

1.0 Introduction_Problem_Solving_IPOS_Algorithms

Chapter 1 introduces computer science, emphasizing its role in problem-solving through algorithms and abstraction. It outlines the definition of computer science, the types of software, and the importance of programming languages in creating efficient solutions. The chapter also details a systematic approach to problem-solving, including understanding the problem, formulating a model, developing algorithms, and evaluating solutions.

Uploaded by

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

1.0 Introduction_Problem_Solving_IPOS_Algorithms

Chapter 1 introduces computer science, emphasizing its role in problem-solving through algorithms and abstraction. It outlines the definition of computer science, the types of software, and the importance of programming languages in creating efficient solutions. The chapter also details a systematic approach to problem-solving, including understanding the problem, formulating a model, developing algorithms, and evaluating solutions.

Uploaded by

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

Chapter 1

An Introduction to Computer Science and


Problem Solving
Source : http://people.scs.carleton.ca/~lanthier/teaching/
ProcessingNotes/COMP1405_Ch1_IntroductionToComputerScience.pdf

What is in This Chapter ?


This first chapter explains what computer science is all about. It will help you understand that
the goal of a computer scientist is to solve problems using computers. You will see how
problems are formulated by means of algorithms and how the process of abstraction can be
used to break problems down to easily manageable pieces. Finally, we will discuss the notion
of efficiency.
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011

1.1 What is Computer Science ?


Computers are used just about everywhere in our society:

Communications: internet, e-mail, cell phones


Word Processing: typing/printing documents
Business Applications: accounting, spreadsheets
Entertainment: games, multimedia applications
Database Management: police records, stock market
Engineering Applications: scientific analysis, simulations
Manufacturing: CAD/CAM, robotics, assembly
... many more ...

A computer is defined as follows (Wikipedia):

A computer is a programmable machine that receives input, stores


and manipulates data, and provides output in a useful format.

In regards to today’s computers, the “machine” part of the computer


is called the hardware, while the “programmable” part is called the
software.

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.

Software itself can be broken down into 3 main categories:

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..)

Middleware: is a set of services that allows multiple processes running


on one or more machines to interact. Most often used to support and
simplify complex distributed applications. It can also allow data
contained in one database to be accessed through another. Middleware
is sometimes called plumbing because it connects two applications and
passes data between them. (e.g., web servers, application servers).

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:

Computer science is the study of the theoretical foundations of


information and computation, and of practical techniques for their
implementation and application in computer systems (Wikipedia).

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.

In mathematics, a solution is often expressed in terms of formulas and equations. In


computer science, the solution is expressed in terms of a program:

A program is a sequence of instructions that can be executed by a


computer to solve some problem or perform a specified task.

However, computers do not understand arbitrary instructions written in


English, French, Spanish, Chinese, Arabic, Hebrew, etc..

Instead, computers have their own languages that they understand. Each of these
languages is known as a programming language.

A programming language is an artificial language designed


to automate the task of organizing and manipulating information, and
to express problem solutions precisely.

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.

Traditional programming languages were known as structural programming languages (e.g.,


C, Fortran, Pascal, Cobol, Basic). Since the late 80's however, object-oriented
programming languages have become more popular (e.g., JAVA, C++, C#)

-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):

Java, C, C++, PHP, Python, C#, VisualBasic, Objective-C, Perl, Ruby

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.

We therefore recently adjusted this course to use a language called Processing


(www.processing.org) which is a JAVA-based language with much less overhead in getting
started in programming. In addition, the graphical nature of the Processing language allows
for more visual applications to be developed quicker and easier. You will learn more about
this language as the course goes on.

When thinking of jobs and careers, many people think that


computer science covers anything related to computers (i.e.,
anything related to Information Technology). However, computer
science is not an area of study that pertains to IT support, repairing
computers, nor installing and configuring networks. Nor does it
have anything to do with simply using a computer such as doing
word-processing, browsing the web or playing games. The focus
of computer science is on understanding what goes on behind the
software and how software/programs can be made more efficiently.

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

algorithms and data structures


- investigates efficient ways of storing, organizing and using data

programming methodology and languages


- investigates different approaches to describing and expressing problem solutions

computer elements and architecture


- investigates the design and operation of computer systems

However, in addition, they also identify other important fields of computer science:

software engineering distributed computation


artificial intelligence computer-human interaction
computer networking & communication computer graphics
database systems operating systems
parallel computation numerical & symbolic computation
-4-
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011

1.3 Problem Solving


Regardless of the area of study, computer science is all about solving problems with
computers. The problems that we want to solve can come from any real-world problem or
perhaps even from the abstract world. We need to have a standard systematic approach to
solving problems.

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:

Problem Solving is the sequential process of analyzing information related to a


given situation and generating appropriate response options.

There are 6 steps that you should follow in order to solve a problem:

1. Understand the Problem


2. Formulate a Model
3. Develop an Algorithm
4. Write the Program
5. Test the Program
6. Evaluate the Solution

Consider a simple example of how the input/process/output works on a simple problem:

Example: Calculate the average grade for all students in a class.

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.

2. Process: add them all up and compute the average grade.

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

STEP 1: Understand the Problem:


It sounds strange, but the first step to solving any problem is to make sure that
you understand the problem that you are trying to solve. You need to know:

o What input data/information is available ?


o What does it represent ?
o What format is it in ?
o Is anything missing ?
o Do I have everything that I need ?
o What output information am I trying to produce ?
o What do I want the result to look like … text, a picture, a graph … ?
o What am I going to have to compute ?

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.

STEP 2: Formulate a Model:


Now we need to understand the processing part of the problem. Many problems
break down into smaller problems that require some kind of simple mathematical
computations in order to process the data. In our example, we
are going to compute the average of the incoming grades. So,
we need to know the model (or formula) for computing the
average of a bunch of numbers. If there is no such “formula”,
we need to develop one. Often, however, the problem breaks
down into simple computations that we well understand.
Sometimes, we can look up certain formulas in a book or online if
we get stuck.

- 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:

Average1 = (x1 + x2 + x3 + … + xn) / n

where the result will be a number from 0 to 100.

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:

Average2 = (y1 + y2 + y3 + … + yn) / n

where the result will be a number from 0 to 12.

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.

STEP 3: Develop an Algorithm:


Now that we understand the problem and have
formulated a model, it is time to come up with a precise
plan of what we want the computer to do.

An algorithm is a precise sequence of


instructions for solving a problem.

- 17 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011

Some of the more complex algorithms may be considered “randomized algorithms” or


“non-deterministic algorithms” where the instructions are not necessarily in sequence
and in may not even have a finite number of instructions. However, the above definition
will apply for all algorithms that we will discuss in this course.

To develop an algorithm, we need to represent the instructions in some way that is


understandable to a person who is trying to figure out the steps involved. Two
commonly used representations for an algorithm is by using (1) pseudo code, or (2)
flow charts. Consider the following example (from Wikipedia) of solving the problem
of a broken lamp. To the right is an example of a flow chart, while to the left is an
example of pseudocode for solving the same problem:

Pseudo Code

1. IF lamp works, go to step 7.


2. Check if lamp is plugged in.
3. IF not plugged in, plug in lamp.
4. Check if bulb is burnt out.
5. IF blub is burnt, replace bulb.
6. IF lamp doesn’t work buy new lamp.
7. Quit ... problem is solved.

Notice that:

pseudocode is a simple and


concise sequence of English-like
instructions to solve a problem.

Pseudocode is often used as a way of


describing a computer program to someone
who doesn’t understand how to program a computer. When learning to program, it is
important to write pseudocode because it helps you clearly understand the problem that
you are trying to solve. It also helps you avoid getting bogged down with syntax details
(i.e., like spelling mistakes) when you write your program later (see step 4).

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

sequence: listting instructtions step by


b step in order
o (often numbered)

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 lamp is not plugged


d in
then plug it in

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

You will notice thatt the bold in


n the above
e examples highlights thet specificc control strructure.
You will notice thatt for the con
ndition and repetition structures,
s t portion of the pseu
the udocode
ondition or the repeat loop are indented a bit so as to make
that is part of the co m it clea
ar that
- 19 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011

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:

if (bulb is burned out) then {


replace bulb
}
otherwise {
buy new lamp
}

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

1. set the sum of the grade values to 0.


2. load all grades x1 … xn from file.
3. repeat n times {
4. get grade xi
5. add xi to the sum
}
6. compute the average to be sum / n.
7. print the average.

- 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:

n = 5, x1 = 92, x2 = 37, x3 = 43, x4 = 12, x5 = 71 … result should be 51


n = 3, x1 = 1, x2 = 1, x3 = 1 ……………………….… result should be 1
n = 0 …………………………………………………… result should be 0

STEP 4: Write the Program:


Now that we have a precise set of steps for solving the problem,
most of the hard work has been done. We now have to
transform the algorithm from step 3 into a set of instructions that
can be understood by the computer.

Writing a program is often called "writing code" or


“implementing an algorithm”. So the code (or source code)
is actually the program itself.

Without much of an explanation, below is a program (written in processing) that


implements our algorithm for finding the average of a set of grades. Notice that the
code looks quite similar in structure, however, the processing code is less readable and
seems somewhat more mathematical:

Pseudocode Processing code (i.e., program)

1. set the sum of the grade values to 0. int sum = 0;


2. load all grades x1 … xn from file. byte[] x = loadBytes("numbers");
3. repeat n times { for (int i=0; i<x.length; i++)
4. get grade xi sum = sum + x[i];
5. add xi to the sum
} int avg = sum / x.length;
6. compute the average to be sum / n. print(avg);
7. print the average.

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

Compiling is the process of converting a program into instructions that can


be understood by the computer.

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.

STEP 5: Test the Program:


Once you have a program written that compiles, you need to make sure that it solves
the problem that it was intended to solve and that the solutions are correct.

Running a program is the process of telling the


computer to evaluate the compiled instructions.

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.

Bugs are problems/errors with a program that cause it to stop working or


produce incorrect or undesirable results.

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.

STEP 6: Evaluate the Solution:


Once your program produces a result that seems correct,
you need to re-consider the original problem and make
sure that the answer is formatted into a proper solution to
the problem. It is often the case that you realize that your
program solution does not solve the problem the way that
you wanted it to. You may realize that more steps are
involved.
- 22 -
COMP1405/1005 – An Introduction to Computer Science and Problem Solving Fall 2011

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.

a. Making a peanut butter and jam sandwich

b. Putting together a jigsaw puzzle

c. Playing the game of musical chairs

d. Replacing a flat tire on your car

e. Getting home from school today

f. Emptying a case of drinks into your refrigerator

g. Shopping for groceries (from entering store to leaving store)

- 23 -

You might also like