22C:030/115 Programming Techniques and Data Structures

Fall 2001

Assignment 1
Unix and g++

DUE DATE: 9/7/01


This lab exercise will get you started using the Unix machines in the Mathematical Sciences computing labs. It focuses on basic Unix and a C++ compiler called g++.

Basic Unix

Listing and Copying Files

The Unix file system is organized in directories. Each directory contains a set of files, which may contain text,
data, executable programs, or other directories. To determine the contents of the current directory, use the command

ls

Unix commands may be augmented with flags that give additional information. For example, the ls command
normally omits listing files that begin with a dot such as ".public-html". To list these hidden files
use the -a flag:

ls -a

You will find a number of files that begin with a dot in your home directory -- the directory you enter when you first log in. These files often contain information for initializing standard programs or recording preferences.
Subdirectories

Unix permits you to organize your files in sub-directories. You should use this facility to group software related to the projects you develop for this course. In this exercise we will step through the process of creating a project directory and submitting a programming assignment. The first step is to create a sub-directory that will contain all files related to this course. Let's name this directory 22c030. To create a new directory enter the command:

mkdir 22c030

This new directory will be a sub-directory of the current directory. To change the location of your working directory to the new subdirectory use the command:

cd 22c030

If you have a complex directory structure, you may forget where you are (i.e. what is current working directory). To print the working directory use the command "pwd". If you type this command following the change directory command above, the directory name "/space/login-name/22c030" should be printed where login-name is your account name.

For your first assignment, you should create another directory for your program. Call this new directory "program1". This directory should be a sub-directory of the 22c030 directory. Create this directory using the mkdir command and then change the working directory to this new directory using the cd command.

Copying a Program

In this step you will copy an already written C++ program to the "program1" directory you just created. In the next step you will compile and run this program. All of the code from the text book is available to you in the 22c:030 class account in the directory "/group/class/c030/programs". For this assignment you will copy a program called "heatwave.cxx" from the class repository to your new directory. To make a copy, use the copy command:

cp /group/class/c030/programs/heatwave.cxx .

The cp command takes two arguments: (1) the file to be copied and (2) the location and new name for the file. In this instance, the first argument (i.e. the program we want to copy) is indicated by a complete file system path that references the file "heatwave.cxx" in the directory "/group/class/c030/programs". The second argument is a symbol representing the current working directory (a period). Thus, this commands states that the program should be stored in the current working directory using the original file name.

Printing a File

There are a variety of commands to print files in Unix. A useful command for printing C++ code is:

lp -Pp301 -2r heatwave.cxx

The first argument indicates that the file should be directed to the the printer names p301 (this is the printer in room 301 MLH.) The -G flag enables the gaudy mode (Try printing the file with and without this flag on the command line. The -2r causes the program to be printed in two column format. This will both save paper and make it easier to scan the code. You must supply your own paper for printing. Be considerate of others and do not send jobs to the printer if you do not have paper for the printer. These jobs will clutter the print queue and interfere with other student's printing.


The g++ Compiler

Compiling a Program

We will be using the GNU C++ compiler. This compiler is called g++. The g++ compiler is available on the Hewlett Packard computers on the computer science network. These machines are located in rooms 301 and B5 MLH. If you are working on an SGI machine, you must login to an HP machine to compile your programs.
You can compile heatwave.cxx with the command:

g++ -Wall heatwave.cxx -o heatwave

The switch -Wall instructs the compiler to list all warning messages. These warning messages usually indicate programming errors. At the end of the compilation line, the arguments "-o heatwave" instruct the compiler to put ithe compiled code (i.e. the "object code") in a file called heatwave. If you don't specify a location for the object code, then the compiler places its result in a file named a.out.

Running a Program

To execute the program, simply type "heatwave"and press return. The program will prompt you for input.
and then interact with the program (which computes how long a tree will heat a house).
Six Pieces of C++

    The next step of this lab is to scan through heatwave.cxx. Using a text editor, open the file heatwave.cxx. Examine the program and observe the following aspects:
  1. Comments in C++ are indication by two slashes in a row. The entire line, after the // is a comment.
  2. The program contains two include directives. These tell the compiler to include standard C++ libraries.

    #include <iostream.h> // Provides cin, cout
    #include <stdlib.h> // Provides EXIT_SUCCESS

    These directives will appear in most C++ programs. The library iostream.h provides standard input and output functions and devices such as the standard input. The library stdlib.h provides several other standard items such as a constant named EXIT_SUCCESS that our program uses.
  3. After the two include directives, the program declares a named constant of type double called PI.
  4. const double PI = 3.14159;

    Because PI is declared as a constant, the compiler will disallow statements that change its value. This is an important way to give symbolic names to significant values and to prevent erroroneous modifications to these values.

  5. Search through the code and find the heading of the main program indicated by the symbol "main". Notice that the main program returns a value of type int. The role of this value is to inform the operating system about the success of the program's execution. For the g++ compiler, a main program returns the number 0 to indicate successful termination. For our g++ compiler, this "successful return value" is defined in stdlib.h as a constant called EXIT_SUCCESS. Look for the return statement in heatwave's main program that indicates successful termination.
  6. Basic output is simple in C++. A common form of the output statement looks like this:
  7. cout << "How big is your tree?" << endl;

    The name cout is the "console output device"-- i.e. the screen. The operator << is the output operator. So this statement sends the string "How big is your tree?" to the console output device, and then the special object, endl, is sent to the console output device. The object endl signifies the end of a line. It also serves to "flush" the output (meaning that the output will be written then and there, rather than waiting for the output buffer to fill).

  8. Basic input is also very simple in C++. A common form of the input statement looks like this:
  9. cin >> radius;

    The name cin is the "console input device"-- i.e. the keyboard. The operator >> is the input operator. Thus, this statement reads a real number set the variable radius to its value.

Programming Assignment 1

You will submit your programs electronically using the "submit" command. For this assignment you should modify the heatwave program to estimate the number of birds nesting in the tree. You should ask the user to enter the number trees in the forest and the number of birds in the forest. You should then divide the number of birds by the number of trees and print the result.  Don't remove anything from the original program. Just add a new section of code to the end of the program. When you are done, save the result in your directory program1 in the file "program1.cxx". You should then compile and run your program.
Once you are confident your program is compiles and runs correctly you should submit it for grading. Directions for submitting programs are given on the course home page.