






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































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.
Computers are used just about everywhere in our society:
A computer is defined as follows (Wikipedia):
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:
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:
However, in addition, they also identify other important fields of computer science:
There are aspects of each of the above fields can fall under the general areas mentioned previously. For example, within the field of database systems you can work on theoretical computations, algorithms & data structures, and programming methodology.
As you continue your studies in computer science, you will be able to specialize in one or more of these areas that interest you. This course, however, is meant to be an introduction to programming computers with an emphasis on problem solving.
This is your first programming course here in the School of Computer Science at Carleton. You have some more core programming courses coming up after this one. Here is a break- down of how this course fits in with your first 2 years of required programming courses:
Of course, there are other computer science courses as well. These are just the core courses that nearly everyone is required to take. After this course is over, you should understand how to write computer programs. In the winter term, you will take COMP1406/1006 which is a more detailed course focused on Object-Oriented programming in JAVA. Together, these two courses give you a solid programming background and you will be able to learn other computer languages easily afterwards … since they all have common features. If you want to do well in this course, attend all lectures and tutorials and do your assignments.
Here is what it looks like when you are working with Processing:
Each program is called a “sketch” in Processing. The top left play button starts your program which brings up a window (shown yellow here with a picture of a teddy bear). Since, many processing programs are meant to be animations, there is also a stop button beside the play button to stop the program. You should explore the Processing IDE (i.e., Integrated Development Environment) a little to get used to it.
Processing uses the same syntax as JAVA. That means, Processing code looks almost exactly like JAVA code. So when you are programming in Processing, you are actually learning JAVA as well. However, Processing has been designed in a way that makes it easier to get you started because some of the overhead in getting your first program working is hidden.
As you may recall, Processing is a graphics-based language and therefore we will spend a lot of time and effort drawing various things on the screen. When drawing anything, it is important to specify where you want to draw.
The output screen is organized as a 2-dimensional (2D) grid of pixels organized by the standard x and y coordinate system. That is, given an (x,y) pair, which we call a point , the x specifies a number of horizontal pixels from the origin (or start location at the top-left of the screen) while y specifies the number of vertical pixels from the origin. So, point (0,0) is the origin and is at the top-left of the screen.
Lets write our first program. Lets draw a simple house like this one shown here. This involves drawing a square, a triangle, a rectangle and a dot.
Since Processing is a graphical-based language, there are pre-defined functions for drawing shapes. Each of these functions requires some parameters to specify further information about how to do the drawing such as locations and dimensions of what we are trying to draw.
Looking at the table of functions on the next page, it should be clear that we need to call the following functions in order to draw our house:
All we need to do then is to figure out what the parameters should be.
Here are the ones that we can use to draw 2-dimensional shapes:
Function Description Example
point (x,y) draws a single dot at the location specified by x and y.
line (x 1 ,y 1 ,x 2 ,y 2 ) draws a line from location ( x1, y1) to location ( x2, y 2 ).
rect (x,y,w,h)
draws a rectangle with its top- left at location ( x, y ). The width and height of the rectangle are w and h. If w and h are equal, a square is drawn.
Next, we’ll need to know roughly how big the drawing area will be. Sometimes it is a good idea to know this even before we decide upon the dimensions so that our house is the “proper” size according to the context of the drawing area. Assume that we choose a drawing area of 300 x 300 pixels.
Now we need to decide where within the area the house will be located and assign point values to the corners of the house … remembering that (0,0) is at the top left corner of the drawing area.
Note that for rectangles, we just need to know the top left corner, along with the dimensions.
Here is the final program:
The size(w,h) function in Processing allows you to specify the size of the drawing area. If you do not call this method, the default window size will be 100 x 100.
In our case, if we did not call size(300,300) , then a small window would appear and the house would not be shown since it would be drawn off the window’s visible area as shown here:
“Drawing things off the screen” is a common error that many students encounter when writing graphical programs.
So … we have just created our first Processing program. Notice that each function call ends
the program. It is like having a period at the end of an English sentence to indicate that the sentence has ended and a new one is about to begin. Leaving off a semi-colon somewhere is considered a syntax error (i.e., like a spelling or grammar error) and will prevent your program from running:
If this happens, Processing usually highlights the line in your program that appears just after the line that is missing the semicolon.
It tells the Processing interpreter that the rest of the line is to be ignored by the program. This allows you to put English-like explanations throughout your code so that anyone who read your program later (possibly you yourself) will understand what you are doing in that part of the program. It is always a good idea to use comments but not too many of them such that you code becomes too cluttered. Use just enough to make it clear what you are doing in that part of your program. Throughout the course, take notice of where and how many comments are being used in the example programs that we do together.
example, this could be a comment at the top of your program:
This program draws a simple house that has A frame, a roof and a door with a door knob. /*
Forgetting one / in your comment will produce this error:
When using computers, colors are often represented as:
Here are a few color-related functions that you can make use of:
Function Description
stroke (gray);
Sets the border-color for lines & shapes (e.g., points, lines, arcs, rectangles, triangles, ellipses) to a shade of gray specified by the value of the gray parameter.
stroke (r, g, b);
Sets the border-color for shapes to the color with the given amount of red, green and blue, specified by parameters r , g and b , respectively. noStroke (); Sets the border-color to be transparent (i.e., glass).
fill (gray);
Sets the fill-in-color for enclosed shapes (e.g., rectangles, triangles, ellipses) to a shade of gray specified by the value of the gray parameter.
fill (r, g, b);
Sets the fill-in-color for enclosed shapes to the color with the given amount of red, green and blue, specified by parameters r , g and b , respectively. noFill (); Sets the fill-in-color to be transparent (i.e., glass).
background (gray); Sets the drawing area’s background to a shade of gray specified by the value of the gray parameter.
background (r, g, b);
Sets the drawing area’s background to the color with the given amount of red, green and blue, specified by parameters r , g and b , respectively.
background ( loadImage (“hills.png”));
Sets the drawing area’s background to the image specified by the given image file name which may be a png , gif or jpg file.
See if you can produce the following images (of course, you would need to supply your own 300 x 300 image for the example with the background hills):
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.
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.
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.
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 x 1 ,x 2 ,…,xn representing a grade percentage, we can use the following computational model:
Average1 = (x 1 + x 2 + x 3 + … + 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 y 1 ,y 2 ,…,yn , then we can use the following computational model:
Average2 = (y 1 + y 2 + y 3 + … + 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 Average 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/10012)* or (Average10.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.
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.
instructions for solving a problem.
COMP
These a
You will You will that is p
05/1005 – An
are shown h
notice that notice that art of the co
n Introduction
here along w
ence : list
dition : ma els
tition : rep so
age : sto
ping : be
t the bold in t for the con ondition or
to Computer
with some e
ting instruct
1. Make s 2. Check 3. Check 4. …
aking a dec se dependin
if lamp is then pl
if bulb is b then rep otherwise
peating som me conditio
repeat get a ne put it in until lamp
repeat 3 t unplug plug int …
oring inform
x ← a count ←
eing able to
if bulb w then
n the above ndition and the repeat
Science and
examples:
tions step b
sure switch k if lamp is p k if bulb is b
cision and d ng on the o
not plugged ug it in
burned out place bulb e buy new
mething a fi on occurs.
ew light bu n the lamp p works or n
times lamp to different
mation for us
new bulb ← 8
jump to a s
works goto step
e examples repetition s loop are in
Problem Sol
by step in o
h is turned o plugged in urned out
doing one th outcome of
d in
lamp
ixed numbe
lb
no more bu
socket
se in instru
specific ste
highlights t structures, t dented a b
ving
order (often
on
hing or som the decisio
er of times o
ulbs left
ctions furth
p when nee
the specific the portion it so as to m
numbered
mething n.
or until
her down th
eded
c control str of the pseu make it clea
Fall 2011
e list
ructure. udocode ar that
1
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 x 1 ... xn that were loaded from a file:
Algorithm: DisplayGrades