Title: PsyD58
1PsyD58
- Experimental Microcomputer Laboratory
- Notes for First Class
2Goals for Today
Learn the basics of getting into the
editor Learn the distinction between projects
and nodes, and learn how to create a new project
(and, eventually, adding nodes to an existing
project). Create your first program(s) while
learning about - parts of a program -
functions - defining (and initializing)
variables variable types - basic operators -
inputting and outputting information (cin and
cout) - if statements (along with else if
and else) - loops (for and while)
3Basics
If they are allowed to just boot, the machines
will go into a unix login prompt to get to
windows instead - press the reset key -
immediately after the memory test hit ltCaps
Lockgt - at the lilo prompt type dos then hit
ltentergt - the windows environment should then
appear To get from here to turbo C find the
appropriate program group and mouse click on the
turbo C program that should bring you into
the editor. You really should read through Day
1 in the text to learn about the turbo C
environment.
4Creating a Program
Simply getting into the editor is not sufficient
for creating a program (it used to be in the old
days) we must instead create a project which
includes a number of files necessary to the
program in addition to the program itself. The
procedure to do this is described in detail on
page 36 of the text although the exact
procedure is a little different for us because we
are using a slightly older version of the program
than the book assumes. Given we are going to
have to do this over and over it is worth
taking the time to make it explicit but first
...
5Projects versus nodes
What you will be learning to do is to create
programs where programs are a set of user
entered commands (code) that tell the computer
what the user wants it to do. After you enter
the commands, they are compiled by the program
which can be thought of as the program taking
your commands and translating them into a form
that can be run much more quickly and
efficiently. In order to do this, the program
needs to know about, and use, information over
and above the information provided in
your program the whole package of information
it needs is called a project, and your code is
one node of that project.
6Projects versus Nodes (continued)
Thus, before we can begin entering in our
commands, we must first create the project that
our source code will be part of. For our
purposes, most of this process is irrelevant as
we will likely do it the same way every
time. The other files (nodes) of the project are
also largely irrelevant to us suffice it to say
that creating a project is a necessary evil for
getting the programs we write to run
properly. We will likely return to the topic of
projects later in the class, and explain them in
more detail then, but for now ...
7Creating a Project step by step
1. Click on project from the menu bar, and then
new project. 2. In the place for the project
path and name make it read c\tcwin45\bin\proje
ct.ide where project is the name you want to
assign to the project (e.g., assign1.ide). 3. In
the target name box, place the name for the
specific node (program) you are planning to
create (e.g., question1). 4. Select the easy
win option from the target type list. 5.
Click the advanced button, then check the box
labeled .cpp node, and make sure the .rc and
.def options are turned off. Then click on
OK. 6. Click the OK and you will have
created a new project you will see a display
open at the bottom of the editor, showing you the
project you have created, and its nodes click
on question1.cpp to open an editor window in
which we can (finally) type our code.
8Our First Program
So, we are finally at a point where we can begin
to talk about programming to give us a context
for this, lets begin by creating, compiling and
running our first program. This program is based
on Listing1.1 from the text (pp. 36). // Our
first program! include ltiostream.hgt
int main() cout ltlt Im a C
programmer! ltlt endl cout ltlt Is that cool
or what! ltlt endl return 0
9Parts of a Program - Comments (//)
Notice that the first line of our program begins
with // The // tells the computer that
whatever follows it in the line is just a
comment. Comments have absolutely no effect on
the way the program runs their purpose (which
is more important than you think) is to allow
someone else, or you at a later time, to better
understand what you experiment is doing. Given
this, it is good form to begin each program with
a comment that explains what the program is meant
to do in an overall sense we will also use
comments throughout our code to explain what
smaller parts of it do.
10Parts of a Program - Header Files ()
The next part of the program is the part
beginning with the sign anything following
a sign is called a program directive for our
purposes the only relevant program directives
will be header files (while always end in
.h). Header files are collections of code that
have been created by someone else, and that allow
you use certain simple commands to do relative
complex things. The header files you include
define the commands you are allowed to use. Why
not use all possible commands? That would take
up a lot of memory, most of which we would never
use. Thus, instead, we only include those we
will use, and we have to specify our header files
very early in our program.
11Header Files - An Example
To make the notion of header files more clear,
consider the code we inputted in that code, we
eventually use the command cout. This command
is one that outputs text to the screen, a
process that is actually fairly complicated
however, someone has written more detailed code
that does all the complicated stuff for you if
you use the cout command. The code for the
cout command is one of the things in the
iostream.h header file (input/output stream)
thus, given that we want to use that command in
our program, we must include that header file.
12Parts of a Program - The main function
The next part of the code is the main function,
which is a necessary part of all programs in
order to understand it we must first talk about
functions in general. Functions are simply sets
of commands, grouped together to perform some
desired activity. Functions generally have the
following form var_type function_name(passed_va
riables) command1 command2 return 0
13Parts of a Program - The main function
For example, we might want to write code that
takes some number, then prints out the square of
it plus five, then prints out done. The code
to the right shows how we might do this with
a function (inefficient, but heh). Thus the
value 12 is passed to the function which does its
thing.
// function demo include ltiostream.hgt int
main() square_plus_five(12) void
square_plus_five(int a) cout ltlt aa5 ltlt
endl cout ltlt done ltlt endl return 0
14Parts of a Program - The main function
OK, we will talk a great deal about functions in
the weeks to come for now, the main point of
all this is that the compiler thinks of every
program as a function called main(). Thus, the
commands you write must fall within this
function. This means that, after the comments
and header, the next part of the program is
always initiates that function. Often, though
not always, we will call other functions we
create from within the main function. Thus,
bringing this all together, a typical C program
will tend to look like ...
15Parts of a Program the big picture
// comments explaining the program include
ltheaderfile1.hgt include ltheaderfile2.hgt //
etc int main() commands commands
function_calls var_type function1(variables_pa
ssed) commands
By commands and function_calls I simply mean
that within these areas you would typically have
a variety of commands and function calls. Note
that you can have function calls within
functions as you do within the main()
function.
16A Word About Syntax
As shown in all the examples, all of the commands
and function calls have to end in a semicolon
(). The semicolon tells the computer that you
are finished giving one command, and are about to
give another. The presence of these semicolons
is critical and, without them, the program will
not compile properly. Well talk more of syntax
(i.e. the rules defining the acceptable structure
of the language) as we learn more. But, for now,
what say we compile and run our programs!
17Compiling and Running a Program
OK, now we have entered in the user-code portion
of our program, and we want turbo C to compile
it and run it. To do this 1. First save what
you have done by choosing save from the file
menu. 2. Hold down the ltctrlgt button and press
the ltF9gt button. Your program will then be
compiled and, assuming there were no errors, it
will run. Presto, youre a programmer! Bunch of
geeks! )
18Half-Time
- OK, time for a break then on to
- - declaring variables
- - assigning values
- - inputting and outputting information
- - the if statement (along with else
statements) - - for loops
- - Assignment 1!
19Declaring and Initializing Variables
As you learned in PsyB07 (I hope), variables are
simply things that can take on numerous
values. In C, we draw a distinction between
different kinds of variables because some
variables require more memory than others (e.g.,
integers versus floating point) and some
are treated differently than others (e.g.,
integer versus strings). Because C treats
different variable types differently, it
is important to specify the type of variable
prior to using it. It is also wise to initialize
variables at the beginning of a program to be
sure they are not carrying some weird value.
20Types of Variables
The text goes into detail about the different
types of variables on page 45 I will mention
only the most common ones here int an integer
value between -32768 32767 (assumes signed
and short) float when you want to use decimal
values char character values You should also
read the part of the text referring to
constants, they can come in handy sometimes.
21Declaring and Initializing Variables
Thus, a typical bit of code might look like //
code to demonstrate the declaration of
variables include ltiostream.hgt main() int
count1, x float degrees0.0 x0 //
you can initialize separately from the
declaration Typically, declaring and
initializing variables is one of the first things
done within a function.
22Expressions Operators
Once a variable has been declared and
initialized, we often want to manipulate the
value of it throughout the program. We do so
within expressions, and the actual
manipulation is performed via an operator. For
example, consider the expression y (x 4) /
8 This expression uses three types of
operators (1) the add operator, (2) the
divide operator, and (3) the bracket operators
to specify order of operations.
23Expressions Operators Continued
C has all of the expected arithmetic operators
(pp. 55) such as , -, /, , and Steve will
explain . It also has some somewhat different,
and very powerful, incrementing operators such
as int x0, y0 x5 x // x now equals
6 --x // x now equals 5 again x4 // x now
equals 9 y 3x // x now equals 10, but
y27 y 2--x // x now equals 9, and
y18 There are also a set of logical operators,
but they are best considered in the context of
the if statement.
24The if, else if, else Statements
Very often we only want to invoke some command or
set of commands if certain conditions are true
the if command allows us to do this try the
following code // code to demonstrate if
family, as well as cin/cout include
ltiostream.hgt main() int mark0 cout ltlt
What was your mark on the exam? cin gtgt
mark if (mark gt 80) cout ltlt Woohoo!! You da
person! else if (mark gt 50) cout ltlt OK,
cool, I can live with that else cout ltlt
Bummer dude or dudette!
25Inputting and Outputting Information
As demonstrated in the previous overhead, the
easiest way to input information from the
keyboard is via the cin command. cin gtgt
variable_name Similarly, the easiest way to
output information is using the cout
command. cout ltlt Message you want to output
variables ltlt endl The endl stands for
end_of_line basically, it acts as a carriage
return.
26Logical Operators
When we created our marks experiment, we already
used the greater than operator (gt). There are
several other logical operators as well (p. 68)
including and or ! not lt less
than lt less than or equal to equal to
! not equal to if (x4 (y lt5 ygt
10)) cout ltlt bingo! ltlt endl
27Loops
The final thing we need to talk about today is
loops loops are used when you want to perform
some set of commands repetitively. The most
common loop is the for loop // demonstration
of loops include ltiostream.hgt main() int
trial0, total_trials20 for (trial0 trial
lt total_trials trial) cout ltlt trial ltlt
endl
Trial will start at zero, then will keep being
incremented by 1 until it hits 20 Then it will
stop looping.