CPS 100E, Fall 1995 Lab 1

Introduction to Unix, C++ programming, Emacs, and Netscape This first lab is to introduce you to the SUN Workstations, and to show you how to edit and compile a C++ program. By following the instructions that follow, you'll learn how to login to a workstation, adjust your X window environment, manage files using UNIX, create a new file using the editor emacs, create your first C++ program, and finally logout of the workstation.

This is only a brief introduction to many tools. You'll need to spend some time on your own experimenting further. You should buy the Introduction to the Public Unix Clusters at Duke from the Gothic Bookstore for more information.

In this document, commands that you type will be in a font that look like this , sometimes such commands will 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

Log in to a workstation (see below) . If you have problems logging in, see either the OIT Help desk in 101 North Bldg, although engineering students should check with the engineering school office or the dean's office.

SUN Workstations should already be turned on. If the screen is blank, try moving the mouse to make the screen reappear. You should already have a user id and a password. At the login prompt, type your login id and press the Return key. Type your password and press the Return key. Your password is not displayed on the screen, so that anyone looking at your screen will not see your password.

Now wait until two windows labeled xterm appear. There will also be several other items appearing on your screen.


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. Move the mouse pointer inside the xterm window and type:
               xlogo &
and then press return. A window with a large X in it, called the Xlogo window, should appear.

The gray fuzz covering most of the screen is called the {\it 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. We'll select a few of these during this lab, and you can try the rest on your own later.

Most windows have title bars across the top of the window. Move the mouse to the title bar for the Xlogo window. Click and hold the left mouse button. Move the mouse and the window should follow. 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 at the background.

Another useful window command is resizing the window. This command can be found in the Window Ops menu. A faster way is to move the mouse cursor to the title bar and click and hold the right mouse button. Try this with the Xlogo window. 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 Xlogo window is a different size than before.

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 Xlogo window and click. The window should disappear.


Useful UNIX commands

An operating system is a very large program that coordinates all the resources for the execution of programs. UNIX is the operating system on the SPARCstations. Here is an introduction to some commands you should find useful. Move the mouse inside one of the xterm windows and try out each of these commands. You should see a prompt that may look similar to
    [1] rodger@teer8%
except rodger should be your login id and the number may be different. You may type a UNIX command to the right of the prompt. When the command is finished, you will see another prompt, and can then type another UNIX command. If you make a mistake when typing a UNIX command, use the delete key.

To make the font bigger you can hold the control key down while pressing and holding the right mouse button. Choose medium or large for an easier-to-read window.


Assignment 1: A word is like no other

You will complete the program below to count the number of unique words in a file.

You'll edit, compile, and run a program in this lab. Instructions for editing, compiling, and printing will be given below.

You'll need to copy some files to your lab1 directory first by typing (don't forget the ``.''):

      cp ~ola/cps100e/lab1/*   .
Type ``ls'' to see that the files were copied. You should see two new files: Makefile and words.cc. We'll use the Makefile to compile programs in this class.

You'll also need to create a link to a directory containing large data files (instead of copying them). Type:

       ln  -s  ~ola/data  data 

If you now type ``ls'', you will see that you have a new directory calleddata.

Now complete the C++ program words.cc using the emacs editor (described below).

You should fill in your name, date, and a description of the purpose of the program, and then complete the Store function. Compile, run, and print a copy of the program. Instructions for editing and compiling will be given below.

#include 
#include 
#include "vector.h"
#include "CPstring.h"

// Name: 
//
// Date: 
//
// Purpose: 

void Store(Vector & wordList,
           int & wordCount,
           const string & word);

void Print(const Vector & wordList, int wordCount);        
           
main()
{
    string filename;
    ifstream input;
    string word;

    Vector wordList(10000);         // make room for lots of words
    int wordCount = 0;                      // how many words there are
    
    cout << "filename ";
    cin >> filename;
    input.open(filename);

    if (! input.good())
    {
        cout << "couldn't open file " << filename << endl;
        exit(1);
    }


    while (input >> word)                    // one word at a time
    {
        Store(wordList,wordCount,word.Downcase());
    }

    Print(wordList,wordCount);               // print list of words
}

void Store(Vector & wordList,
           int & wordCount,
           const string & word)
     
// precondition: wordCount = # words in wordList
// postcondition: if word is NOT in wordList, it is added at the
//                end and wordCount is incremented to reflect addition
{
    // needs to be implemented    
}

void Print(const Vector & wordList, int wordCount)
{
    int k;
    for(k=0; k < wordCount; k++)
    {
        cout << wordList[k] << endl;
    }
    cout << "total of " << wordCount << " unique words" << endl;
}

Note that on the second line that starts with ``cout'', there is a space between Engineers and the double quote. Creating your program using the editor emacs To type in your program you need to use an editor. To use the editor emacs to modify the file words.cc, type ``emacs &''.

An emacs window should appear. In emacs the commands are written in the form C-x or M-x . The notation C-x is the same as CTRL-x, and means to press and hold down the control key (the key to the left of the A key) and press the x key, and then release both keys. The notation M-x is the same as ESC-x, and means to press the ESC key and release it, and then press and release the x key.

If the print in your emacs window is not large enough, you can make it larger. (This will only work if you previously typed the setup command). Type the emacs command M-x big by first pressing and releasing the ESC key, and then typing ``xbig'' and pressing the return key. (look in the minibuffer window at the bottom of the screen to see if it's working as you type).

To edit a file from within emacs, type ``C-x C-f'', (that's control-x control-f). When the prompt for {Find file:} appears in the small mini-buffer window at the bottom of the emacs window, type ``words.cc'' and press return. Be sure to specify the full path, you might need to type ``~/cps100e/lab1/words.cc'' in the mini-buffer. You can edit within the mini-buffer using standard emacs commands.

If you look carefully at the emacs information (black) bar, it should be C++ fill or something similar.

You should then enter the program shown above. Be careful that you type it just as it appears. You'll notice that when you type a left-brace { the right brace appears ``automagically'' with the cursor positioned and indented properly.

If you make a mistake, use the following emacs commands to move around and edit in the window. If you begin an emacs command that you do not want to complete, type ``C-g'' (press and hold the Control key and then press g) to cancel the command.

Alternatively, you can also click on Edit and File at the top of the emacs window to do some of these commands.

To save the buffer (the region in emacs in which you're typing) use ``C-x C-s'' (a message that the buffer has been written should appear in the mini-buffer). When you're reasonably sure that the program is correct you're ready to compile it.

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.

When you believe the C++ program is correct, save the file by typing a sequence of two emacs commands: C-x C-s

The file will stay there, but you should see a note at the bottom of the emacs window indicating the file has been written to memory. 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 or from the shell (in an xterm window). Compiling from within emacs will save LOTS of time in the long run. To compile from within emacs type ``C-x c''. Do not type ``C-x C-c'', cntrl-x cntrl-c, this quits emacs . If ``C-x c'' doesn't cause a prompt to appear in the mini-buffer, you should type ``M-x compile''. The default compile command of make -k appears in the mini-buffer. You should compile by editing the command so that it reads ``make words'', then hit return. It will 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 goto the error you can type C-x ` --- that's control-x followed by a back-quote (look in the upper left corner of the keyboard). This command will always goto the next error, 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:

           words.cc:8: syntax error before '<'

This message tells you the name of the file (words.cc), the line number the error occured near (line 8) and a guess as to what is wrong. You don't need to worry about where line 8 is, typing C-x ` moves to this line.

When the program has been successfully compiled continue with the next section.

If you don't have any errors with the first compile, you might want to modify the program so that there is an error so you can see what happens when you compile a program with an error. Once there are no errors, move on to the next section.


Execute your program

From an xterm window, list the files in your lab1 directory. You should see both words.cc and words. The file words is an executable program. The compiler translated your C++ program into machine code the SUN can understand and stored this code in the executable file words.

To run any executable program, type its name, so type ``words'' to execute this program. This program will ask you to type in the name of a data file, to see the number of unique words in hamlet, type: ``data/hamlet.txt'' for the name of the file.

Type ``ls -lt'' to see how big your files are and when they were created. At the end of the lab be sure to type ``rm words'' to remove the executable (which is really big). You can always re-create it by recompiling the source code.


Print a copy of your program

For this assignment you will print out a copy of your program to turn in. To print a file, just type print filename . Thus to print the file words.cc, type ``print words.cc''

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 words.cc on the printer teerlp1, type

     lpr -Pteerlp1 -Fl words.cc

Give your program to the TA and make sure your name is checked off for your participation in the lab. You are not done yet!.


Surfing the Internet

Netscape is an information browser for surfing for information on the Internet. You'll need to use Netscape to answer some questions about computer science and CPS 100E. To start netscape, click on the icon at the top of your window, or type ``netscape'' in your xterm window.

If you have never used Netscape before, you will need to click on ``accept'', and then the Netscape window will appear. Click once anywhere in this window and the Duke homepage will appear. From here, you can click on any highlighted text to get more information on the item. Click on back to retrace your steps. To answer the following questions, you'll need to find the CPS 100E homepage, and Prof. Astrachan and Prof. Rodger's homepages.

Write the answers to the following questions in a file called ``answers''

Submitting Assignments

To submit assignments you'll run a program called {\tt submit100e} The general format to run the program is:
    submit100e lab1secN file1.cc file2.cc ... README 

where the first argument to the program (after {\tt submit100e}) is the assignment name. If you type an invalid assignment name a message will appear. After the assignment name comes all the files you need to submit as part of the assignment. The file names that are arguments to the submit program can be entered in any order. Every assignment must have a README file submitted with it (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the README file. You must also include a list of names of all those people with whom you collaborated on the assignment. So for this assignment, create a README file by using emacs, and ``C-x C-f'' to find a file named README. Then try to submit electronically by typing (where N below is replaced by your section number: 1, 2, 3, 4, or 5)

    submit100e lab1secN words.cc answers README

Leaving

To exit emacs, type the emacs command ``C-x C-c''

If you have not saved all your files, you will be asked at the bottom of the window whether or not to save a particular file. Type y or n for each file. When you are asked ``Subprocesses are executing; kill them and exit?'' type yes and press Return. At this point, the emacs window will disappear. Always exit emacs before logging out!

To logout of your SUN workstation, click on the logout icon at the top left of the screen.

You will see all the windows and icons disappear and you will be logged off. Never turn off the workstation!

Remember this is only a brief summary of useful commands to get you started. You'll need to refer to the other documents mentioned on the first page to learn more.