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: 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:
  1. Find a free workstation.

  2.  

     

    Workstations are always on, do NOT turn them off. Wiggle the mouse if the screen is blank.

  3. Type your login-id, hit return, type your password (your password is not displayed).

  4.  

     

    Note: passwords are case sensitive, Banana is different from banana.

  5. Wait until two windows labeled xterm appear, this may happen quickly or take a while.

  6.  

     

    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.

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:

  1. 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.
  2. Create a new directory called lab1 by typing mkdir lab1
  3. To list your files and directories, type ls (think of this as shorthand "list"). You should see the directory you just created printed.
  4. 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

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:
  1. Use the Compile option under the Tools menu.
  2. 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.
  3. 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. 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.