Lab Assignment 1

Due Tuesday October 3    10:00 pm


Purpose:
To get aquatinted with basic Unix commands, the Pico editor, compiling and running a C++ program, and submitting files to the course homework locker.

Description:
Read the following tutorials for Unix and Pico:

Unix Tutorial for Beginners
Pico Tutorial
Actually you can read just the first two parts of the Unix Tutorial, and read the rest later.  The Pico Tutorial is very short, and may not even be necessary.  Login to the IC Solaris server unix.ic.ucsc.edu.  After logging in you will see some initial message followed by a prompt which I will represent here as "%".  This prompt is just the operating system saying "ready to accept a command".  In the instructions below you are not to type the "%" character yourself but just what follows it, then hit return to execute the command.

Create a directory called cs10 by typing at the Unix prompt (%):

% mkdir  cs10
You will use this directory to store all lab work related to this class.  Confirm that this directory was created by typing the ls (list) command::
% ls
You will see a listing of the contents of your home directory, which is where you are presently located.  This listing should include the item "cs10" which is the directory you just created.  You can list the contents of this new directory by typing:
% ls  cs10
Since you have not put anything in cs10 yet, you should see nothing listed.  Make cs10 your current working directory (i.e. move into the cs10 directory) by using the cd (change directory) command:
% cd  cs10
Confirm that you are now in cs10 by using the pwd (print working directory) command:
% pwd
You will see something like  /afs/cats.ucsc.edu/users/f/your_user_name/cs10.  This tells you that you are now in a directory called "cs10" which lies in you home directory, which lies in a bunch of other directories, all lying within the top level directory "/" which we refer to as "root".  The Unix directory structure can be thought of as a tree with the root "/" at the top.  The pwd command prints the full path name of the directory in which you are presently located, i.e. it tells you exactly which branches you would have to take in a path from root to arrive at your present location.

Now using the Pico editor, create a text file called "first":

% pico  first
This starts the Pico text editor.  Pico is a very simple text editor and is easy to use since there is no need to remember any commands. All commands are listed along the bottom of the screen.  You enter text by just typing, and you get around by using the arrow keys.  Now type something, anything, say three or four lines.  Save what you've typed by typing:
ctrl-o
("ctrl" stands for the control key, thus you hold down the control key and simultaneously press the letter "o".)  Along the bottom of the screen you'll see something like: "Save buffer to file: first", at which point you should press return.  Now exit pico by typing:
ctrl-x
You'll find that you're back at the Unix prompt.  List the files in directory cs10:
% ls
Note that to list the contents of the your current working directory you just type  ls, whereas to list the contents of a subdirectory you type  ls  directory_name.  You'll see the file "first" listed and nothing else.  To view the contents of the file you've just created type:
% more  first
You now know how to create and view a text file.  You can easily change this file by again doing %pico first, make changes, save, and exit.  At some point in you career as a computer scientest you will need to learn a more sophisticated text editor, although Pico will certainly do for this course.  The class webpage has links to tutorials for two very popular Unix text editors called  Vi  and  Emacs.   When you've finished this assignment review at least one of these tutorials and do the assignment over in a new editor.

Now you're ready to write a C++ program.  Create a text file called  "hello.cpp"  and enter the following lines:

//  your_name
// CMPS 10 Fall 2006
// Lab Assignment 1
// File: hello.cpp
// Compile: g++ -o hello hello.cpp

#include<iostream>
using namespace std;

int main(void) {
cout << "Hello World!\n" ;
return(0);
}


Save the file and exit pico as before, then list the files in cs10.  The file  "hello.cpp"  is known as a C++ source file.  It is a valid C++ program, but it cannot be executed directly.  We must first translate this file into an object file (or executable file), which can then be executed by the computer.  This is done by running a C++ compiler.  The compiler we will use is called  g++.  Type:

% g++  -o  hello  hello.cpp

If you list the files in cs10 you will see a new one named  "hello".  Don't try to view this file (you'll get something like "permission denied" if you try).  Now run the program by typing:
% hello
You should see the words  "Hello World!"  printed to the screen, followed by a new command prompt (%) indicating that the program is finished running and the operating system is ready for a new command.  Evidently all this program does is print a message to the screen.

We'll have a lot more to say about the C++ programming language, but for now just consider the following facts:

Edit the source file "hello.cpp" by replacing "your_name" with your name.  Also alter the program so that it prints something other than "Hello World!".

Now create another source file called  "convert.cpp":

//  your_name
// CMPS 10 Fall 2006
// Programming Assignment 1
// File: convert.cpp
// Compile: g++ -o convert convert.cpp

#include<iostream>
using namespace std;

int main(void) {
double fahrenheit, // Temperature in Fahrenheit
celsius; // Temperature in Celsius

// Get temperature in Fahrenheit.
cout << "Enter the temperature in Fahrenheit: " ;
cin >> fahrenheit ;

// Convert to Celsius.
celsius = (fahrenheit - 32)*(5.0/9.0);

// Display temperature in Celsius.
cout << fahrenheit << " degrees Fahrenheit is equivalent to "
<< celsius << " degrees Celsius.\n" ;

return(0);
}


Save this file and compile it using:

% g++  -o  convert  convert.cpp

List the contents of cs10 to see a new file named  "convert".  Run the program by just typing:
% convert
The user is prompted to enter a Fahrenheit temperature, then shown the Celsius equivalent.  Again edit the comment block to indicate your name, student ID, and date.  Turn in the two files  "hello.cpp"  and  "convert.cpp"  by using the submit command described below.  Do not submit the executable files  "hello" and "convert".   As a general rule, if you submit unnecessary files for a lab assignment, you will lose points on that assignment. 

How to submit lab assignments electronically:

You must give the  submit  command the following information in this order:

     1. Which class locker are you submitting to (always  cmps010-pt.f06).
     2. Which assignment you're submitting (lab1, lab2, lab3, etc...).
     3. Names of the files you're submitting.

For example, for  lab1  you are submitting two files:  "hello.cpp"  and  "convert.cpp".  At the Unix prompt type:

% submit cmps010-pt.f06 lab1 hello.cpp convert.cpp
If you submit multiple copies of a file (with the same file name), only the most recent version remains. Thus if you think you made a mistake somewhere in a file and you want to replace it (before the due date of course), just resubmit under the same file name.  The new submission overwrites the old.  I recommend that each time you submit homework, you verify that it was properly accepted by using the  peek  command.  At the Unix prompt type:
% peek cmps010-pt.f06 lab1
You will see a listing of the files  "hello.cpp"  and   "convert.cpp" along with the option to view the contents of these files.
 


If you find any errors, please report them to: ptantalo@soe.ucsc.edu

webmaster@soe.ucsc.edu

Back to the SOE Class Home Pages
Back to the SOE Home Page