lab08, CISC105, Fall 2007

Overview

In this lab, we'll briefly return to the Katrina plots in lab05. We'll then explore C++ a bit.

Goals

By the time you complete this lab, you should be able to:

  1. Demonstrate that you can work with plots in MATLAB without detailed directions
  2. Introduction to emacs editor (or vi if you already know it).
  3. Creating a C++ program
  4. Compiling and running a C++ program

Step-by-Step Instructions

Step 1: Preliminaries

To prepare for this week's lab, do all the following steps. If you are not sure what to do at any stage, you can consult earlier labs for details:

Step 2: Return to Katrina

We promised a return to the Katrina plots...

Back in lab04, the instructions indicated that later in the semester, we'd return to the problem described in lab04—namely, plotting graphs dealing with hurricane Katrina—to make two more plots:

Well, that time has come

And, because you now have much more familiarity with MATLAB than you did four weeks ago, I'm giving you the opportunity—the learning opportunity—to tackle this problem on your own.

Here's what you need to do:

To be graded

Step 3: Opening an XTerm on Strauss

C++ Programming assignments in CISC105 (and later courses that electrical and computer engineers takes, such as CISC181, CISC220) are done on a computer called strauss. Strauss runs the Unix operating system. You access strauss through a terminal session.

To get a terminal session from a PC at home or in your dorm, you get to strauss using a terminal program such as the "Secure Shell Client" (that's what Dr. Conrad uses on his laptop in lecture.) On the Sun Rays, you use a program called "XTerm"


To open an XTerm terminal session on strauss, go to the upper left hand corner of the screen, select the "Applications" menu, then the submenu "Programming", then the option "Xterm on strauss". In this window, you'll be able to do all the Unix commands you've learned in lecture, and most of the ones that are mentioned in Unix textbooks.

Selecting "XTerm on Strauss" from the "Programming" menu on a SunRay

An XTerm looks like this:


XTerm on strauss shown on the SunRay Desktop

"XTerm on Strauss" vs. just plain old "XTerm"

Note that if you accidentally select the Xterm option on the Applications menu that just says "Xterm" (plain old "Xterm", not "Xterm on Strauss"), you'll end up with an Xterm that is running on the SunRay servers (either Vivaldi, Haydn or Schubert.)

Vivaldi, Haydn and Schubert are separate computers from strauss

If you open an XTerm on Vivaldi, Haydn and Schubert, this will allow you to use the Unix commands that work with files. But, only the computer called strauss has the tools like emacs and the C++ compiler that you need to do C++ programming. Strauss is also the only computer with the MATLAB programming environment.

You'll very likely run into difficulty if you try to do your programming here; either the compiler wont be there at all (you'll get messages like "cc: command not found") or you might pick up some off-brand version of the compiler that works differently from what you are used to, confusing you no end. So, make sure you are programming on strauss, and no where else.

To check if you are on strauss, type hostname at the Unix prompt. If it doesn't come back with "strauss.udel.edu", you'll know you have an Xterm on the wrong machine.

Step 4: Creating a text file with Emacs

Step 4a: Learning about Emacs

What is emacs?

Emacs is a text editor you can use to create files on a Unix system. So far, when using MATLAB on the SunRays, we've used the MATLAB editor—this editor is "built-in" to the MATLAB programming environment.

Emacs, by contrast, is a "general purpose" text editor. You can use Emacs to create files in any computer language, including C++, Java, JavaScript, FORTRAN, Perl, PHP, etc—as well as MATLAB files, for that matter.

In fact, if you do MATLAB assignments on strauss from home using the SSH Secure Shell Terminal program, Emacs is the editor that you use. I also typically use Emacs when showing MATLAB programming examples in lecture.

You can also use Emacs to create HTML files for the web, plain text files containing data, and for many other purposes. It is a kind of "swiss army knife" for dealing with files, and a very useful thing to know.

Do I really have to learn emacs?

Yes. Basic proficiency with emacs is one of the learning outcomes for this course. There may be some basic questions about emacs on the next two exams. (You may substitute answers from vi, but that is the only acceptable substitution.)

If you already know how to use Emacs, you can skip directly to

If you've already used emacs, you may skip directly to step 4 of this lab.

If I already know vi, is that ok?

Note also that if, for whatever reason, you know already know the editor known as vi, you may use vi instead of emacs. Otherwise, you should learn emacs.

Step 4b: Getting started with emacs

To start, open an XTerm on strauss (see step2 above).

At the Unix prompt, type xemacs. For more instructions on what to do next, open the following link in a new web browser window:

http://copland.udel.edu/~pconrad/UnixAtUD/EmacsTips.html

Step 4c: Creating the file lab08.txt


Once you are familiar with emacs, go ahead and use emacs to create a text file called lab08.txt by typing emacs lab08.txt at the Unix prompt, and then typing in information like that shown below.

When you type in this information, put each item on a separate line, and include

For example, Prof. Haggerty's file would look like this (don't type in this information—type in your own!)

Michael Haggerty
Malvern, PA	
Delaware
haggerty

Be sure that your file ends with a blank line.

Step 4d: Sending a file to the printer

When you are finished, send the file to the printer in Willard 009 (the room where you have your labs) with the following Unix command:

qpr -q whlps lab08.txt

Or, if you are working closer to Smith Hall, you can use this command:

qpr -q smips lab08.txt

Note that it is not necessary to be logged into the Sun Rays to use these printing commands—you can type them from anywhere. Of course, you have to go to Willard or Smith to pick up your printout.

Is there a charge for printing?

Unlike printing from Windows, for which there is a charge after a certain number of pages, printing from Unix is still free as of November 2006. As long as folks don't abuse this privilege, it will remain free.

Can I print from SSH Secure File Terminal on Windows.

Yes—use the following command:

kpr lab08.txt

As far as I know, this ONLY works from the SSH Secure File Terminal program on Windows. However, some TAs that require printouts may still require a printout from the qpr command, so that the cover sheet with your userid and the date is attached. Ask your TA what he/she requires.

Step 4e: More questions about Emacs

emacs or xemacs?
Can I use these programs from home over SSH Secure Shell Terminal on Windows?

Step 5 : Setting up your .localenv file (to manage your path).

Note:  This section assumes that you changed your shell to tsch in Step 1b in lab03

Open a terminal window on strauss. In that windows, type the following:

> echo $PATH 

You should see output such as the following. Yours wont' match the following exactly, but it will be in the same form:

> echo $PATH
/opt/sfw/bin:/opt/tex/bin:/opt/texutils/bin:/opt/bin:/opt/SUNWspro/bin:/usr/open
win/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/opt/X11R5/bin
> 

The command echo $PATH tells the shell to print the value of a so-called environment variable called PATH. This variable contains a "colon-separated" list of directory locations where Unix will look for executable programs when you type in a command. (Windows has a similar concept, and I imagine Mac OS X probably does as well.)

It is important for your path to contain the right list of directories so that you can access the compilers we will use in this course. The locations of the compilers change periodically as the compilers are upgraded to newer versions (to fix bugs, and to incorporate new features into the C and C++ languages). If your account was set up a while back, it may not have an up-to-date path.

There are several files that you can customize in your directory in order to modify your path. These files are typically stored in your home directory on unix (the one you are in when you first login, and the one you return to if you type cd at the Unix command prompt.) These files have names that start with a period, which is how Unix implements the concept of "hidden files"; files that start with a period (e.g. .cshrc, .bashrc, .localenv, .emacs) do not typically show up in directory listings unless you specifically request to show hidden files.)

To list the files in your directory including hidden files, use the following command. Note that there is a space between ls and -al:

> ls -al
-rw-------   1 haggerty  1173        3391 Oct 19 09:42 %backup%~
drwx--x--x  36 haggerty  4000        8192 Feb  8 20:51 .
drwxr-xr-x 226 root     other       8192 Feb  8 04:08 ..
-rw-------   1 haggerty  4000        2090 Dec  7 10:39 .ICEauthority
-rw-------   1 haggerty  0376        2401 Feb  7 16:40 .TTauthority
drwx------   2 haggerty  2265        4096 Mar 11  2004 .Trash
-rw-------   1 haggerty  0376       13100 Feb  7 18:05 .Xauthority
-rw-------   1 haggerty  4000         814 Jun 11  1996 .ab_library
-rw-------   1 haggerty  1173        1894 Nov  9 15:21 .acrorc
-rw-------   1 haggerty  1173         237 Nov  9 15:21 .acrosrch
-rw-------   1 haggerty  4000         303 Feb 12  1998 .addressbook
-rw-------   1 haggerty  4000        2399 Feb 12  1998 .addressbook.lu
-rw-------   1 haggerty  4000        3071 Sep 20  1995 .article
etc....

The file we are most interested is the file .localenv. Type the command:

> emacs .localenv

You are going to add the following lines to your .localenv file. Be sure to type them carefully; if you are off by even one character, this will not work:

if -d /opt/sfw/bin then
  setenv NEWPATH /opt/sfw/bin:$NEWPATH
  setenv MANPATH /opt/sfw/man:$MANPATH
  setenv LD_LIBRARY_PATH /opt/sfw/lib:$LD_LIBRARY_PATH
endif


if -d /opt/SUNWspro then
  setenv NEWPATH /opt/SUNWspro/bin:$NEWPATH
  setenv MANPATH /opt/SUNWspro/man:$MANPATH
  setenv LD_LIBRARY_PATH /opt/SUNWspro/lib:$LD_LIBRARY_PATH
endif


# make sure file ends with a blank line

Note: when you type in this file, it is important that the file be input exactly as shown here; follow all the punctuation, spacing, and upper vs. lowercase exactly.

In addition, it is important to add a blank line at the end of the file. If you fail to do so, you may get an error such as the following

then: then/endif not found.

In this case, one compiler or the other will likely not work.

(Full disclosure: to be precise, a blank line is not strictly necessary, but the file must end in a newline character. It may be difficult for a Unix beginner to know for sure if the file ends in a newline. Making sure that there is a blank line—or better yet, two or three blank lines—is one easy way to ensure that the file ends with a newline character.)

The .localenv file is a startup script for your shell written in the "C shell command language". If you are interested in learning more, this language is documented in the later chapters of the Andersen "Just Enough Unix" text used in CISC181. Here is a brief explanation that can suffice for the time being:

The first line, if -d /opt/sfw/bin then tests whether the directory /opt/sfw/bin exists or not; this directory contains files for the GNU C and C++ compilers known as gcc (for C programming) and g++ (for C++ programming.) The GNU compilers are "open source" compilers, written by volunteer programmers. If /opt/sfw/bin exists, three setenv commands are y sused to set the values of three environment variables called NEWPATH, MANPATH and LD_LIBRARY_PATH. These three variables control the path for executables, manual pages, and load libraries respectively (you'll learn more about all three of these over the course of the semester.)

The next few lines do exactly the same thing but for the directory /opt/SUNWspro which contains the latest version of Sun's C and C++ compilers. These compilers are produced commercially by Sun Microsystems, the manufacturer of the strauss computer system. Until recently, these were closed source, proprietary compilers (they cost money). I don't know if that is still true or not; that may have changed. In any case, the development history of these two compilers is quite different. Although correctly written programs should produce the same results from both compilers, incorrectly written programs can produce very different error messages or error behavior. Comparing the results from both compilers can be very helpful in debugging, so it is important to be able to use both.

Once you have made the changes in the .localenv file, save the file and logout, then log back in.

You should then be able to do the following commands, and get exactly the results shown. These results indicate that you are able to use the correct versions of both the GNU and Sun compilers for both C and C++. If your output does not match that in the box below, review the .localenv file and try again, and/or ask your TA or instructor for assistance before proceeding.

> which gcc
/opt/sfw/bin/gcc
> which g++
/opt/sfw/bin/g++
> which cc
/opt/SUNWspro/bin/cc
> which CC
/opt/SUNWspro/bin/CC
> 

The which command tells you the full path of the file that gets executed if you type in a particular command. For example, in the script above, you see that if you type in gcc, the actual file that gets executed is /opt/sfw/bin/cc. You'll include the commands above in a script later in this lab; for now, once you are successful in getting the output above, you are done with this step.

Step 7 : Compiling C++ programs on Strauss

In this step, you will enter the traditional "first C++ program" just to make sure that you understand how to create a C++ program, compile it, run it, and script it.

  1. If you are not already there, change to the directory ~/cisc105/lab08 with the command:

    cd ~/cisc105/lab08

    The ~ character here stands for your home directory. Putting this at the front means you'll go directly to your cisc105/lab08 directory under your home directory no matter what your current directory is. Without it, Unix looks for cisc105/lab08 under the current directory, which might not always be what you want (if you are already in, for example, your lab08 directory).
  2. Use emacs to create a file lab08a.cc that contains the following (except substitute your name and Unix user id for those of Jane Doe, and use today's date.)

    DO NOT include your student number (NEVER include that number in anything you submit in this class!)
    // lab08a.cc Jane Doe doej@udel.edu 09/01/04 CISC105 section 099
    // traditional first program (C++ page 11)

    #include <iostream> int main(void)
    {
    std::cout << "Hello, world!" << std::endl;
    return 0;
    }
  3. Once you've created this program, use the following command to compile it (compile means: translate from C++ into machine language, or it cannot be translated because of errors, report the errors) with one of the following commands. At this point in the semester, you may use either one (later on, I may tell you to use one or the other.) Note that the first command is CAPITAL CC, not lowercase cc.

    CC lab08a.cc

    OR

    g++ lab08a.cc
    

    The first command (CC) is a commercial compiler, supplied by Sun Microsystems. The second command, g++ is an open source compiler, supplied by the GNU project, which is staffed by volunteer programmers. From time to time, we may discover differences, but for now, I don't care which one you use. Sometimes if you have a tricky syntax error it is useful to try both, because sometimes one gives you more useful error messages than the other.

  4. In both cases, a new file is produced called a.out. To execute this file (run your program, type the following:

    ./a.out

    You might be able to get away with just typing a.out without the leading ./, or you might not; it all depends on how your account is set up. (It has to do with the Unix concept of the "path"; we'll talk about this in lecture.)

    If all goes well, you should see something like the following:

    > CC lab08a.cc
    > ./a.out Hello, world! >
  5. You are now ready to use the script command to make a record of your work.

    Note that you must be careful when using the script command. The script command will wipe out your work if you are not careful!

    The thing to remember is: on the command line, type script followed by the name of a .txt file; for example:

    script lab08a.txt
    This is correct!!!!

    Never put script followed by the name of a .cc file. It will wipe out your .cc file!

    script lab08a.cc
    WRONG!!! WRONG !!!!
    WRONG!!!!

    Ok, now that we have that out of the way...

    Type script lab08a.txt. Then go through the following steps:

  6. Now, use more lab08a.txt to make sure your script looks ok, and use the following command to print your .txt file. You'll be submitting this to your TA when you hand in this lab:
qpr -q whlps lab08a.txt 


And finally, you are done with Goal 4! We'll return the file lab08a.txt and lab08a.cc files in the final step of this lab, where you'll submit those to WebCT along with the files you create in Goal 5:

Step 8 : C++ programming on Strauss

  1. If you are not already there, change directory into ~/cisc105/lab08 with the command:

     cd  ~/cisc105/lab08

  2. Do a pwd command to be sure that you are in the ~/cisc105/lab08 subdirectory. Then, use the following command to copy a program from Prof. Conrad's directory into your current working directory. BEFORE YOU TYPE THIS COMMAND, look over it carefully. You will notice that there is a space between the cp and the /www near the start of the command, and there is a space between the lab08b.cc and a period ( . ) which is at the end of the command. Those spaces are very important, and the period at the end is especially important. Be sure you type the command exactly as it appears here:

    cp /www/htdocs/CIS/105/haggerty/07F/labs/lab08/lab08b.cc .

    The cp command in Unix is used to copy a file. The file is copied from a directory under Prof. Conrad's home directory. Note that ~ by itself refers to your home directory, but ~pconrad refers to "pconrad's home directory". Similar, you can refer to any user's home directory on strauss if you know that user's login name; e.g. jsample's home directory can be accessed via ls ~jsample.

    The period by itself at the end is the "target" of the copy command, i.e. the place we are copying the file "to". A period, all by itself, refers to the "current working directory", the same one that comes up when you type pwd (print working directory). So, since the current working directory is ~/cisc105/lab08, we could also have done the same copy command using the following:

    cp /www/htdocs/CIS/105/haggerty/07F/labs/lab08/lab08b.cc ~/cisc105/lab08

    but the command was already pretty long, so using the . is a nice shortcut.
  3. Now, do an ls command, and you should see the lab08b.cc file in your directory. Your next step is to list the contents of the file, then compile the program, and run the program. Before you look at the list of commands below (which do those three steps), see if you can figure out what you would type to accomplish this. Then scroll down and see if you were right.

    scroll down for the answer

    keep scrolling down for the answer

    still further

    almost there

    ok here's the answer:

    cat lab08b.cc
    g++ lab08b.cc
    ./a.out

    Did you get it right? Note: you could also have done more lab08b.cc in the first step (if the file is too large to fit on the screen all at once), and CC lab08b.cc for the second step.

    An aside about the more program: note that you should never use more inside a script file; only use cat inside a script file. The more program is only for when you are looking at the file in "real time", not for scripts that you are going to print or submit electronically.
  4. Run the program a few times with different values, and look it over to see how it works.
  5. Now, the actual thinking work starts (though admittedly, this is still pretty darn easy.) This program, implements an algorithm to convert from Fahrenheit to Celsius (subtract 32, divide by 9, multiply by 5.) Your job is to edit the program (e.g. using emacs lab08b.cc or vi lab08b.cc) so that it converts, instead, from Celsius to Fahrenheit.

    You will need to modify (at least) the following:
  6. Once you have finished editing, try compiling and running again. Test your program with several different values to be sure the conversion is working properly.
  7. Now you are ready to script your lab08b.cc program. We'll do that in the "finishing up" step.

Finishing up: What to turn in for this lab

  1. Create a zip file that contains all of your files for this week.
    It is ok if the files provided with the lab are included along with the ones you yourself created.
    To create and test the zip file, follow the instructions from step11 of lab05 and/or lab06.
    The diary files may be included directly in the zip file this week.
    Before you submit your lab08.zip file on WebCT (step 7) be sure that your plots from step 1 are readable on the web.

  2. Change to the ~/cisc105/lab08 subdirectory. Make a script called lab08b.txt that contains all of the following steps. Note that we are showing that your program compiles with both CC and g++. Typically, it is only necessary to compile with one or the other.

    echo $PATH
    which g++
    which CC
    cat ~/.localenv
    cd ~/cisc105/lab08
    pwd
    ls
    cat lab08b.cc
    CC lab08b.cc
    g++ lab08b.cc
    ./a.out 
       repeat this step with several different inputs
    exit  to end the script

  3. Once you have created this script, print it out with:

    qpr -q whlps lab08b.txt

    (Note that this prints out the file in the lab, Willard 009. You can type this command from anywhere, including from home, but you have to go to Willard 009 to pick up your printout. You can also change whlps to smips to print in the basement of Smith Hall, or prsps to print at the Pearson Hall computing site. For the final version that you submit to the TA, you should always print in one of these places, not on your home printer.)

Step 7: Submit your lab08.zip file on WebCT

Now you can submit your work on WebCT, and you are done!

You should upload the following files


Grading: 125 Points (Yes I know the total below is 150 point but I figured you would want the other 25 points as Extra Credit!!!)

    Be sure that for your paper submission, all your pages are in the correct order (put your script lab08a.txt on top, and your script lab08b.txt on bottom), staple everything together, and write your name, email address, and (IMPORTANT!) your section number on the first page. These "submission details" are worth @@ points.

    If you don't know your section number, at least write down what time your lab starts; your TA can figure it out from that. But LEARN YOUR SECTION NUMBER! You'll lose points if you don't put it on your labs.

    All paper submissions MUST be turned in directly to the TA...  Do not leave the paperwork in my mailbox or give it to anyone other than the TA.  If you do not give it directly to the TA it will not count for credit.

step what we're looking for points
2 lab08_step2.zip (contains lab08_step2.m and any m files you needed to reduce repetition)
hhttp://copland.udel.edu/~youruserid/cisc105/lab08/windspeed.jpg http://copland.udel.edu/~youruserid/cisc105/lab08/pressure.jpg
50
7 lab08a.cc 10
7 lab08a.txt 5
8 lab08.cc 50
8 lab08b.txt (see above) 25
overall following of directions student should follow the directions given 10
Total   150

 

End of lab08 for CISC105, Fall 2007
Due Date: 11/16/2007, 11:55pm