Lab 09, CISC106, Fall 2007

Introduction

This lab is about C++. We will program in C++ and  see how working with C++ differs from working with MATLAB—and, more generally, how working with a "compiler" is different from working with a language that has an “interpreter”.

Overview

  1. You'll learn how to open an XTerm on strauss on the SunRay machines.
  2. You'll learn how to write C++ programs on strauss using emacs (or vi).
  3. You’ll learn how to compile and run C++ programs on strauss.
  4. You’ll learn how to record your Unix session using the “script” Unix shell command.

Step-by-Step Instructions

Step 1: Opening an XTerm on Strauss on the SunRay machines

C++ Programming assignments in CISC106 (and later courses that electrical and computer engineers takes, such as CISC181, CISC220) are done on a computer called strauss. Strauss runs the Unix operating system. You access strauss through a terminal session.

To get a terminal session from a PC at home or in your dorm, you get to strauss using a terminal program such as the "Secure Shell Client” (i.e. SSH client).On the Sun Rays, you use a program called "XTerm".


To open an XTerm terminal session on strauss, go to the upper left hand corner of the screen, select the "Applications" menu, then the submenu "Programming", then the option "Xterm on strauss". In this window, you'll be able to do all the Unix commands you've learned in lecture, and most of the ones that are mentioned in Unix textbooks.

Selecting "XTerm on Strauss" from the "Programming" menu on a SunRay

An XTerm looks like this:


XTerm on strauss shown on the SunRay Desktop

"XTerm on Strauss" vs. just plain old "XTerm"

Note that if you accidentally select the Xterm option on the Applications menu that just says "Xterm" (plain old "Xterm", not "Xterm on Strauss"), you'll end up with an Xterm that is running on the SunRay servers (either Vivaldi, Haydn or Schubert.)

Vivaldi, Haydn and Schubert are separate computers from strauss

If you open an XTerm on Vivaldi, Haydn and Schubert, this will allow you to use the Unix commands that work with files. But, only the computer called strauss has the tools like emacs and the C++ compiler that you need to do C++ programming. Strauss is also the only computer with the MATLAB programming environment.

You'll very likely run into difficulty if you try to do your programming here; either the compiler wont be there at all (you'll get messages like "CC: command not found") or you might pick up some off-brand version of the compiler that works differently from what you are used to, confusing you no end. So, make sure you are programming on strauss, and no where else.

To check if you are on strauss, type hostname at the Unix prompt. If it doesn't come back with "strauss.udel.edu", you'll know you have an Xterm on the wrong machine.

Step 2 : Writing and compiling your first C++ program on Strauss

In this step, you will enter the traditional "first C++ program" just to make sure that you understand how to create a C++ program, compile it, and run it.

  1. Preparing your lab09 directory:  create a new subdirectory ~/cisc106/lab09 and change your working directory to this directory. That is, type the following in Unix command prompt.

mkdir ~/cisc106/lab09

cd ~/cisc106/lab09

  1. Use emacs (or vi) to create a file hello.cc that contains the following (except substitute your name and Unix user id for those of Jane Doe, and use today's date.)
// hello.cc Jane Doe doej@udel.edu 09/01/04 CISC106 section 099

// traditional first program 



#include <iostream>


using namespace std;
 
int main()

{

 cout << "Hello, world!" << endl;

 return 0;

}

3.      Once you've created this program, use the following command to compile it (compile means: translate from C++ into machine language, or it cannot be translated because of errors, report the errors) with one of the following command. Note that the first command is CAPITAL CC, not lowercase cc.

CC hello.cc

4.      A new file is produced called a.out. To execute this file (i.e., to run your program), type the following at the unix command prompt:

./a.out

You might be able to get away with just typing a.out without the leading ./, or you might not; it all depends on how your account is set up.

 If all goes well, you should see something like the following:

> CC hello.cc

> ./a.out 
Hello, world!
> 

5.      You are now ready to use the script Unix shell command to make a record of your work.

Important Note: Script file vs. Diary File

·         A special note should be made about the use of the words script file in this lab.

·         When working with MATLAB, the word script file refers to a special kind of M-file, namely one that just contains a sequence of MATLAB commands to be carried out in order to solve a problem. We distinguish script M-files from function M-files.

·         We also talk about diary files as the files where we keep a record of what we did in a particular MATLAB session

·         However, when we work with C++, when we refer to a script file, we are talking about a record of our work, i.e. the same thing we call a diary file when working with MATLAB.

·         Basically, script command in unix is similar to diary command in MATLAB. Please make a note of this!

Note that you must be careful when using the script Unix shell command. The script Unix shell command will wipe out your work if you are not careful!

The thing to remember is: on the command line, type script followed by the name of a .txt file; for example:

script lab09a.txt

This is correct !

Never put script followed by the name of a .cc file. It will wipe out your .cc file!

script hello.cc

WRONG!!! WRONG !!!!
WRONG!!!!

OK, now that we have that out of the way...

Type script lab09a.txt at at the Unix command prompt. Then go through the following steps:


You'll submit the files lab10a.txt and hello.cc in the final step of this lab.

Step 3 : a C++ program to convert inches to centimeters and vice versa 

1.      If you are not already there, change directory into ~/cisc106/lab09 with the command:

 cd  ~/cisc106/lab09

  1. Copy the program unitConvert.cc from Prof. Aydin’s directory into your current working directory.That is type the command:

    cp /www/htdocs/CIS/106/iaydin/07F/labs/lab09_files/unitConvert.cc   ~/cisc106/lab09/

  2. Now, do an ls command, and you should see the unitConvert.cc file in your directory.

 

  1. Your next step is to list the contents of the file, compile the program, and then run the program. That is type the following one by one.

 
cat unitConvert.cc
CC unitConvert.cc
./a.out

Note: you could also have done more unitConvert.cc in the first step (if the file is too large to fit on the screen all at once).


An aside about the
more program: note that you should never use more inside a script file; only use cat inside a script file. The more program is only for when you are looking at the file in "real time", not for scripts that you are going to print or submit electronically.

  1. Run the program a few times with different values, and look it over the file unitConvert.cc to see how it works (note that // (i.e., two forward slashes) is used for writing comment inside a C++ program, similar to % in MATLAB).

 

  1. Now, the actual thinking work starts (though admittedly, this is still pretty darn easy.) This program, implements an algorithm to convert from inches to centimeters. Your job is to edit the program (e.g. using emacs unitConvert.cc or vi unitConvert.cc) so that it converts, instead, from centimeters to inches (hint: 1 cm = 0.3937 in ).

    You will need to modify (at least) the following:
  2. Once you have finished editing, try compiling and running again. Test your program with several different values to be sure the conversion is working properly.

 

  1. Now you are ready to script your unitConvert.cc program for submission to webCT. Make a script called lab09b.txt that contains all of the following steps.


script lab10b.txt   
to start the script
ls
cat unitConvert.cc
CC unitConvert.cc
./a.out 
   repeat this step with several different inputs
exit  to end the script

 

You'll submit the files lab10b.txt and unitConvert.cc in the final step of this lab.

Step 4 : a C++ program to print a triangle of stars

In this section you will write C++ program together with its corresponding functions to print a triangle of demanded size, composed of star (*) symbols.

Here are the steps to complete the job.

1.      If you are not already there, change directory into ~/cisc106/lab09 with the command:

 cd  ~/cisc106/lab09

  1. Copy the program triangle.cc from Prof. Aydin’s directory into your current working directory.That is type the command:

    cp /www/htdocs/CIS/106/iaydin/07F/labs/lab09_files/triangle.cc   ~/cisc106/lab09/

  2. Now, do an ls command, and you should see the triangle.cc file in your directory. You will use triangle.cc as a skelaton to complete this Step 4.

 

  1. You will write two functions named printALine() and printATriangle() and a main(). Follow the steps below.

 

  1. Open triangle.cc in emacs (or vi). Write a C++ function named, printALine() inside the triangle.cc. Here are the rules to write your function:

 

printALine(5);

 

The output it produces on the screen will be five stars printed next to each other on a single line, like below.

 

*****

 

  1. Write another C++ function named, printATriangle(), inside triangle.cc. Here are the rules to write your function.

 

printATriangle(5);

 

The output the function produces will be

 

*****

****

***

**

*

 

That is a five-line triangle that is composed of starts (note that, there is 5 stars in the first line, then 4 stars, and eventually 1 star in the last line to print the shape of a triangle to the screen).

7.      Writing your main function:

A sample output once you compile and run your triangle.cc is as follows:

> CC triangle.cc

> ./a.out

How many lines do you want in the triangle: 0

ERROR: Please enter a value greater than zero.

> ./a.out

How many lines do you want in the triangle: -1

ERROR: Please enter a value greater than zero.

> ./a.out

How many lines do you want in the triangle: 5

*****

****

***

**

*

> 

  1. Once you have finished editing all your functions, try compiling and running again. Test your program with several different values to be sure that it works properly.

 

  1. Now you are ready to script your triangle.cc program for submission to webCT. Make a script called lab09c.txt that contains all of the following steps.


script lab10c.txt   
to start the script
ls
cat triangle.cc
CC triangle.cc
./a.out 
   repeat this step with several different inputs
exit  to end the script

 

You'll submit the files lab10c.txt and triangle.cc in the final step of this lab.

Step 5: Modifying the triangle.cc in Step 4

You task is to modify the functions printALine (), printATriangle() and  main() from Step 4 to print a triangle of any requested sysmbol (such as @, #, +, etc.)  instead of only printing a triangle composed of stars (*). To complete the task, you will do the following:

  1. Copy the file triangle.cc you finished in Step 4 as newTriangle.cc into your lab09 directory. That is, type the command


cp ~/cisc106/lab09/triangle.cc  ~/cisc106/lab09/newTriangle.cc

2.      Modify the function prototype of the printALine(). The function will now get two inputs; the symbol to print to the screen and the number of symbols on the line. You will modify the function body to print the correct symbols on a lines

3.      Modify the function prototype of the function printATriangle(). The function will now get two inputs, the symbol the triangle is composed of and the number of lines on the on triangle.

4.      Modify the function main() such that you will now prompt the user to enter a symbol and a number (for the lines of symbols on the triangle).

A sample output once you compile and run your newTriangle.cc is as follows:

 

> CC newTriangle.cc

> ./a.out

How many lines do you want in the triangle: 3

What symbol do you want the triangle to be composed of:@

@@@

@@

@

> ./a.out

How many lines do you want in the triangle: -1

What symbol do you want the triangle to be composed of:*

ERROR: Please enter a value greater than zero.

> ./a.out

How many lines do you want in the triangle: 5

What symbol do you want the triangle to be composed of:+

+++++

++++

+++

++

+

> 

  1. Once you have finished editing all your functions, try compiling and running again. Test your program with several different values to be sure that it works properly.

 

  1. Now you are ready to script your newPrintTriangle.cc program for submission to webCT. Make a script called lab09d.txt that contains all of the following steps.


script lab10d.txt   
to start the script
ls
cat newTriangle.cc
CC newTriangle.cc
./a.out 
   repeat this step with several different inputs
exit  to end the script

You'll submit the files lab10d.txt and newTriangle.cc in the final step of this lab to get extra credit, if you like.

Step 6: Creating a zip file of your .cc files

Create a zip file that contains only the all of your .cc files (from Step 2-5) for this week.

To create and test the zip file, follow the instructions from step11 of lab06 and/or lab07.

Step 7: Upload and Submit your work on WebCT

You will submit the following files to webCT:

You will also print and submit a hard copy of your script files: lab09a.txt, lab09b.txt, lab09c.txt, lab09d.txt, to your TA.

A few important DOs and DON'Ts


Grading: 100 pts

  1. hello.cc (10 pts)
  2. unitConvert.cc (15 pts)
  3. trinangle.cc (30 pts)
  4. newTriangle.cc (20 pts)

End of lab10 for CISC106, Fall 2007 (100 pts)

This lab is copied from and modified with permissions by Dr. Phill Conrad.