COSC 1435 Lab 3

Purpose: This lab assignment is the homework from Chapter 5, Brookshear and learning about compiling and executing code.

Due: This lab is due at the beginning of class, February 18, 2013


Part 1. Do from Chapter 5 (Brookshear), Problems 1, 3, 6, 7, 8, 9, 11, 13, 14, 23, 24, 31, & 35. These problems will be turned in as a paper copy. I prefer typed answers, but will accept neatly written material. Make sure your name, COSC 1435, and Lab 3 are at the top of the assignment you turn in.

Part 2.

The bulk of this lab will be performed while logged in to a Computer Science compute server. A server is a machine used to provide services (often the programs that provide those services are also called servers). A compute server, is a machine that has a lot of processing power and can generally service many users at one time, and is used to provide processing power for computing things. penguin or clst is the Computer Science Program's compute server for students. A user logs in to a compute computer on a terminal. Terminals used to be basically a keyboard and monitor. Today, we emulate a terminal on say a laptop or PC using a peice of sotware that connects the machine it is run on to the server via a network (the internet). When you talk to the compute server you use a command shell which allows you to perform commands on the compute server to handle your programs (compile, execute) and also with your data (stored as files).

Since the traffic on the internet can be spied on and you want your connection to the server to not be known to anyone but you, the terminal emulation software will encrypt all communications between your machine and the server. The most common protocol (method of communication) for doing this is called Secue Shell (SSH). Since the computer science also doesn't want people to spy on your traffic (and maybe figure out your password), the only way you are allowed to connect to a computer server is using SSH. All machines in the labs have such sofware installed. For you machine at home, you can install SSH Secure Shell for windows. Putty is another popular program for SSH connections. If you are using a machine running mac or linux, most likely, an SSH client is already installed that you can connect with available in your machine's commnad shell.

Most of you are using to using a graphical user interface to connect to your machine. It allows you to mostly just use a mouse to move files around and control what your machine does. Most computers have such an interface to them. However, most computers also have a command-line interface (a command shell) as well that generally give you more control over the machine. For this lab you will learn some basic commands that will allow you to complete the labs in this course.

In the lab, click on the secure shell shell client icon to start the client. Hit quick connect and for the host name use and for the username use the same one you logged into the lab machine. When you are prompted for a password, use the same password you logged into the lab machine. When you log in, you will most likely get the following message:


Texas A&M University-Corpus Christi

-  Unauthorized use is prohibited.
-  Usage may be subject to security testing and monitoring.
-  Abuse is subject to criminal prosecution.
-  No expectation of privacy except as otherwise provided
   by applicable privacy laws.

If you log into this computer system, you acknowledge your awareness
of and concurrence with the Texas A&M University - Corpus Christi rules
and procedures (

These warnings are required in accordance with Texas Administrative
Code 202 as administered by the Texas Department of Information
Resources.  Reference Chapter 33, Texas Penal Code.

For questions or problems regarding CAMSLab accounts or labs please
email CAMSLab Administrators at
Last login: Tue Jan 29 18:04:41 2013 from
[sking@penguin ~]$ 

If you don't see this but two panes with files and folders, then maybe you clicked on the SSH Secure File Transfer. If so, just go to the menu, and select Window>New Terminal.

Let's read the message you just got when you logged in. What exactly is it saying?

Notice the very end of the message, something like: [yourUserName@penguin ~]$. This is the command prompt and it is telling you a few things. First the account (username) you are currently logged in as, second the name of the machine you are logged in to, third your current directory (~) the tilde (~) is a special character that stands for the home directory. A directory is a list files (sometimes called a folder. A file contains data on a storage device. A file store data stored as bytes. Data can be input to a program, output from a program, an executable program, source code for a program, a list of files (a directory), or a list of your cat's names, etc. The first command we will use, we saw in lab 1. Do an ls command. Note:, when you are asked to enter in a command (generally bolded and italicized), you need to enter in the command exactly and follow it with the <enter> key. Also note, that when we are talking about a specific key on the keyboard we will surround it with <> as in <CTRL> for the control key, <space> for the space key, <shift> for the shift key, <a> for the 'a' key. You won't be reminded to hit the enter key for the rest of the lab, but you will need to do it. At the command prompt, hit the <l> <s> and <enter> keys. You may just get back the command prompt. If so, it means you have no regular files in your home directory.

Let's create a new directory to hold all of you COSC1435 files for the semster. We will do that with the make a directory command. It is abbreviated on the command line as mkdir. Most commands take argument that affect what they do. For the mkdir command, the arguments are the directories you want to create. We want to create a directory name COSC1435. So to do that enter the following command
mkdir COSC1435
Then do an ls. Now you should see

plus maybe any other files you had in your directory. COSC1435 may be bolded or maybe in a color. This is telling you the type of file it is (directory). We are actually still in our home directory, but we want to go to this new directory for COSC1435 so that we can work in there, and our 1435 work will be separated from other coursework. To do that we will use the change directory. It is abbreviated with cd and it takes the directory you wish to go to as its argument. So enter in
cd COSC1435

For this lab, you will need to turn in the answers to several questions. Everytime you see a Q# that is where you will supply the answer on the answersheet.
Q1 Try ls again? What do you get?

Let's create a directory for lab3 so that we can keep each lab separate as well, avoiding clutter is quite useful. Create another directory called lab3 using
mkdir Lab3
Follow it with an ls. You should see your new lab3 directory. Note that for the COSC1435 directory we used uppercase characters, but for this one we used both uppper and lowercase letters. In linux, file names (and thus directory names) are case sensitive. That is lab3 and Lab3 are two different files. As a convention, naming directorys starting with a capital letter can be useful. Since ls gives the list of files sorted by name, using their ASCII values, all directories will be listed after all other files, so it is easier to tell what is a directory and what is not. There are other ways to do this, so isn't strictly necessary, but it can be useful, and I tend to do things this way. Another thing to not, spaces in directory names is a very bad thing to do in linux, primarily becasue command line arguments are separted by spaces so a directory with a space in it would look like two directories instead of one. There are ways to deal with this, but the best thing is to just not do it. Repeat, do not use spaces in directory names in linux, and in fact, not in the any filenames

Go ahead and change your directory to the new lab3 directory. hint (cd lab3) Before we continue a few words about your current directory. Generally your shell prompt will have the directory name in it. If you want to see where you are you can issue the pwd (print working directory) command. If you issued the cd command without an argument it will take you to your home directory. Goahead and try cd. Now to get back to were you were you can do cd COSC1435 followed by cd Lab3 or just cd COSC1435/Lab3. Some of you may think the directory name looks a little different than on windows. In addition to being case sensitive, linux uses / insetead of \ to separate directories.

Now we are going to learn one method of getting files onto penguin. First right click on the this link and save the file on your desktop. Now on the SSH client you have been doing this lab on, got the the menu and select Window->New File Transfer. This will bring up a secure file transfer application. That means that you can use it to move files between your machine and penguin in a secure manner (encrypted). Because you have already logged onto penguin using your password it won't require it again. If you start with the File Transfer application, then you will be required to enter a password. You will see to window panes. The left window pane say Local Name and the right pane says Remote Name. The local machine is the one you are sitting at, and the remote machine is the one you are logged into via the secure communication (in this case penguin). To move a file from one machine to the other, you just need to left click (and while holding) and drag the file to where you want it. In the right window click on the COSC1435 folder that you see, then click on the lab3 folder and you will be in the correct place. Now click on the lab3.cpp file you just saved onto you desktop and put it in the lab3 window. You can click and drag from the left window, or from the actual desktop, or from a windows explorer window.

The bottom pane should be a transfer log and it should show you that the file has been transfered. Verify by doing an ls. But this time, do an ls -a. The -a is called an option, that is an optional argument for the command. (note: Traditionally options start with a - (dash) and can be combined so you could do ls -a -l or ls -al. Many commands also have long arguments which you specify with -- (dash dash). ) The -a means to show all files in the directory. You should get the following:

.  ..  lab3.cpp
The '.' and '..' are special directory names. The single . (called dot) means the current directory and the double .. (called dot dot) specifies the parent directory. So if you were to do a cd .. it would take you back one directory level, or to the parent directory. The -a option on ls allows you to see all files. In *NIX (flavors of unix, which includes linux) files that start with a dot (.) are hidden files. They won't show up during a normal ls. This is a convention and you generall put setup files that control how the operating system (and other tools you use) works. Since these are you working files, you don't normally want to see these, thus they are hidden.

Now we are going to compile the file we just downloaded with the gnu c++ compiler. We call the compiler with g++. So issue the command g++ lab3.cpp. Now do an ls and what do you get?
Q2 Try ls again? What do you get?

The file a.out is the default name of the executable created by g++. An executable is a file that can be executed, this is as opposed to say a source code file, or a data file, which cannot be executed. When you execute the program it will be run by the operating system. If you type in a file name in the command shell, and that file name has execute priviledges, then it will execute the file. So let's execute the file, type in a.out

Oops, command not found? The executable file was not in the PATH, we'll talk about paths later in the semester. For now, lets just tell the shell that we need the file in the current working directory (dot). So enter in ./a.out. And run the program. This program implements and algorithm that solves a problem very similar to lab2, but the size of the triangles is specified by the height of the isosceles triangle instead of the number of lines of asterisks.

Let's recompile the program but name the execute to lab3. Enter in g++ -o lab3 lab3.cpp. Since we don't need the other executable, let's get rid of it. To delete a file we use the rm command (remove). Go ahead and enter rm a.out. The file should now be gone, verify with an ls. Note, it does not go into a trashcan, it gets deleted, so it is gone.

Let's take a quick look at the source code. To print a file to the screen we can use the cat command for concatenation. cat, takes all the files listed as arguments and concatenates them together. If only one file is given, you only get that file. By default, *NIX commands send their output to stdout (standard output) which in your case will be your command shell. So effectively cat lab3.cpp says read in lab3.cpp and then print it out to the terminal. Go ahead and do so.

Ugh, it went by really fast and you may not be able to see the beginning of the file. Luckily there are some programs to help us. The are more and less. More was first and it's purposes was to give you one screen of output at a time, wait for you to press a key (space to get a new screen, enter to get a new line, q to quit) so that you could actually read the output from cat. But if you accidentally went past where you wanted to go, you had to start over, so less was created, which allows you to go backwards as well. Let's try less. less lab3.cpp. The easiest is to use the arrow keys to navigate (space will also give you a new screen). There are a lot of keystroke commands that control less. Most *NIX commands have a way of getting help from the command line, unfortunately there is no real standard. Newer commands almost always will take --help. But *NIX has a way to read the help manual for anything, with the manual (man) command. You can use man less to get help on it. All programs will have a manual entry. Use man man to figure out how to use man.

Now we want to run a program and we need to turn in the output from the program. We could run it and cut and paste. But it may be very long. Instead lets redirect output. You'll learn more about this as we progress, but data is just a bunch of bytes and they have some order. So one byte comes before another. Since we just have this list of bytes, let's just think of it as a stream of bytes (kind of like a stream of water). If we have a stream of water going through a hose, we can hook up the hose to other hoses, so we can control where the water goes. Same thing with a stream of data. If you look at lab3.cpp again (with more or less or cat), you'll see that we are using cout to send out output. This is hooked up to the stdout (standard output) mentioned earlier, which is just going to the terminal (your screen). *NIX allows us to redirect where stdout is going. Here it can get a little tricky. There are more than one command shells out there and redirecting is done slightly differently for complex redirections. The most common shells are the Bourne shell, and the C-shell, and each of these have variants. The default on penguin is bash (Bourne-Again SHell).

Let's start by redirecting the output from our program. To redirect output you use > and tell it where to redirect (a filename). So lets redirect the output of lab3 to lab3.out. So enter the following command:
./lab3 > lab3.out
You notice you aren't getting any prompt at all. Generally this is telling you that either the computer is working or waiting for you. In this case it is waiting for you. Recall how the program executed before, it prompted you for the size of the triangle. Why isn't it doing it now? Because, we redirected stdout to a file, so the prompt is going to the file. But we know what the prompt is, so enter a number between 1 and 25 and hit return. Now it is waiting for us to enter Y or N. Go ahead and enter N. Now look at the file you just created.

Q3 What command did you enter to look at the output of the lab3 executable?

Now let's try and redirect some input. We want the program to run and have it create triangles of size 5 and then 7 and then quit. So that means we will have to enter in a 5 <enter> Y <enter> 7 <enter> N <enter> So the first thing to do, is get that into a file. We need to use an editor. Let's use a simple on on linux, called nano. Call nano with an argument of lab3.dat nano lab3.dat Then put in the correct lines for input. Your should see something like:

  GNU nano 1.3.12                        File: lab3.dat                                             Modified  


                                                 [ New File ]
^G Get Help       ^O WriteOut       ^R Read File      ^Y Prev Page      ^K Cut Text       ^C Cur Pos
^X Exit           ^J Justify        ^W Where Is       ^V Next Page      ^U UnCut Text     ^T To Spell
The ^ represent the <CTRL> key. so press the <CTRL> and the <X> keys at the same time (you don't need to hit shift). You will be prompted to save the buffer, enter Y. It will prompt you about the file name, just hit the <ENTER> and now you have exited and save your input. Cat the file to make sure it has what you want in it. Then let's use the input to run our program. As you guesses, we redirect input with <. So do the following command
./lab3 < lab3.dat

The program should have run twice, but both runs were too long to see on the screen at one time. So let's save the output this time. We can actually redirect both input and output at the same time. So let's do that
./lab3 < lab3.dat > lab3.out
Note that if you get a message about file exists, that means that it doesn't want to overwrite what was already in lab3.out. Some shells do this by default, some shells need to be told to do this. If you get the file exists error, use the ! (bang) character, to overwrite as in:
./lab3 < lab3.dat >! lab3.out

Redirection is very powerful and you can redirect an command. For instance enter in
ls -al > ls.out

Sometimes you will need to do a report for a lab, or sometimes you want instructions on how to do things in a directory. Often these files are name README So right now, using nano, create a file called README. In it, put your name, this course, which assignment this is (lab3), the date, and how many minutes you have spent on this lab so far. Also, add your answers to the questions from this lab.

At this point, inside you Lab3 directory, which is inside your COSC1435 directory you should have 6 files. A source code, an executable, a README, one input data file and two output datafiles. Now we want to submit this lab. But we have many files, so we want to create an archive of all these files. We are going to use zip for this lab. We also want to zip it up as a subdirectory. First let's go back one director ( cd ..). Then we can create the zip file. We want to name our zip file uniquely so let's use our username. We also want to put the course and lab# in there so it doesn't get misplaced or over looked. So, if your user name was juser then you would do: zip Lab3/* ('lny' is my initials, please your initials here in place of 'lny').

Note the '*' (called star), is the glob character. That means it matches all filename. So any file inside the Lab3 subdirectory will match and thus be added to the zip archive.


Program Submission. You will be submitting all programs to me and my grader via Blackboard. This is a large class and it is important to follow these instructions exactly. Failure to follow these instructions exactly will cause you to lose points on each lab. Do the following:

  1. Submit Part 1 (questions from Brookshear Chapter 5), via paper at the beginning of class.
  2. Make sure you have all parts of the lab saved in a location that allows you to 'zip' them together as described above.
  3. Follow the instructions given above about the files to be in the zip file.
  4. Upload this file to Blackboard.

Grading Criteria: 100 points available for this lab.

Grading Criteria: 35 points available for this lab