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 some examples of 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 that most of you have is the 1440 directory.  If you have that directory under your home directory, skip the following steps.  Otherwise follow them to create the 1440 directory:
step 1) % cd (cd, Change Directory will take you all the way back to your home directory)
step 2) % ls -la  (List all your files and make sure you do not have 1440 directory)
step 3) % mkdir 1440 (to create the 1440 directory)

% cd  1440  (cd -- Change Directory, To go to 1440 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.

All work for each lab must be completed under a directory that you create for that lab. In particular, each "project" or programming assignment must be placed in its own project subdirectory within the 1440 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 Pico screen where you will type your pogram.

In the first program, we will learn to define different variables and to read some values and to display the result of some calculations on the screen.   Type everything between the *** lines.

Program 1:
//*******************************************************************
// File name: lab2a.C
// Name:
// Last Midification Date:
//
// Description:  This program covert a temperature in Fahrenheit to Celsius.
// Input: Degree Fahrenheit
// Output: Degree Celsius

#include<iostream.h>

int main( )
{
     float cel_temp, fah_temp;

     cout << "Enter a temperature in Fahrenheit \n";
     cin >> fah_temp;

     // Conversion to Celsius
     cel_temp = (5.0/9.0)* (fah_temp-32);
     //Note how (5.0/9.0) is written, what if we would use (5/9)

     cout << fah_temp << "  in Fahrenheit is " << cel_temp
             << " in Celsius \n\n";
     return 0;
}
//*******************************************************************

Program 2:
//*******************************************************************
// File name: lab2b.C
// Name:
// Last Midification Date:
//
// Description:  This program asks users to input three numbers and will write them
// as one number between 0-999.
//
// Input: 3 integer values between 0 - 9 (note no less then 0 or more than 9)
// Output: 1 integer representing all three input values

#include<iostream.h>

int main( )
{
     int first_digit, second_digit, third_digit;
     int all_digits;

     //making the hundrends part of the number
     cout << "Enter the first number \n";
     cin >> first_digit;
     first_digit = first_digit * 100;

     //making the tens part of the number
     cout << "Enter the second number \n";
     cin >> second_digit;
     second_digit *= 10;

     //making the ones part of the number
     cout << "Enter the third number \n";
     cin >> third_digit;

     // compute the three digit integer
     all_digits = first_digit + second_digit + third_digit;

     cout << first_digit << " and " << second_digit << " and " << third_digit
             << " made " << all_digits << "\n";
     return 0;
}
//***************************************************************

Program 3: Another version of lab2b.c
//*******************************************************************
// File name: lab2b2.C
// Name:
// Last Midification Date:
//
// Description:  This program asks users to input three numbers and will write them
// as one number between 0-999.
//
// Input: 3 integer values between 0 - 9 (note no less then 0 or more than 9)
// Output: 1 integer representing all three input values

#include<iostream.h>

int main( )
{
     char first_digit, second_digit, third_digit;

     //making the hundrends part of the number
     cout << "Enter the first number \n";
     cin >> first_digit;

     //making the tens part of the number
     cout << "Enter the second number \n";
     cin >> second_digit;

     //making the ones part of the number
     cout << "Enter the third number \n";
     cin >> third_digit;

      cout << first_digit << " and " << second_digit << " and " << third_digit
             << " made " << first_digit << second_digit << third_digit << "\n";
     return 0;
}
//***************************************************************

What is the difference between program lab2b.C and lab2b2.C?

Program 4: Type the following C++ code (everything between ***) in the page:
//***************************************************************
//File name: lab2c.C
//Author: Your name
//Last Modification Date: 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";
     }
     else{
         cout << "You are ";
         c = 20 - age;
         cout << c << " years younger than 20 \n";
     }
     return 0;
}

//***************************************************************

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++ lab2a.C
Type:
% ls  (To see if a new file a.out has been created, this is the executable file that will "execute" your C++ code)

Type:
% 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++ lab2a.C -o lab2a
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,

% lab2a
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 programs using the automated submission program, use:

(Section 103) % ~rt/bin/submit1440_103  lab2  lab2a.C lab2b.C  lab2c.C

If you are in another section of the course, use proper id (~???) for the instructor and proper section number for the course (instead of 103) to submit your program.