CS 1440, Lab 2 Unix directories and compiling C++

The goals of this lab are to:
  1. Learn about UNIX directories
  2. Learn how to create/change directories (Create lab2 directory)
  3. Learn how to use Pico to Create/Edit a File
  4. Learn how to compile and execute C++ programs
  5. Learn to submit their program using the automated submission program

1: UNIX Directories

Recall that secondary storage (disk drives) are structured as directories and files. Also recall that files are often text (for example, letters, class rosters, etc.). Another kind of file is an executable file. An executable file is a "ready-to-run" program. For example, pine, pico, and vi (or vile) are programs we used last lab. They are also stored as files somewhere in the secondary memory of the computer, cs. Lastly recall that directories are special files that are used to organize other files that are related. Directories can contain other directories (sub-directories).

On cs you have what is called a "home" directory -- this is the directory you're in when you first log on. You can create files and subdirectories within this directory. Using the Unix tutorial from lab (1), you have already created some directories.  One of the directories I asked you to create was the cs1440 directory.  If you have created that directory already, skip the following steps.  To create the cs1440 directory do:
step 1) % cd (cd, Change Directory will take you all the way back to your home directory)
step 2) % mkdir cs1440 (to create the cs1440 directory)

% cd  cs1440  (cd -- Change Directory, To go to cs1440 directory)
% pwd
Here you can make subdiretories.  We will make another directory here for lab(2):

% mkdir lab2
% cd lab2
% pwd (You have to see that you are in the lab2 directory)

Subdirectories are like "folders" on PCs and Macs -- they are containers for files and other directories/folders. You will have a cs1440 subdirectory already created in your home directory. All work for this course should be done in this subdirectory. In particular, each "project" or programming assignment must be placed in its own project subdirectory within the cs1440 subdirectory. I'll indicate exactly which name to use for each class project. Here's a partial diagram of the directory organization you'll use:

Diagram of a directory tree.

2: Using Pico to Create/Edit a File

At this time you should be in lab2 directory.  To make sure you are there, one again use:
% pwd

If by any chance you are not there, go to that directory using cd command.  At the prompt type:
% pico lab2.C (This will open a blank file named lab2.C, note: .C is used for C++ codes)
 After you typed the command, you will get a blank screen like the one shown on Figure (1).

Figure (1) - Pico Editor

Type the following C++ code (everything between ***) in the page:
//File name: lab2.C
//Author: Your name
//Last Changed: Put today's date here
// .....

# include<iostream.h>
int main( )
      // Declaration of variables
      int age, c;

      cout << " Please tell me how old you are \n";
      cin >> age;
      if(age >= 20) {
         cout << "You are ";
         c = age - 20;
         cout << c << " years older than 20 \n";
         cout << "You are ";
         c = 20 - age;
         cout << c << " years younger than 20 \n";
     return 0;

Look at the bottom of the page, you will see a list of commands.  To save and exit from this file, you have (^x).  ^ stands for Ctrl key.  Thus, to save the file and exit, hold the Ctrl key and press x.  Answer the questions to save the file and keep the same name for the file and exit.

3: Program Translation and Execution

As we will better understand later, all computer programs must be translated into a form the computer can use prior to their execution. For now it is a two-step process,
  1. Translate the source file into an "executable file"
  2. Run (or execute) the program
Let's do it! In order to translate your source file enter,
g++ lab2.C
% ls  (To see if a new file a.out has been created, this is the executable file that will "execute" your C++ code)

% ls -la (This will list the file size, file permission, and other information about the file)

The name a.out is a default name for the executable file produced by the compiler. We can change it to something that will help us remember what program it goes with. Let's do that now. First, let us delete this non-mnemonic name ( and recover all that disk space) by entering,

rm a.out
Now we'll compile our C++ program again, but this time we'll specify a better name for our executable file. Enter,
g++ -o lab2 lab2.C  (BE VERY CAREFULL NOT TO USE lab2.C first)
List your files with the 'ls' command and note the difference. You might want to see the long form listing again too. Now you are ready to "run" your C++ program.

To execute your program enter,

% lab2
Input a value for age, Note: there is no way for the program to find what your real age is, so put any number and press enter.

Congratulations! Welcome to C++ programming!  Note that if you haven't deleted the a.out file, you would execute the program using a.out and the same thing would happened.

To submit your program using the automated submission program, use:

(Section 101) % ~rt/bin/submit1440_101  lab2  lab2.C
(Section 102) % ~rt/bin/submit1440_102  lab2  lab2.C

If you are in another section of the course, use proper id (~???) and proper section number to submit your program.  Note that to submit your homework for lab2, you will change the name of your program, i.e., replace lab2.C with ager.C.


Due: Wednesday, Jan. 24
Modify the above program to do the following:

Ask for your age
Ask for your friends age

And prints two different statements (shown in bold below) depending on your ages:
If you are younger:
   You are ?? years old
   Your friend is ?? years old
   You are ??? years younger than your friend
   You are ?? years old
   Your friend is ?? years old
   You are ??? years older than your friend

Remember, you can be younger than your friends all the time !

Every program you turn in this term should include the following:

  1. A comment at the top which includes the program name, the date, your name, your class, your instructor's name, and a brief description of the program. (Required input, operations performed, output.)
  2. Comments throughout the program describing any steps or blocks which perform functions which are not obvious.
  3. Descriptive variable names.
  4. Run-time prompts to the user describing input requested.
  5. Consistent use of style, including white space and statement alignment.
For this program, only requirements 1 and 5 are meaningful. You will hear more about this later.

Turn in hard copy of your program at the start of the next lab January 24. You can print your program by typing (at the Unix prompt)

        % lpr -Ppclab -J"your name" ager.C
(where you substitute your name in the position indicated). Pay close attention to the spaces and case used in this command.