CPS 006, Lab 1
Jan 18th, Tuesday
6 points
Prelab
1. Using a web browser like Netscape or IE, answer
the following questions and bring the answers, written on a piece of paper,
to lab.
1. What is the name of one
of Prof. Rodger's cats?
2.What is the name of Prof.
Astrachan's dog?
3.Professors in the computer
science department are involved in research in five areas. List three of
these areas.
4.What percentage of your
grade is the final exam for CPS 6?
2. Write a C++ program on paper that prints your name exactly
128 times. Use only cout statements and function calls. Bring the program
to lab. You should be able to modify the program to print your name 729
times. Note that 27 = 128 and that
36
= 729. The example program below provides a hint.
void Print()
{
cout << "Bjarne Stroustrup" << endl;
cout << "Bjarne Stroustrup" << endl;
}
void One()
{
Print();
Print();
}
int main()
{
One();
One();
return 0;
}
Purpose
This document will show you how to:
-
Login to and logout from the Sun Workstations found on campus
-
Use basic UNIX tools, including:
-
Copying, moving, deleting files
-
Traversing and moving between directories
-
Invoking programs including editors and compilers
-
Edit, compile, debug, and run a C++ program
This lab comes in several parts. You will login, reconfigure certain files
to facilitate working in CPS 6, practice some UNIX commands, write a simple
C++ program, and use netscape. This document is only a
brief introduction
to many tools. You will need to spend some time on your own experimenting
further to become comfortable with the tools. You should consult the OIT
online help page ( http://www.oit.duke.edu/helpdesk/search.html ) for
more information.
In this document, commands that you type will be in a font that
looks like this, sometimes commands may be quoted --- "like this"
--- do not type the quotes, just type what is between them. When
instructed to type something, press the return key after typing it.
Logging in
Login to a workstation. If you have problems logging in ask a TA for help.
(You may need to go to the OIT Help desk in 101 North Building, although
engineering students may need to check with the engineering school office
or the dean's office.) To log in:
-
Find a free workstation.
Workstations are always on, do NOT turn them off. Wiggle the
mouse if the screen is blank.
-
Type your login-id, hit return, type your password (your password is not
displayed).
Note: passwords are case sensitive, Banana is different from banana.
-
Wait until two windows labeled xterm appear, this may happen quickly
or take a while.
Note, sometimes a message of the day window (MOTD) pops up (this window
is labeled with xmessage at the top) with important information. If this
window has appeared, then you can click the left mouse button on
the bar that says "click here with the Left Mouse Button to continue" and
this window will disappear. When this message does appear, you should read
it as it usually contains important announcements.
Managing Windows in X
The two rectangles labeled xterm are called windows. The
row of small rectangles containing pictures in the top left position of
the screen is a menubar which contains some icons. Icons
represent windows that are temporarily hidden. Click the mouse (left button)
on the picture of the calculator (also labeled xcalc) and wait until a
calculator appears on the screen.
-
The green with black dots covering most of the screen is called the
background.
Move the mouse to any area of the background and try clicking the three
mouse buttons each at a different time. Each button brings up a different
menu of applications. Do not try any of these now, you can try them
on your own later.
-
Most windows have title bars across the top of the window. Move the mouse
to the title bar for the calculator window. Click and hold the left
mouse button on the word Calculator. Move the mouse and the window should
follow. Let go and the window will be in its new position. (Note: You can
also move windows using the Window Ops commands that appear when
you click and hold the left mouse button when the mouse points to the background.)
-
Another useful window command is resizing the window. Move the mouse cursor
to the Calculator title bar and click and hold the right mouse button.
An outline of the window appears. Move the mouse outside of the window
boundary and you will see the outline grow (and shrink if you move in the
other direction). When you release the mouse button, the Calculator window
is a different size than before. (Note: This command can also be found
in the Window Ops menu.)
-
To get rid of windows you no longer want, click once on the kill
icon along the top of the screen. The cursor becomes a skull and cross
bones. Move the mouse to any position inside the Calculator window and
click. The window should disappear.
Getting Started with UNIX
An operating system is a large program that coordinates all the resources
for the execution of programs. UNIX is the operating system on the Sun
workstations. UNIX contains lots of commands, a few important ones are
introduced here. Common commands are are
summarized on an OIT web page.
To enter a command, type after the prompt in one of the xterm windows.
You should see a prompt that may look similar to
[1] ola@teer8%
except ola should be replaced by your login-id and the number may
be different. If you make a mistake when typing a UNIX command, use the
delete key or the arrow keys to correct it.
To make the font bigger and easier to read you can hold
the control key down while pressing and holding the right mouse
button while the cursor is in an xterm window. Choose large for an easier-to-read
window.
If this is a new account, you should change your password to something
that is easier to remember, but not easy for someone to guess. Do not use
your name, or an English word, but rather compose a password formed with
numbers, upper and lower case letters, and punctuation marks. To change
your password, type the UNIX command passwd and follow the
directions it prints.
Next, from within an xterm window, at the prompt type
~ola/cps006/bin/setup.
This runs a program that will set up some files in your directory to make
it easier to use the programming tools we provide. Additionally, it creates
a directory in your account, called cps006, for you to put
your CPS 6 work. Any files changed by this program are saved with a .old
extension, e.g., .cshrc.old. If you do not get a prompt back
after typing the command, consult a TA. If you do not want to run
this program because you have made your own changes to your accounts setup,
you will need to look at the setup program to determine which parts of
it you want to setup. Some are needed for accessing and submitting files.
In order for changes made by the setup command to take effect, you will
need to logout and then log back in. To logout of your Sun workstation,
click the logout icon at the top left of the window. Now, please log
out, then log back in.
Creating and Traversing Directories
To do the work for the remainder of this lab, you will need to create a
directory
(i.e., folder) to contain the files for your program. In this
course, we will follow a convention of creating a separate directory for
each lab project and programming assignment within your cps006
directory.
When you login, your session begins in your main directory. To see the
name of your directory, type the UNIX command pwd (print working
directory). The complete name it prints in your terminal window should
look similar to /afs/acpub.duke.edu/users/o/l/ola
except your login-id
should be the rightmost word.
To create a directory called lab1 for storing new files related
to this lab, you will need to follow these steps:
-
Change into your cps006 directory (this directory was automatically created
for you) by typing cd cps006. This command should not produce
any output, but should move you from your home directory to the cps006
directory within your home directory. You can verify this by typing
pwd
again.
-
Create a new directory called lab1 by typing mkdir lab1
-
To list your files and directories, type ls (think of this as
shorthand "list"). You should see the directory you just created printed.
-
Change into your lab1 directory by typing cd lab1
You are now ready to start working on a C++ program. The command
to change directories, cd, lets you change directories relative
to your current directory or absolutely to a specific directory.
For example you can type cd ~ola to change into Professor Astrachan's
directory. Type ls to see the files and directories contained there.
At any time, you can type cd (with no arguments) to return
to your home directory. Now type cd cps006/lab1 to return
to your new lab directory. Now when you type ls you will
see there are no files in the directory, because you have not created any
yet. Instructions for editing, compiling, and printing are given
below. First, you will need to copy some files to your
lab1 directory.
The copy command is cp so from within your lab1 directory
type (do not forget the trailing dot: "."):
cp ~ola/cps006/lab1/* .
Type ls to see that the files were copied. You should see a new
file called Makefile. We will use the Makefile to compile programs
in this course. This Makefile will automatically link to libraries
that will be used in this course. You can use it without modification for
any C++ program you write as long as the complete program is in one file.
If you write a program in multiple files, you will need to modify the Makefile
slightly to tell it which files to combine together to make your program.
Test Program: Edit, Compile, Debug
You will edit, compile, and run a C++ program in this section. This first
program is very simple, but will provide some practice with editing, compiling,
and debugging. Using the editing commands described below, you should type
in the program shown. Be sure to use your name and lab section
number.
// Author: YOUR NAME HERE
// Date: TODAY'S DATE
// Course: CPS 6
// Lab: YOUR SECTION NUMBER HERE (01-08)
// Purpose: Practice C++
#include <iostream>
using namespace std;
int main()
{
cout << "This was no time for play." << endl;
cout << "This was no time for fun." << endl;
cout << "This was no time for games." << endl;
cout << "There was work to be done" << endl;
cout << " Dr. Seuss" << endl;
return 0;
}
Starting the editor emacs
To type in your program you need to use an editor. To use the editor
emacs
to create a new file for your program click on the emacs icon. An
emacs window should appear (please be patient!). You can use the arrow
keys and the mouse to move around in the emacs window.
Loading, Editing, Saving Files
-
Use the file menu to load a file (even one that does not exist yet).
Click and hold the menu and choose Open File before releasing the
mouse button.
(you can also use cntrl-x cntrl-f, that is hold down the control key
and press x followed by f as a substitute for using the menu.)
-
The prompt for Find file: appears in the small minibuffer
window at the bottom of the emacs window. You should also see a path which
might be ~/ or ~/cps006/lab1/. If you do not see this,
consult with a TA because you may not be in the correct directory. You
can edit what appears in the minibuffer with arrow/delete keys.
-
You will create a file named seuss.cpp. To do so complete the
path in the minibuffer so that it reads ~/cps006/lab1/seuss.cpp
and press return.
-
Enter the C++ program shown previously. Be careful that you type it just
as it appears. You will notice sometimes after you type something, the
cursor will "automagically" move to another position to indent properly.
When you type a "}", the cursor always jumps back to the "{" for a second
so you can see if the "}" matches with the correct "{". These are
features of the emacs editor meant to make it easier for you to complete
your programs.
-
If you make a mistake, use the arrow keys or mouse to move around and edit
in the window. You can use the mouse to position the cursor within
emacs by clicking in the window where you want to type. You can also
use the arrow keys and the scroll bar to move around.
In general, you should always have an emacs window open. Unless you are
logging off, there is seldom a reason to quit emacs. You can, however
iconify
(make small) the window by clicking the mouse on the dot-button in the
upper right of the window. This works for any window. Double-clicking on
an icon opens that application.
Until you save your file, it is stored in a temporary buffer
(the region in emacs in which you are typing). When you believe the C++
program is correct, save it by clicking the File button at the top
of the emacs window, and then selecting Save Buffer. A message that
the buffer has been written should appear in the minibuffer. You
should save your program often to ensure none of your changes are lost
inadvertently. You will not be granted any leniency if you lose
your work because you did not save it properly. You can type cntrl-x
cntrl-s to save instead of using the mouse.
Compiling a C++ program
A compiler transforms your C++ code into machine readable code the
computer can understand. Programs can be compiled from within emacs as
well. To compile from within emacs:
-
Use the Compile option under the Tools menu.
-
The command make -k, which appears in the minibuffer, is the default
compile command. You should compile by editing this command so that it
reads make -k seuss, then hit return. It may take a while for
the program to compile, but you should see an emacs sub-window with an
indication that something is happening.
-
If there is a syntax error in the program, a message will appear in the
*compilation*
buffer. To get to the error you can click on the error with the middle
mouse button. This will take your cursor to the line on which the error
appears, so you can use it to cycle through multiple errors, fix them,
and then recompile.
If there are multiple error messages, fix the first one
only and then recompile. Sometimes one error causes the compiler to list
additional errors that are not really errors.
An example of an error message is:
seuss.cpp:5: unterminated string or character constant
seuss.cpp:5: possible real start of unterminated constant
This message tells you the name of the file (seuss.cpp), the line
number the error occurred near (line 5) and a guess as to what
is wrong. You do not need to worry about where line 5 is since clicking
on the error message takes you there.
If you do not have any errors with the first compile, you
might want to modify the program so that there is an error. For example,
remove a semi-colon and then compile the program.
When the program has been successfully compiled continue with the next
section.
Execute your program
Compiling your program creates a new file with the same name as your source
file (in this case seuss.cpp), but without the .cpp extension.
This new file is executable, meaning it can now be used as a UNIX
command.
-
In an xterm window, type ls to see the files in you lab1
directory.
You should see both seuss.cpp and seuss. The file
seuss
is an executable program. You might also see a file called
seuss.cc~.
This is a backup copy of your program that emacs automatically creates.
It is not an exact copy, but rather a copy before you made your most recent
changes.
-
To run any executable program, type its name in your terminal window, so
type seuss to execute this program.
-
You can type ls -l to see how big the files are and when
they were created.
To conserve the limited space in your account, you may want to get into
the habit of removing the executable program (which is really big) when
you are finished with the assignment (since you can always re-create it
by recompiling the source code). To do this, type rm seuss
to remove the executable.
Print a copy of your program
To print a file, just type print filename
. Thus to print the file
seuss.cpp,
type print seuss.cpp
The print command sends your file to the printer in the room you are
currently in. If several people try to print at the same time on the same
printer, the print jobs are queued and printed one at a time. To see where
in the queue your job is type lpq followed by -Pname where
name
name
is replaced by the printer name. In Teer 106, the printers are in the back
of the room.
If you want to specify a specific printer, use the lpr command.
There are two printers in Teer 106, named teerlp1 (the last three symbols
are lowercase L, p, and the number 1) and teerlp2. To print any text file
use the lpr command, specify the printername (prefixing it with
-P
,
specify the file is a text file by -Fl (with lowercase L here) and
type the file name. To print your program seuss.cpp on the printer
teerlp1, type
lpr -Pteerlp1 -Fl seuss.cpp
Postlab
Each lab is worth six points. A TA must check each of the points. It is
your responsibility to ensure that a TA awards each point. After a lab
is over you cannot receive credit for the first two points.
-
Two point for completing the prelab activities and
bringing them to lab.
-
Two point for participating actively during the in lab activities.
-
Two point for completing the lab.